Class: Decidim::Proposals::ProposalSearch

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

Overview

A service to encapsualte all the logic when searching and filtering proposals in a participatory process.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ProposalSearch

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



11
12
13
# File 'decidim-proposals/app/services/decidim/proposals/proposal_search.rb', line 11

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

Instance Method Details

#search_activityObject

Handle the activity filter



35
36
37
38
39
40
41
42
43
44
45
# File 'decidim-proposals/app/services/decidim/proposals/proposal_search.rb', line 35

def search_activity
  if activity.include? "voted"
    query
      .includes(:votes)
      .where(decidim_proposals_proposal_votes: {
               decidim_author_id: options[:current_user]
             })
  else
    query
  end
end

#search_originObject

Handle the origin filter The ‘official’ proposals doesn’t have an author id



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

def search_origin
  if origin == "official"
    query.where(decidim_author_id: nil)
  elsif origin == "citizenship"
    query.where.not(decidim_author_id: nil)
  else # Assume 'all'
    query
  end
end

Filters Proposals by the name of the classes they are linked to. By default, returns all Proposals. When a ‘related_to` param is given, then it camelcases item to find the real class name and checks the links for the Proposals.

The ‘related_to` param is expected to be in this form:

"decidim/meetings/meeting"

This can be achieved by performing ‘klass.name.underscore`.

Returns only those proposals that are linked to the given class name.



69
70
71
72
73
74
75
76
77
78
79
# File 'decidim-proposals/app/services/decidim/proposals/proposal_search.rb', line 69

def search_related_to
  from = query
         .joins(:resource_links_from)
         .where(decidim_resource_links: { to_type: related_to.camelcase })

  to = query
       .joins(:resource_links_to)
       .where(decidim_resource_links: { from_type: related_to.camelcase })

  query.where(id: from).or(query.where(id: to))
end

#search_search_textObject

Handle the search_text filter



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

def search_search_text
  query
    .where("title ILIKE ?", "%#{search_text}%")
    .or(query.where("body ILIKE ?", "%#{search_text}%"))
end

#search_stateObject

Handle the state filter



48
49
50
51
52
53
54
55
56
# File 'decidim-proposals/app/services/decidim/proposals/proposal_search.rb', line 48

def search_state
  if state == "accepted"
    query.accepted
  elsif state == "rejected"
    query.rejected
  else # Assume 'all'
    query
  end
end