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
Classes: Check
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
139
140
141
|
# File 'lib/action_policy/policy/pre_check.rb', line 139
def included(base)
base.extend ClassMethods
end
|
Instance Method Details
#__apply__(rule) ⇒ Object
162
163
164
|
# File 'lib/action_policy/policy/pre_check.rb', line 162
def __apply__(rule)
run_pre_checks(rule) { super }
end
|
#allow! ⇒ Object
158
159
160
|
# File 'lib/action_policy/policy/pre_check.rb', line 158
def allow!
Check::Result::ALLOW
end
|
#deny! ⇒ Object
154
155
156
|
# File 'lib/action_policy/policy/pre_check.rb', line 154
def deny!
Check::Result::DENY
end
|
#run_pre_checks(rule) ⇒ Object
144
145
146
147
148
149
150
151
152
|
# File 'lib/action_policy/policy/pre_check.rb', line 144
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
|