Module: ActionPolicy::Behaviours::Namespaced

Included in:
Channel, Controller
Defined in:
lib/action_policy/behaviours/namespaced.rb

Overview

Adds an ability to lookup policies from current context (namespace):

module Admin
class UsersController < ApplictionController
  def index
    # uses Admin::UserPolicy if any, otherwise fallbacks to UserPolicy
    authorize!
  end
end
end

Modules nesting is also supported:

module Admin
module Client
  class UsersController < ApplictionController
    def index
      # lookup for Admin::Client::UserPolicy -> Admin::UserPolicy -> UserPolicy
      authorize!
    end
  end
end
end

NOTE: in order to support namespaced lookup for non-inferrable resources, you should specify policy_name at a class level (instead of policy_class, which doesn't take into account namespaces):

class Guest < User def self.policy_name "UserPolicy" end end

NOTE: by default, we use class's name as a policy name; so, for namespaced resources the namespace part is also included:

class Admin class User end end

search for Admin::UserPolicy, but not for UserPolicy

authorize! Admin::User.new

You can access the current authorization namespace through authorization_namespace method.

You can also define your own namespacing logic by overriding authorization_namespace:

def authorization_namespace
return ::Admin if current_user.admin?
return ::Staff if current_user.staff?
# fallback to current namespace
super
end

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object Also known as: included



65
66
67
# File 'lib/action_policy/behaviours/namespaced.rb', line 65

def prepended(base)
  base.prepend InstanceMethods
end