Module: ActionPolicy::Behaviours::PolicyFor
- Included in:
- ActionPolicy::Behaviour, Policy::Core
- Defined in:
- lib/action_policy/behaviours/policy_for.rb
Overview
Adds ‘policy_for` method
Instance Method Summary collapse
- #authorization_context ⇒ Object
- #authorization_namespace ⇒ Object
- #authorization_strict_namespace ⇒ Object
- #build_authorization_context ⇒ Object
- #default_authorization_policy_class ⇒ Object
-
#implicit_authorization_target ⇒ Object
Override this method to provide implicit authorization target that would be used in case ‘record` is not specified in `authorize!` and `allowed_to?` call.
-
#implicit_authorization_target! ⇒ Object
Return implicit authorization target or raises an exception if it’s nil.
-
#policy_for(record:, with: nil, namespace: authorization_namespace, context: nil, allow_nil: false, default: default_authorization_policy_class, strict_namespace: authorization_strict_namespace) ⇒ Object
Returns policy instance for the record.
- #policy_for_cache_key(record:, with: nil, namespace: nil, context: authorization_context) ⇒ Object
Instance Method Details
#authorization_context ⇒ Object
21 |
# File 'lib/action_policy/behaviours/policy_for.rb', line 21 def = @authorization_context ||= |
#authorization_namespace ⇒ Object
27 28 29 |
# File 'lib/action_policy/behaviours/policy_for.rb', line 27 def # override to provide specific authorization namespace end |
#authorization_strict_namespace ⇒ Object
35 36 37 |
# File 'lib/action_policy/behaviours/policy_for.rb', line 35 def # override to provide strict namespace lookup option end |
#build_authorization_context ⇒ Object
23 24 25 |
# File 'lib/action_policy/behaviours/policy_for.rb', line 23 def Kernel.raise NotImplementedError, "Please, define `build_authorization_context` method!" end |
#default_authorization_policy_class ⇒ Object
31 32 33 |
# File 'lib/action_policy/behaviours/policy_for.rb', line 31 def # override to provide a policy class use when no policy found end |
#implicit_authorization_target ⇒ Object
Override this method to provide implicit authorization target that would be used in case ‘record` is not specified in `authorize!` and `allowed_to?` call.
It is also used to infer a policy for scoping (in ‘authorized_scope` method).
44 45 46 |
# File 'lib/action_policy/behaviours/policy_for.rb', line 44 def # no-op end |
#implicit_authorization_target! ⇒ Object
Return implicit authorization target or raises an exception if it’s nil
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/action_policy/behaviours/policy_for.rb', line 49 def || Kernel.raise( NotFound, [ self, "Couldn't find implicit authorization target " \ "for #{self.class}. " \ "Please, provide policy class explicitly using `with` option or " \ "define the `implicit_authorization_target` method." ] ) end |
#policy_for(record:, with: nil, namespace: authorization_namespace, context: nil, allow_nil: false, default: default_authorization_policy_class, strict_namespace: authorization_strict_namespace) ⇒ Object
Returns policy instance for the record.
11 12 13 14 15 16 17 18 19 |
# File 'lib/action_policy/behaviours/policy_for.rb', line 11 def policy_for(record:, with: nil, namespace: , context: nil, allow_nil: false, default: , strict_namespace: ) context = context ? .merge(context) : policy_class = with || ::ActionPolicy.lookup( record, namespace:, context:, allow_nil:, default:, strict_namespace: ) policy_class&.new(record, **context) end |
#policy_for_cache_key(record:, with: nil, namespace: nil, context: authorization_context) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/action_policy/behaviours/policy_for.rb', line 62 def policy_for_cache_key(record:, with: nil, namespace: nil, context: , **) record_key = record._policy_cache_key(use_object_id: true) context_key = context.values.map { _1._policy_cache_key(use_object_id: true) }.join(".") "#{namespace}/#{with}/#{context_key}/#{record_key}" end |