Class: Decidim::Lausanne::Budgets::FilteredProjects

Inherits:
Query
  • Object
show all
Defined in:
app/queries/decidim/lausanne/budgets/filtered_projects.rb

Overview

A class used to find projects filtered by components and a date range

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components, start_at = nil, end_at = nil) ⇒ FilteredProjects

Initializes the class.

components - An array of Decidim::Component start_at - A date to filter resources created after it end_at - A date to filter resources created before it.



22
23
24
25
26
# File 'app/queries/decidim/lausanne/budgets/filtered_projects.rb', line 22

def initialize(components, start_at = nil, end_at = nil)
  @components = components
  @start_at = start_at
  @end_at = end_at
end

Class Method Details

.for(components, start_at = nil, end_at = nil) ⇒ Object

Syntactic sugar to initialize the class and return the queried objects.

components - An array of Decidim::Component start_at - A date to filter resources created after it end_at - A date to filter resources created before it.



13
14
15
# File 'app/queries/decidim/lausanne/budgets/filtered_projects.rb', line 13

def self.for(components, start_at = nil, end_at = nil)
  new(components, start_at, end_at).query
end

Instance Method Details

#queryObject

Finds the Projects scoped to an array of components and filtered by a range of dates.



30
31
32
33
34
35
# File 'app/queries/decidim/lausanne/budgets/filtered_projects.rb', line 30

def query
  projects = Decidim::Lausanne::Budgets::Project.where(budget: budgets)
  projects = projects.where("created_at >= ?", @start_at) if @start_at.present?
  projects = projects.where("created_at <= ?", @end_at) if @end_at.present?
  projects
end