Class: Decidim::Initiatives::InitiativeSearch

Inherits:
Searchlight::Search
  • Object
show all
Includes:
CurrentLocale
Defined in:
app/services/decidim/initiatives/initiative_search.rb

Overview

Service that encapsulates all logic related to filtering initiatives.

Instance Method Summary collapse

Methods included from CurrentLocale

#current_locale

Constructor Details

#initialize(options = {}) ⇒ InitiativeSearch

Public: Initializes the service. page - The page number to paginate the results. per_page - The number of proposals to return per page.



12
13
14
# File 'app/services/decidim/initiatives/initiative_search.rb', line 12

def initialize(options = {})
  super(options)
end

Instance Method Details

#base_queryObject



16
17
18
19
20
# File 'app/services/decidim/initiatives/initiative_search.rb', line 16

def base_query
  Decidim::Initiative
    .includes(scoped_type: [:scope])
    .where(organization: options[:organization])
end

#search_authorObject



52
53
54
55
56
57
58
# File 'app/services/decidim/initiatives/initiative_search.rb', line 52

def search_author
  if author == "myself" && options[:current_user]
    query.where(decidim_author_id: options[:current_user].id)
  else
    query
  end
end

#search_scope_idObject



60
61
62
63
64
65
66
67
# File 'app/services/decidim/initiatives/initiative_search.rb', line 60

def search_scope_id
  return if scope_id.nil?
  query
    .joins(:scoped_type)
    .where(
      "decidim_initiatives_type_scopes.decidim_scopes_id": scope_id
    )
end

#search_search_textObject

Handle the search_text filter



23
24
25
26
27
28
29
30
31
32
# File 'app/services/decidim/initiatives/initiative_search.rb', line 23

def search_search_text
  query
    .where("title->>'#{current_locale}' ILIKE ?", "%#{search_text}%")
    .or(
      query.where(
        "description->>'#{current_locale}' ILIKE ?",
        "%#{search_text}%"
      )
    )
end

#search_stateObject

Handle the state filter



35
36
37
38
39
40
41
42
# File 'app/services/decidim/initiatives/initiative_search.rb', line 35

def search_state
  case state
  when "closed"
    query.closed
  else # Assume open
    query.open
  end
end

#search_typeObject



44
45
46
47
48
49
50
# File 'app/services/decidim/initiatives/initiative_search.rb', line 44

def search_type
  return query if type == "all"

  types = InitiativesTypeScope.where(decidim_initiatives_types_id: type).pluck(:id)

  query.where(scoped_type: types)
end