Class: Decidim::Verifications::Authorizations

Inherits:
Query
  • Object
show all
Defined in:
decidim-verifications/app/queries/decidim/verifications/authorizations.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:, user: nil, name: nil, granted: nil) ⇒ Authorizations

Initializes the class.

Parameters:

  • organization (Organization)

    The organization where this authorization belongs to

  • name (String) (defaults to: nil)

    The name of an authorization method

  • user (User) (defaults to: nil)

    A user to find authorizations for

  • granted (Boolean) (defaults to: nil)

    Whether the authorization is granted or not



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

def initialize(organization:, user: nil, name: nil, granted: nil)
  @organization = organization
  @user = user
  @name = name
  @granted = granted
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
# File 'decidim-verifications/app/queries/decidim/verifications/authorizations.rb', line 23

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

  scope = scope.where(name:) unless name.nil?
  scope = scope.where(user:) unless user.nil?

  case granted
  when true
    scope = scope.where.not(granted_at: nil)
  when false
    scope = scope.where(granted_at: nil)
  end

  scope
end