Class: Decidim::Plans::PlanSearch

Inherits:
ResourceSearch
  • Object
show all
Defined in:
app/services/decidim/plans/plan_search.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PlanSearch

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



12
13
14
# File 'app/services/decidim/plans/plan_search.rb', line 12

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

Instance Method Details

#search_activityObject

Handle the activity filter



34
35
36
# File 'app/services/decidim/plans/plan_search.rb', line 34

def search_activity
  query
end

#search_originObject

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



23
24
25
26
27
28
29
30
31
# File 'app/services/decidim/plans/plan_search.rb', line 23

def search_origin
  if origin == "official"
    query.where.not(coauthorships_count: 0).joins(:coauthorships).where(decidim_coauthorships: { decidim_author_type: "Decidim::Organization" })
  elsif origin == "citizens"
    query.where.not(coauthorships_count: 0).joins(:coauthorships).where.not(decidim_coauthorships: { decidim_author_type: "Decidim::Organization" })
  else # Assume 'all'
    query
  end
end

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

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 plans that are linked to the given class name.



67
68
69
70
71
72
73
74
75
76
77
# File 'app/services/decidim/plans/plan_search.rb', line 67

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



17
18
19
# File 'app/services/decidim/plans/plan_search.rb', line 17

def search_search_text
  query.where(localized_search_text_in(:title), text: "%#{search_text}%")
end

#search_stateObject

Handle the state filter



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/services/decidim/plans/plan_search.rb', line 39

def search_state
  case state
  when "accepted"
    query.accepted
  when "rejected"
    query.rejected
  when "evaluating"
    query.evaluating
  when "withdrawn"
    query.withdrawn
  when "except_rejected"
    query.except_rejected.except_withdrawn
  else # Assume 'not_withdrawn'
    query.except_withdrawn
  end
end

#search_tag_idObject



79
80
81
82
83
# File 'app/services/decidim/plans/plan_search.rb', line 79

def search_tag_id
  return query unless tag_id.is_a? Array

  query.joins(:tags).where(decidim_plans_tags: { id: tag_id })
end