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.



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

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.



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

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.



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

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

#search_search_textObject

Handle the search_text filter



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

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}%"))
end