Class: Decidim::Verifications::AuthorizationsBeforeDate

Inherits:
Query
  • Object
show all
Defined in:
decidim-verifications/app/queries/decidim/verifications/authorizations_before_date.rb

Overview

Finds authorizations by different criteria

Instance Method Summary collapse

Methods inherited from Query

#cached_query, #each, #eager?, #exists?, merge, #none?, #relation?, #|

Constructor Details

#initialize(organization:, date:, granted: true, impersonated_only: false) ⇒ AuthorizationsBeforeDate

Initializes the class.

Parameters:

  • organization (Organization)

    The organization where this authorization belongs to

  • date (Date)

    The verification’s date of an authorization

  • granted (boolean) (defaults to: true)

    Whether return granted auths only or not granted only

  • impersonated_only (boolean) (defaults to: false)

    Whether return impersonated auths only



13
14
15
16
17
18
# File 'decidim-verifications/app/queries/decidim/verifications/authorizations_before_date.rb', line 13

def initialize(organization:, date:, granted: true, impersonated_only: false)
  @organization = organization
  @date = date
  @granted = granted
  @impersonated_only = impersonated_only
end

Instance Method Details

#queryObject

Finds the Authorizations for the given method

Returns an ActiveRecord::Relation.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'decidim-verifications/app/queries/decidim/verifications/authorizations_before_date.rb', line 23

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?

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