Action Policy supports looking up policies based on the current execution namespace (the module/class where authorization is being called). This allows you to define specialized policies for specific contexts (like an Admin module) while falling back to a default policy if a namespaced one is not found.
Lookup Logic
When authorize! is called within a module, Action Policy performs a hierarchical search:
- It looks for a policy matching the current namespace (e.g.,
Admin::UserPolicy). - If not found, it traverses up the module nesting (e.g.,
Admin::Client::UserPolicy -> Admin::UserPolicy -> UserPolicy). - If no namespaced policy is found, it falls back to the base policy.
Requirements
Namespace support is an extension of ActionPolicy::Behaviour. It is included by default in Rails controllers and channel integrations via ActionPolicy::Behaviours::Namespaced.
module Admin
module Client
class UsersController < ApplicationController
def index
# lookup for Admin::Client::UserPolicy -> Admin::UserPolicy -> UserPolicy
authorize!
end
end
end
end