Class: Decidim::Elections::ElectionSearch

Inherits:
ResourceSearch
  • Object
show all
Defined in:
decidim-elections/app/services/decidim/elections/election_search.rb

Overview

This class handles search and filtering of elections.

Instance Attribute Summary

Attributes inherited from ResourceSearch

#component, #organization, #user

Instance Method Summary collapse

Methods inherited from ResourceSearch

#base_query, #results, #search_category_id, #search_origin, #search_scope_id, text_search_fields

Constructor Details

#initialize(options = {}) ⇒ ElectionSearch

Public: Initializes the service. component - A Decidim::Component to get the election from. page - The page number to paginate the results. per_page - The number of proposals to return per page.


11
12
13
# File 'decidim-elections/app/services/decidim/elections/election_search.rb', line 11

def initialize(options = {})
  super(Election.all, options)
end

Instance Method Details

#search_search_textObject

Handle the search_text filter


16
17
18
19
20
# File 'decidim-elections/app/services/decidim/elections/election_search.rb', line 16

def search_search_text
  query
    .where("decidim_elections_elections.title::text ILIKE ?", "%#{search_text}%")
    .or(query.where("decidim_elections_elections.description::text ILIKE ?", "%#{search_text}%"))
end

#search_stateObject

Handle the state filter


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

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