Class: Decidim::Search

Inherits:
Rectify::Command
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(term, organization, filters = {}) ⇒ Search

Public: Initializes the command.

Parameters:

  • term:

    The term to search for.

  • organization:

    The Organization to which the results are constrained.

  • filters: (optional)

    A Hash of SearchableResource attributes to filter for.



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

#resultsObject (readonly)

Returns the value of attribute results.



8
9
10
# File 'app/commands/decidim/search.rb', line 8

def results
  @results
end

#termObject (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

#callObject

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