Class: Decidim::Budgets::ProjectSearch

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ProjectSearch

Public: Initializes the service. feature - A Decidim::Feature to get the projects from.



10
11
12
13
# File 'app/services/decidim/budgets/project_search.rb', line 10

def initialize(options = {})
  super(Project.all, options)
  @random_seed = options[:random_seed].to_f
end

Instance Method Details

#random_seedObject

Returns the random seed used to randomize the proposals.



37
38
39
40
# File 'app/services/decidim/budgets/project_search.rb', line 37

def random_seed
  @random_seed = (rand * 2 - 1) if @random_seed == 0.0 || @random_seed > 1 || @random_seed < -1
  @random_seed
end

#resultsObject

Returns the random projects for the current page.



29
30
31
32
33
34
# File 'app/services/decidim/budgets/project_search.rb', line 29

def results
  @projects ||= Project.transaction do
    Project.connection.execute("SELECT setseed(#{Project.connection.quote(random_seed)})")
    super.reorder("RANDOM()").load
  end
end

#search_scope_idObject

Handle the scope_id filter



24
25
26
# File 'app/services/decidim/budgets/project_search.rb', line 24

def search_scope_id
  query.where(decidim_scope_id: scope_id)
end

#search_search_textObject

Handle the search_text filter



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

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