Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Add namespace for cancan authorization library
Ruby
branch: master
Failed to load latest commit information.
lib Pass content directly
MIT-LICENSE Initial commit
README.rdoc Pass content directly
Rakefile Update to cancan 1.6.3
cancan_namespace.gemspec Pass content directly
init.rb Initial commit

README.rdoc

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

Something went wrong with that request. Please try again.