Class: Decidim::Debates::DebateSearch

Inherits:
ResourceSearch
  • Object
show all
Defined in:
app/services/decidim/debates/debate_search.rb

Overview

This class handles search and filtering of debates. Needs a ‘current_component` param with a `Decidim::Component` in order to find the debates.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DebateSearch

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



13
14
15
# File 'app/services/decidim/debates/debate_search.rb', line 13

def initialize(options = {})
  super(Debate.not_hidden, options)
end

Instance Method Details

#search_order_start_timeObject



36
37
38
39
40
41
42
43
44
# File 'app/services/decidim/debates/debate_search.rb', line 36

def search_order_start_time
  if order_start_time == "asc"
    query.order("start_time ASC")
  elsif order_start_time == "desc"
    query.order("start_time DESC")
  else
    query.order("start_time ASC")
  end
end

#search_originObject

Handle the origin filter



26
27
28
29
30
31
32
33
34
# File 'app/services/decidim/debates/debate_search.rb', line 26

def search_origin
  if origin == "official"
    query.where(author: component.organization)
  elsif origin == "citizens"
    query.where.not(decidim_author_type: "Decidim::Organization")
  else # Assume 'all'
    query
  end
end

#search_search_textObject

Handle the search_text filter. We have to cast the JSONB columns into a ‘text` type so that we can search.



19
20
21
22
23
# File 'app/services/decidim/debates/debate_search.rb', line 19

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