Class: Decidim::Search
- Inherits:
-
Rectify::Command
- Object
- Rectify::Command
- Decidim::Search
- Defined in:
- app/commands/decidim/search.rb
Overview
A command that will act as a search service, with all the business logic for performing searches.
Constant Summary collapse
- ACCEPTED_FILTERS =
[:resource_type, :decidim_scope_id].freeze
Instance Attribute Summary collapse
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#term ⇒ Object
readonly
Returns the value of attribute term.
Instance Method Summary collapse
-
#call ⇒ Object
Executes the command.
-
#initialize(term, organization, filters = {}) ⇒ Search
constructor
Public: Initializes the command.
Constructor Details
#initialize(term, organization, filters = {}) ⇒ Search
Public: Initializes the command.
15 16 17 18 19 |
# File 'app/commands/decidim/search.rb', line 15 def initialize(term, organization, filters = {}) @term = term @organization = organization @filters = filters end |
Instance Attribute Details
#results ⇒ Object (readonly)
Returns the value of attribute results.
8 9 10 |
# File 'app/commands/decidim/search.rb', line 8 def results @results end |
#term ⇒ Object (readonly)
Returns the value of attribute term.
8 9 10 |
# File 'app/commands/decidim/search.rb', line 8 def term @term end |
Instance Method Details
#call ⇒ Object
Executes the command. Broadcasts these events:
-
:ok when everything is valid, together with the search results.
-
:invalid if something failed and couldn’t proceed.
Returns nothing.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/commands/decidim/search.rb', line 27 def call query = SearchableResource.where(organization: @organization, locale: I18n.locale) @filters.each_pair do |attribute_name, value| query = query.where(attribute_name => value) if permit_filter?(attribute_name, value) end @results = if term.present? query.global_search(I18n.transliterate(term)) else query.all end broadcast(:ok, @results.order("datetime DESC")) end |