Module: ActionPolicy::Policy::PreCheck
- Included in:
- Base
- Defined in:
- lib/action_policy/policy/pre_check.rb
Overview
Adds callback-style checks to policies to extract common checks from rules.
class ApplicationPolicy < ActionPolicy::Base
authorize :user
pre_check :allow_admins
private
def allow_admins
allow! if user.admin?
end
end
You can specify conditional pre-checks (through ‘except` / `only`) options and skip already defined pre-checks if necessary.
class UserPolicy < ApplicationPolicy
skip_pre_check :allow_admins, only: :destroy?
def destroy?
user.admin? && !record.admin?
end
end
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Classes: Check
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.prepended(base) ⇒ Object
Also known as:
included
139
140
141
142
|
# File 'lib/action_policy/policy/pre_check.rb', line 139
def prepended(base)
base.extend ClassMethods
base.prepend InstanceMethods
end
|
Instance Method Details
#allow! ⇒ Object
161
162
163
|
# File 'lib/action_policy/policy/pre_check.rb', line 161
def allow!
Check::Result::ALLOW
end
|
#deny! ⇒ Object
157
158
159
|
# File 'lib/action_policy/policy/pre_check.rb', line 157
def deny!
Check::Result::DENY
end
|
#run_pre_checks(rule) ⇒ Object
147
148
149
150
151
152
153
154
155
|
# File 'lib/action_policy/policy/pre_check.rb', line 147
def run_pre_checks(rule)
self.class.pre_checks.each do |check|
next unless check.applicable?(rule)
res = check.call(self)
return res.value if res.fulfilled?
end
yield if block_given?
end
|