Class: Decidim::Initiatives::InitiativeSearch

Inherits:
Searchlight::Search
  • Object
show all
Includes:
CurrentLocale
Defined in:
decidim-initiatives/app/services/decidim/initiatives/initiative_search.rb

Overview

Service that encapsulates all logic related to filtering initiatives.

Instance Method Summary collapse

Methods included from CurrentLocale

#current_locale

Constructor Details

#initialize(options = {}) ⇒ InitiativeSearch

Public: Initializes the service. page - The page number to paginate the results. per_page - The number of proposals to return per page.


12
13
14
# File 'decidim-initiatives/app/services/decidim/initiatives/initiative_search.rb', line 12

def initialize(options = {})
  super(options)
end

Instance Method Details

#base_queryObject


16
17
18
19
20
21
22
# File 'decidim-initiatives/app/services/decidim/initiatives/initiative_search.rb', line 16

def base_query
  Decidim::Initiative
    .includes(scoped_type: [:scope])
    .joins("JOIN decidim_users ON decidim_users.id = decidim_initiatives.decidim_author_id")
    .where(organization: options[:organization])
    .published
end

#search_area_idObject


92
93
94
95
96
# File 'decidim-initiatives/app/services/decidim/initiatives/initiative_search.rb', line 92

def search_area_id
  return query if area_ids.include?("all")

  query.where(decidim_area_id: area_ids)
end

#search_authorObject


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'decidim-initiatives/app/services/decidim/initiatives/initiative_search.rb', line 66

def search_author
  if author == "myself" && options[:current_user]
    co_authoring_initiative_ids = Decidim::InitiativesCommitteeMember.where(
      decidim_users_id: options[:current_user].id
    ).pluck(:decidim_initiatives_id)

    query.where(decidim_author_id: options[:current_user].id, decidim_author_type: Decidim::UserBaseEntity.name)
         .or(query.where(id: co_authoring_initiative_ids))
         .unscope(where: :published_at)
  else
    query
  end
end

#search_scope_idObject


80
81
82
83
84
85
86
87
88
89
90
# File 'decidim-initiatives/app/services/decidim/initiatives/initiative_search.rb', line 80

def search_scope_id
  return query if scope_ids.include?("all")

  clean_scope_ids = scope_ids

  conditions = []
  conditions << "decidim_initiatives_type_scopes.decidim_scopes_id IS NULL" if clean_scope_ids.delete("global")
  conditions.concat(["? = ANY(decidim_scopes.part_of)"] * clean_scope_ids.count) if clean_scope_ids.any?

  query.joins(:scoped_type).references(:decidim_scopes).where(conditions.join(" OR "), *clean_scope_ids.map(&:to_i))
end

#search_search_textObject

Handle the search_text filter


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'decidim-initiatives/app/services/decidim/initiatives/initiative_search.rb', line 25

def search_search_text
  query
    .where("title->>'#{current_locale}' ILIKE ?", "%#{search_text}%")
    .or(
      query.where(
        "description->>'#{current_locale}' ILIKE ?",
        "%#{search_text}%"
      )
    )
    .or(
      query.where(
        "cast(decidim_initiatives.id as text) ILIKE ?", "%#{search_text}%"
      )
    )
    .or(
      query.where(
        "decidim_users.name ILIKE ? OR decidim_users.nickname ILIKE ?", "%#{search_text}%", "%#{search_text}%"
      )
    )
end

#search_stateObject

Handle the state filter


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

def search_state
  ids = []
  ids += state.member?("accepted") ? query.accepted.ids : []
  ids += state.member?("rejected") ? query.rejected.ids : []
  ids += state.member?("answered") ? query.answered.ids : []
  ids += state.member?("open") ? query.open.ids : []
  ids += state.member?("closed") ? query.closed.ids : []

  query.where(id: ids)
end

#search_type_idObject


58
59
60
61
62
63
64
# File 'decidim-initiatives/app/services/decidim/initiatives/initiative_search.rb', line 58

def search_type_id
  return query if type_ids.include?("all")

  types = InitiativesTypeScope.where(decidim_initiatives_types_id: type_ids).pluck(:id)

  query.where(scoped_type: types)
end