Class: Decidim::Accountability::ResultSearch

Inherits:
ResourceSearch
  • Object
show all
Defined in:
app/services/decidim/accountability/result_search.rb

Overview

This class handles search and filtering of results. Needs a ‘current_component` param with a `Decidim::Component` in order to find the results.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ResultSearch

Public: Initializes the service.

options - A hash of options to modify the search. These options will be

converted to methods by SearchLight so they can be used on filter
methods. (Default {})
* component - A Decidim::Component to get the results from.
* organization - A Decidim::Organization object.
* parent_id - The parent ID of the result. The value is forced to false to force
              the filter execution when the value is nil
* deep_search - Whether to perform the search on all children levels or just the
                first one. True by default.


20
21
22
23
24
25
# File 'app/services/decidim/accountability/result_search.rb', line 20

def initialize(options = {})
  options = options.dup
  options[:deep_search] = true if options[:deep_search].nil?
  options[:parent_id] = "root" if options[:parent_id].nil?
  super(Result.all, options)
end

Instance Method Details

#search_parent_idObject

Handle parent_id filter



35
36
37
38
39
40
41
42
43
44
# File 'app/services/decidim/accountability/result_search.rb', line 35

def search_parent_id
  parent_id = options[:parent_id]
  parent_id = nil if parent_id == "root"

  if options[:deep_search]
    query.where(parent_id: [parent_id] + children_ids(parent_id))
  else
    query.where(parent_id: parent_id)
  end
end

#search_search_textObject

Handle the search_text filter



28
29
30
31
32
# File 'app/services/decidim/accountability/result_search.rb', line 28

def search_search_text
  query
    .where(localized_search_text_in(:title), text: "%#{search_text}%")
    .or(query.where(localized_search_text_in(:description), text: "%#{search_text}%"))
end