Module: IdentityToolbox::DefaultPolicy

Includes:
PolicyMethods
Defined in:
lib/identity_toolbox/default_policy.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

IDENTITY_ATTRIBUTES =
%i[account_id client_id identification_document
sinacor_advisor_id].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



74
75
76
# File 'lib/identity_toolbox/default_policy.rb', line 74

def record
  @record
end

#userObject (readonly)

Returns the value of attribute user.



74
75
76
# File 'lib/identity_toolbox/default_policy.rb', line 74

def user
  @user
end

Class Method Details

.included(klass) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



51
52
53
54
55
56
# File 'lib/identity_toolbox/default_policy.rb', line 51

def self.included(klass)
  klass.class_eval do
    extend ClassMethods
    klass.const_set('Scope', DefaultPolicy.policy_class(klass))
  end
end

.policy_class(klass) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/identity_toolbox/default_policy.rb', line 11

def self.policy_class(klass)
  Class.new do
    include PolicyMethods

    attr_reader :user, :scope

    delegate :attributes, to: klass.name.to_sym

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      return scope if queries.blank?

      queries.reduce(nil) do |new_scope, query|
        if new_scope.nil?
          scope.where(query)
        else
          new_scope.or(scope.where(query))
        end
      end
    end

    private

    def queries
      IDENTITY_ATTRIBUTES.map(&method(:to_query)).compact
    end

    def to_query(attribute)
      return if attributes.nil? || attributes[attribute].nil?

      { attributes[attribute] => send("#{attribute}s") }
    end
  end
end

Instance Method Details

#allowed?Boolean Also known as: show?, create?, update?, destroy?

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/identity_toolbox/default_policy.rb', line 81

def allowed?
  IDENTITY_ATTRIBUTES.map(&method(:include_attribute?)).
    compact.reduce(:|)
end

#initialize(user, record) ⇒ Object



76
77
78
79
# File 'lib/identity_toolbox/default_policy.rb', line 76

def initialize(user, record)
  @user = user
  @record = record
end