Action Policy tries to automatically infer policy class from the target using the following probes:
-
If the target is a
Symbol
:a) Try
"#{target.to_s.camelize}Policy"
as apolicy_name
(see below);b) If
String#classify
is available, e.g. when using Rails' ActiveSupport, try"#{target.to_s.classify}Policy"
; -
If the target responds to
policy_class
, then use it; -
If the target's class responds to
policy_class
, then use it; -
If the target or the target's class responds to
policy_name
, then use it (thepolicy_name
should end withPolicy
as it's not appended automatically); -
Otherwise, use
#{target.class.name}Policy
or#{target.name}Policy
if target is a Module or Class.
* Namespaces could be also be considered when
namespace
option is set. You can also set thestrict_namespace
option to disable constant cascade lookup.
You can call ActionPolicy.lookup(record, options)
to infer policy class for the record.
When no policy class is found, an ActionPolicy::NotFound
error is raised.
You can customize lookup logic if necessary.