Sha256: 7107aa9a89450afc49622a5c70d56206e484e0729fe99974f765bcdb3e58f61d

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Decidim
  module EnhancedTextwork
    # A class used to find paragraphs filtered by components and a date range
    class FilteredParagraphs < 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 Paragraphs scoped to an array of components and filtered
      # by a range of dates.
      def query
        paragraphs = Decidim::EnhancedTextwork::Paragraph.where(component: @components)
        paragraphs = paragraphs.where("created_at >= ?", @start_at) if @start_at.present?
        paragraphs = paragraphs.where("created_at <= ?", @end_at) if @end_at.present?
        paragraphs
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-enhanced_textwork-1.0.5 app/queries/decidim/enhanced_textwork/filtered_paragraphs.rb
decidim-enhanced_textwork-1.0.4 app/queries/decidim/enhanced_textwork/filtered_paragraphs.rb
decidim-enhanced_textwork-1.0.3 app/queries/decidim/enhanced_textwork/filtered_paragraphs.rb
decidim-enhanced_textwork-1.0.2 app/queries/decidim/enhanced_textwork/filtered_paragraphs.rb
decidim-enhanced_textwork-1.0.1 app/queries/decidim/enhanced_textwork/filtered_paragraphs.rb
decidim-enhanced_textwork-1.0.0 app/queries/decidim/enhanced_textwork/filtered_paragraphs.rb