Module: ActionPolicy::Policy::Reasons
- Included in:
- Base
- Defined in:
- lib/action_policy/policy/reasons.rb
Overview
Provides failure reasons tracking functionality. That allows you to distinguish between the reasons why authorization was rejected.
It’s helpful when you compose policies (i.e. use one policy within another).
For example:
class ApplicantPolicy < ApplicationPolicy
def show?
user.(:view_applicants) &&
allowed_to?(:show?, object.stage)
end
end
Now when you receive an exception, you have a reasons object, which contains additional information about the failure:
rescue_from ActionPolicy::Unauthorized do |ex|
ex.reasons. #=> { stage: [:show] }
end
You can also wrap local rules into ‘allowed_to?` to populate reasons:
class ApplicantPolicy < ApplicationPolicy
def show?
allowed_to?(:view_applicants?) &&
allowed_to?(:show?, object.stage)
end
def view_applicants?
user.(:view_applicants)
end
end
Defined Under Namespace
Modules: InstanceMethods
Instance Attribute Summary collapse
-
#reasons ⇒ Object
readonly
Returns the value of attribute reasons.
Class Method Summary collapse
- .prepended(base) ⇒ Object (also: included)
Instance Method Summary collapse
-
#with_clean_reasons ⇒ Object
:nodoc:.
Instance Attribute Details
#reasons ⇒ Object (readonly)
Returns the value of attribute reasons.
72 73 74 |
# File 'lib/action_policy/policy/reasons.rb', line 72 def reasons @reasons end |
Class Method Details
.prepended(base) ⇒ Object Also known as: included
65 66 67 |
# File 'lib/action_policy/policy/reasons.rb', line 65 def prepended(base) base.prepend InstanceMethods end |
Instance Method Details
#with_clean_reasons ⇒ Object
:nodoc:
74 75 76 77 78 79 80 |
# File 'lib/action_policy/policy/reasons.rb', line 74 def with_clean_reasons # :nodoc: old_reasons = reasons @reasons = nil res = yield @reasons = old_reasons res end |