Sha256: d68b31cd2efde20e78f083aeca1b964d069f2f50d890dbf9032ec7f06c69bbf3

Contents?: true

Size: 1.7 KB

Versions: 9

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Decidim
  module Verifications
    # Finds authorizations by different criteria
    class AuthorizationsBeforeDate < Rectify::Query
      # Initializes the class.
      #
      # @param organization [Organization] The organization where this authorization belongs to
      # @param date [Date] The verification's date of an authorization
      # @param granted [boolean] Whether return granted auths only or not granted only
      # @param impersonated_only [boolean] Whether return impersonated auths only
      def initialize(organization:, date:, granted: true, impersonated_only: false)
        @organization = organization
        @date = date
        @granted = granted
        @impersonated_only = impersonated_only
      end

      # Finds the Authorizations for the given method
      #
      # Returns an ActiveRecord::Relation.
      def query
        return Decidim::Authorization.none unless organization

        query = Decidim::Authorization.left_outer_joins(:organization).where(decidim_organizations: { id: organization.id })

        if impersonated_only
          query = query
                  .left_outer_joins(:user)
                  .where(decidim_users: { decidim_organization_id: organization.id })
                  .where(decidim_users: { managed: true })
        end

        query = query.where("#{Decidim::Authorization.table_name}.created_at < ?", date) unless date.nil?

        query = if granted
                  query.where.not(granted_at: nil)
                else
                  query.where(granted_at: nil)
                end

        query
      end

      private

      attr_reader :organization, :date, :granted, :impersonated_only
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
decidim-verifications-0.23.6 app/queries/decidim/verifications/authorizations_before_date.rb
decidim-verifications-0.23.5 app/queries/decidim/verifications/authorizations_before_date.rb
decidim-verifications-0.23.4 app/queries/decidim/verifications/authorizations_before_date.rb
decidim-verifications-0.23.3 app/queries/decidim/verifications/authorizations_before_date.rb
decidim-verifications-0.23.2 app/queries/decidim/verifications/authorizations_before_date.rb
decidim-verifications-0.23.1 app/queries/decidim/verifications/authorizations_before_date.rb
decidim-verifications-0.23.1.rc1 app/queries/decidim/verifications/authorizations_before_date.rb
decidim-verifications-0.23.0 app/queries/decidim/verifications/authorizations_before_date.rb
decidim-verifications-0.22.0 app/queries/decidim/verifications/authorizations_before_date.rb