Class: Decidim::Results::FilteredResults

Inherits:
Rectify::Query
  • Object
show all
Defined in:
decidim-results/app/queries/decidim/results/filtered_results.rb

Overview

A class used to find results filtered by features and a date range

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(features, start_at = nil, end_at = nil) ⇒ FilteredResults

Initializes the class.

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



21
22
23
24
25
# File 'decidim-results/app/queries/decidim/results/filtered_results.rb', line 21

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

Class Method Details

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

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

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



12
13
14
# File 'decidim-results/app/queries/decidim/results/filtered_results.rb', line 12

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

Instance Method Details

#queryObject

Finds the Results scoped to an array of features and filtered by a range of dates.



29
30
31
32
33
34
# File 'decidim-results/app/queries/decidim/results/filtered_results.rb', line 29

def query
  results = Decidim::Results::Result.where(feature: @features)
  results = results.where("created_at >= ?", @start_at) if @start_at.present?
  results = results.where("created_at <= ?", @end_at) if @end_at.present?
  results
end