Class: GraphQR::Policies::PunditProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/graphqr/policies/pundit_provider.rb

Overview

This is a wrapper around Pundit provided to keep all PolicyProviders with the same methods.

If you want to use the Pundit integration with our extensions you should pass:

“‘

policy_provider: GraphQR::Policies::PunditProvider.new(policy_context: pundit_user)

“‘

To the Schema context.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy_context:) ⇒ PunditProvider

Returns a new instance of PunditProvider.



22
23
24
# File 'lib/graphqr/policies/pundit_provider.rb', line 22

def initialize(policy_context:)
  @policy_context = policy_context
end

Instance Attribute Details

#policy_contextObject (readonly)

Returns the value of attribute policy_context.



20
21
22
# File 'lib/graphqr/policies/pundit_provider.rb', line 20

def policy_context
  @policy_context
end

Instance Method Details

#allowed?(action:, record:, policy_class: nil) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/graphqr/policies/pundit_provider.rb', line 26

def allowed?(action:, record:, policy_class: nil)
  policy = policy_for(record: record, policy_class: policy_class)

  policy.apply(action)
end

#authorized_records(records:) ⇒ Object



32
33
34
# File 'lib/graphqr/policies/pundit_provider.rb', line 32

def authorized_records(records:)
  Pundit.policy_scope(policy_context, records)
end

#permitted_field?(record:, field_name:) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/graphqr/policies/pundit_provider.rb', line 36

def permitted_field?(record:, field_name:)
  policy = policy_for(record: record)

  policy.permitted_fields.include?(field_name)
end