Class: Decidim::Search
- Defined in:
- decidim-core/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
- HIGHLIGHTED_RESULTS_COUNT =
4
Instance Method Summary collapse
-
#call ⇒ Object
Executes the command.
-
#initialize(term, organization, filters = {}, page_params = {}) ⇒ Search
constructor
Public: Initializes the command.
Methods inherited from Command
call, #evaluate, #method_missing, #respond_to_missing?, #transaction, #with_events
Constructor Details
#initialize(term, organization, filters = {}, page_params = {}) ⇒ Search
Public: Initializes the command.
14 15 16 17 18 19 |
# File 'decidim-core/app/commands/decidim/search.rb', line 14 def initialize(term, organization, filters = {}, page_params = {}) @term = term @organization = organization @filters = filters.with_indifferent_access @page_params = page_params end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Decidim::Command
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 could not proceed.
Returns nothing.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'decidim-core/app/commands/decidim/search.rb', line 27 def call search_results = Decidim::Searchable.searchable_resources.inject({}) do |results_by_type, (class_name, klass)| result_ids = filtered_query_for(class_name).pluck(:resource_id) results_count = result_ids.count results = if filters[:with_resource_type].present? && filters[:with_resource_type] == class_name paginate(klass.order_by_id_list(result_ids)) elsif filters[:with_resource_type].present? ApplicationRecord.none else klass.order_by_id_list(result_ids.take(HIGHLIGHTED_RESULTS_COUNT)) end uncommentable_resources = uncommentable_resources(results) if results.present? if uncommentable_resources.present? results -= uncommentable_resources results_count -= uncommentable_resources.count end results_by_type.update(class_name => { count: results_count, results: }) end broadcast(:ok, search_results) end |