Sha256: 8a52fe1536a56ce4f00bf8a0071c017f595db55edeca4aa9c91d88978c1b1e57

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true
module Decidim
  module Results
    # A class used to find results filtered by features and a date range
    class FilteredResults < Rectify::Query
      # 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.
      def self.for(features, start_at = nil, end_at = nil)
        new(features, start_at, end_at).query
      end

      # 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.
      def initialize(features, start_at = nil, end_at = nil)
        @features = features
        @start_at = start_at
        @end_at = end_at
      end

      # Finds the Results scoped to an array of features and filtered
      # by a range of dates.
      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
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
decidim-results-0.1.0 app/queries/decidim/results/filtered_results.rb
decidim-0.1.0 decidim-results/app/queries/decidim/results/filtered_results.rb