Class: Decidim::Lausanne::Budgets::ProjectSearch

Inherits:
ResourceSearch
  • Object
show all
Defined in:
app/services/decidim/lausanne/budgets/project_search.rb

Overview

This class handles search and filtering of projects. Needs a ‘current_component` param with a `Decidim::Component` in order to find the projects.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ProjectSearch

Public: Initializes the service. component - A Decidim::Component to get the projects from.



13
14
15
# File 'app/services/decidim/lausanne/budgets/project_search.rb', line 13

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

Instance Method Details

#base_queryObject

Creates the SearchLight base query.



26
27
28
29
30
# File 'app/services/decidim/lausanne/budgets/project_search.rb', line 26

def base_query
  raise "Missing budget" unless budget
  raise "Missing component" unless component
  @scope.where(budget: budget)
end

#resultObject

Returns the random projects for the current page.



45
46
47
# File 'app/services/decidim/lausanne/budgets/project_search.rb', line 45

def result
  Project.where(id: results.pluck(:id)).includes([:scope, :component, :attachments, :category])
end

#search_descriptionObject

Another search method.



39
40
41
42
43
# File 'app/services/decidim/lausanne/budgets/project_search.rb', line 39

def search_description
  # If `"description"` was the second key in the options_hash,
  # `query` here will be whatever `search_first_name` returned.
  query.where(description: description)
end

#search_search_textObject



16
17
18
19
20
21
22
23
24
# File 'app/services/decidim/lausanne/budgets/project_search.rb', line 16

def search_search_text
  return query unless self.class.text_search_fields.any?
  fields = self.class.text_search_fields.dup
  text_query = query.where(localized_search_text_in("#{query.table_name}.#{fields.shift}"), text: "%#{search_text}%")
  fields.each do |field|
    text_query = text_query.or(query.where(localized_search_text_in("#{query.table_name}.#{field}"), text: "%#{search_text}%"))
  end
  text_query
end

#search_statusObject



49
50
51
52
53
# File 'app/services/decidim/lausanne/budgets/project_search.rb', line 49

def search_status
  return query if status.member?("all")

  apply_scopes(%w(selected not_selected), status)
end

#search_titleObject

A search method.



32
33
34
35
36
# File 'app/services/decidim/lausanne/budgets/project_search.rb', line 32

def search_title
  # If `"title"` was the first key in the options_hash,
  # `query` here will be the base query, namely, `Person.all`.
  query.where(title: options[:title])
end