Class: Decidim::Votings::VotingSearch

Inherits:
Searchlight::Search
  • Object
show all
Defined in:
app/services/decidim/votings/voting_search.rb

Overview

Service that encapsulates all logic related to filtering votings.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ VotingSearch

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



10
11
12
# File 'app/services/decidim/votings/voting_search.rb', line 10

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

Instance Method Details

#base_queryObject



14
15
16
# File 'app/services/decidim/votings/voting_search.rb', line 14

def base_query
  Decidim::Votings::Voting.where(organization: options[:organization]).published
end

#search_search_textObject

Handle the search_text filter



19
20
21
22
23
24
25
# File 'app/services/decidim/votings/voting_search.rb', line 19

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



28
29
30
31
32
33
34
35
36
37
# File 'app/services/decidim/votings/voting_search.rb', line 28

def search_state
  active = state.member?("active") ? query.active : nil
  upcoming = state.member?("upcoming") ? query.upcoming : nil
  finished = state.member?("finished") ? query.finished : nil

  query
    .where(id: upcoming)
    .or(query.where(id: active))
    .or(query.where(id: finished))
end