Sha256: 448c59cac7cfff3eb0a22fc05cde6952c1e8997a99afc54fb74c208c52fb6469

Contents?: true

Size: 1.48 KB

Versions: 33

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Decidim
  module Budgets
    # A class used to find projects filtered by components and a date range
    class FilteredProjects < Rectify::Query
      # 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.
      def self.for(components, start_at = nil, end_at = nil)
        new(components, start_at, end_at).query
      end

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

      # Finds the Projects scoped to an array of components and filtered
      # by a range of dates.
      def query
        projects = Decidim::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

      private

      attr_reader :components

      def budgets
        @budgets ||= Decidim::Budgets::Budget.where(component: components).order(weight: :asc)
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
decidim-budgets-0.26.10 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.9 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.8 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.7 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.5 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.4 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.3 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.2 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.1 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.0 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.0.rc2 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.26.0.rc1 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.25.2 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.25.1 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.25.0 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.25.0.rc4 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.25.0.rc3 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.25.0.rc2 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.25.0.rc1 app/queries/decidim/budgets/filtered_projects.rb
decidim-budgets-0.24.3 app/queries/decidim/budgets/filtered_projects.rb