Module: ActionPolicy::Policy::Core
Overview
Core policy API
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
-
#__apply__(rule) ⇒ Object
This method performs the rule call.
-
#allowed_to?(rule, record = :__undef__, **options) ⇒ Boolean
Returns a result of applying the specified rule to the specified record.
-
#apply(rule) ⇒ Object
Returns a result of applying the specified rule (true of false).
- #initialize(record = nil) ⇒ Object
-
#resolve_rule(activity) ⇒ Object
Returns a rule name (policy method name) for activity.
-
#with_clean_result ⇒ Object
Wrap code that could modify result to prevent the current result modification.
Methods included from Behaviours::PolicyFor
#authorization_context, #authorization_namespace, #policy_for
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
60 61 62 |
# File 'lib/action_policy/policy/core.rb', line 60 def record @record end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
60 61 62 |
# File 'lib/action_policy/policy/core.rb', line 60 def result @result end |
Class Method Details
.included(base) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/action_policy/policy/core.rb', line 28 def included(base) base.extend ClassMethods # Generate a new class for each _policy chain_ # in order to extend it independently base.module_eval do @result_class = Class.new(ExecutionResult) # we need to make this class _named_, # 'cause anonymous classes couldn't be marshalled base.const_set(:APR, @result_class) end end |
Instance Method Details
#__apply__(rule) ⇒ Object
This method performs the rule call. Override or extend it to provide custom functionality (such as caching, pre checks, etc.)
77 78 79 |
# File 'lib/action_policy/policy/core.rb', line 77 def __apply__(rule) public_send(rule) end |
#allowed_to?(rule, record = :__undef__, **options) ⇒ Boolean
Returns a result of applying the specified rule to the specified record. Under the hood a policy class for record is resolved (unless it’s explicitly set through ‘with` option).
If record is ‘nil` then we uses the current policy.
95 96 97 98 99 100 101 |
# File 'lib/action_policy/policy/core.rb', line 95 def allowed_to?(rule, record = :__undef__, **) if record == :__undef__ __apply__(rule) else policy_for(record: record, **).apply(rule) end end |
#apply(rule) ⇒ Object
Returns a result of applying the specified rule (true of false). Unlike simply calling a predicate rule (‘policy.manage?`), `apply` also calls pre-checks.
69 70 71 72 |
# File 'lib/action_policy/policy/core.rb', line 69 def apply(rule) @result = self.class.result_class.new @result.load __apply__(rule) end |
#initialize(record = nil) ⇒ Object
62 63 64 |
# File 'lib/action_policy/policy/core.rb', line 62 def initialize(record = nil) @record = record end |
#resolve_rule(activity) ⇒ Object
Returns a rule name (policy method name) for activity.
By default, rule name is equal to activity name.
Raises ActionPolicy::UknownRule when rule is not found in policy.
108 109 110 111 112 |
# File 'lib/action_policy/policy/core.rb', line 108 def resolve_rule(activity) raise UnknownRule.new(self, activity) unless respond_to?(activity) activity end |
#with_clean_result ⇒ Object
Wrap code that could modify result to prevent the current result modification
83 84 85 86 87 88 |
# File 'lib/action_policy/policy/core.rb', line 83 def with_clean_result was_result = @result res = yield @result = was_result res end |