Class: Decidim::Proposals::ProposalSearch

Inherits:
ResourceSearch
  • Object
show all
Defined in:
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. component - A Decidim::Component to get the proposals from. page - The page number to paginate the results. per_page - The number of proposals to return per page.



14
15
16
17
18
# File 'app/services/decidim/proposals/proposal_search.rb', line 14

def initialize(options = {})
  options[:scope] = options.fetch(:scope, Proposal)
  options[:scope] = options[:state_withdraw] == "withdrawn" ? options[:scope].withdrawn : options[:scope].except_withdrawn
  super(options[:scope], options)
end

Instance Method Details

#search_activityObject

Handle the activity filter



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/decidim/proposals/proposal_search.rb', line 21

def search_activity
  case activity
  when "voted"
    query
      .includes(:votes)
      .where(decidim_proposals_proposal_votes: { decidim_author_id: user })
  when "my_proposals"
    query
      .where.not(coauthorships_count: 0)
      .joins(:coauthorships)
      .where(decidim_coauthorships: { decidim_author_type: "Decidim::UserBaseEntity" })
      .where(decidim_coauthorships: { decidim_author_id: user })
  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.



72
73
74
75
76
77
78
79
80
81
82
# File 'app/services/decidim/proposals/proposal_search.rb', line 72

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_stateObject

Handle the state filter



45
46
47
# File 'app/services/decidim/proposals/proposal_search.rb', line 45

def search_state
  apply_scopes(%w(accepted rejected evaluating state_not_published), state)
end

#search_state_withdrawObject



38
39
40
41
42
# File 'app/services/decidim/proposals/proposal_search.rb', line 38

def search_state_withdraw
  return query if state_withdraw == "withdrawn"

  query.except_withdrawn
end

#search_typeObject

Handle the amendment type filter



50
51
52
53
54
55
56
57
58
59
# File 'app/services/decidim/proposals/proposal_search.rb', line 50

def search_type
  case type
  when "proposals"
    query.only_amendables
  when "amendments"
    query.only_visible_emendations_for(user, component)
  else # Assume 'all'
    query.amendables_and_visible_emendations_for(user, component)
  end
end