Class: Decidim::Proposals::ProposalSearch
- Inherits:
-
ResourceSearch
- Object
- ResourceSearch
- Decidim::Proposals::ProposalSearch
- 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
-
#initialize(options = {}) ⇒ ProposalSearch
constructor
Public: Initializes the service.
-
#search_activity ⇒ Object
Handle the activity filter.
-
#search_origin ⇒ Object
Handle the origin filter The ‘official’ proposals doesn’t have an author id.
-
#search_related_to ⇒ Object
Filters Proposals by the name of the classes they are linked to.
-
#search_search_text ⇒ Object
Handle the search_text filter.
-
#search_state ⇒ Object
Handle the state filter.
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( = {}) super(Proposal.all, ) end |
Instance Method Details
#search_activity ⇒ Object
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: [:current_user] }) else query end end |
#search_origin ⇒ Object
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 |
#search_related_to ⇒ Object
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 from = query .joins(:resource_links_from) .where(decidim_resource_links: { to_type: .camelcase }) to = query .joins(:resource_links_to) .where(decidim_resource_links: { from_type: .camelcase }) query.where(id: from).or(query.where(id: to)) end |
#search_search_text ⇒ Object
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_state ⇒ Object
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 |