Module: ForemanTasks::Task::Search

Included in:
ForemanTasks::Task
Defined in:
app/models/foreman_tasks/task/search.rb

Constant Summary collapse

SUPPORTED_DURATION_FORMAT =

Expects the time in the format “d+ (seconds|minutes|hours|days|months|years)”

/\A\s*(\d+(\s+\b(seconds?|minutes?|hours?|days?|months?|years?)\b)?)\b\s*\z/i.freeze

Instance Method Summary collapse

Instance Method Details

#search_by_duration(_key, operator, value) ⇒ Object



30
31
32
33
34
35
# File 'app/models/foreman_tasks/task/search.rb', line 30

def search_by_duration(_key, operator, value)
  raise "Unsupported duration '#{value}' specified for searching" unless value =~ SUPPORTED_DURATION_FORMAT
  value = value.strip
  { :conditions => "coalesce(ended_at, current_timestamp) - coalesce(coalesce(started_at, ended_at), current_timestamp) #{operator} ?::interval",
    :parameter => [value] }
end

#search_by_generic_resource(key, operator, value) ⇒ Object



4
5
6
7
8
9
10
11
# File 'app/models/foreman_tasks/task/search.rb', line 4

def search_by_generic_resource(key, operator, value)
  key = 'resource_type' if key.blank?
  key_name = connection.quote_column_name(key.sub(/^.*\./, ''))
  value = value.split(',') if operator.index(/IN/i)
  condition = sanitize_sql_for_conditions(["foreman_tasks_links.#{key_name} #{operator} (?)", value])

  { :conditions => condition, :joins => :links }
end

#search_by_taxonomy(key, operator, value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/foreman_tasks/task/search.rb', line 13

def search_by_taxonomy(key, operator, value)
  uniq_suffix = SecureRandom.hex(3)
  resource_type = key == 'location_id' ? 'Location' : 'Organization'

  joins = <<-SQL
  LEFT JOIN foreman_tasks_links AS foreman_tasks_links_taxonomy#{uniq_suffix}
  ON (foreman_tasks_links_taxonomy#{uniq_suffix}.task_id = foreman_tasks_tasks.id AND
      foreman_tasks_links_taxonomy#{uniq_suffix}.resource_type = '#{resource_type}')
  SQL
  # Select only those tasks which either have the correct taxonomy or are not related to any
  sql = "foreman_tasks_links_taxonomy#{uniq_suffix}.resource_id #{operator} (?) OR foreman_tasks_links_taxonomy#{uniq_suffix}.resource_id IS NULL"
  value = value.split(',') if operator.index(/IN/i)
  { :conditions => sanitize_sql_for_conditions([sql, value]), :joins => joins }
end