CancanNamespace

Namespace for cancan gem.

Install

gem 'cancan_namespace'

Usage

class Ability
  include CanCanNamespace::Ability

  def initialize(user, context = nil)
    @context = context

    ...

    if user.admin?
      can :manage, :all
      can :manage, :all, :context => :manage
    end
  end
end

Controller

Manage::BaseController < ApplicationController
  protected

    def current_ability
      @current_ability ||= ::Ability.new(current_user, :manage)
    end
end

In this case context extracted from controller name (:manage):

class Manage::PostsController < Manage::BaseController
  before_filter :find_post, :only => [:edit, :update, :destroy]
  authorize_resource
  ...
end

Set context for controller directly:

class People::RelationshipsController < Account::BaseController
  before_filter :find_followed
  before_filter :build_relation, :only => [:create]
  before_filter :find_relationship, :only => [:destroy]

  authorize_resource :relationship, :context => :account

  ...

end

View

<% if can? :edit, post %>
  <%= link_to 'Edit', edit_post_path(post) %>
<% end %>

<% if can? :edit, post, :context => :manage %>
  <%= link_to 'Edit (admin)', edit_manage_post_path(post) %>
<% end %>

Copyright © 2011 Aimbulance, released under the MIT license