From 1edf583110e26c955072f9e638ded989f760a1df Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 17 Nov 2009 10:25:47 -0800 Subject: [PATCH] BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' to set up abilities - closes #4 --- CHANGELOG.rdoc | 2 ++ README.rdoc | 18 ++++++++---------- lib/cancan/ability.rb | 4 ---- lib/cancan/controller_additions.rb | 6 ++---- spec/cancan/ability_spec.rb | 4 ++-- spec/cancan/controller_additions_spec.rb | 3 +++ 6 files changed, 17 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 170d065..6c5611b 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,5 @@ +* BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' to set up abilities - see issue #4 + *0.1.0* (Nov 16th, 2009) * initial release diff --git a/README.rdoc b/README.rdoc index b813bcb..53edaf1 100644 --- a/README.rdoc +++ b/README.rdoc @@ -2,7 +2,7 @@ This is a simple authorization solution for Rails which is completely decoupled from how you set up the user's roles. All permissions are stored in a single location for convenience. -This assumes you already have an authentication solution (such as Authlogic) which proves a current_user model. +This assumes you already have an authentication solution (such as Authlogic) which provides a current_user model. == Installation @@ -27,7 +27,7 @@ First define a class called Ability, place it in "models/ability.rb". class Ability include CanCan::Ability - def prepare(user) + def initialize(user) if user.admin? can :manage, :all else @@ -77,7 +77,7 @@ If the user authorization fails, a CanCan::AccessDenied exception will be raised == Defining Abilities -As shown above, the Ability#prepare method is where all user permissions are defined. The user model is passed into this method so you are free to modify the permissions based on the user's attributes. This way CanCan is completely decoupled with how you choose to handle roles. +As shown above, the Ability#initialize method is where all user permissions are defined. The user model is passed into this method so you are free to modify the permissions based on the user's attributes. This way CanCan is completely decoupled with how you choose to handle roles. The "can" method accepts two arguments, the first one is the action you're setting the permission for, the second one is the class of object you're setting it on. @@ -153,19 +153,17 @@ There is no limit to what actions you can use to determine abilities. For exampl end -== Customizing Assumptions +== Assumptions & Configuring CanCan makes two assumptions about your application. -* The permissions are defined in Ability#prepare. -* The user is fetched with current_user method in the controller. +* The permissions are defined in Ability#initialize. +* The user is fetched with the current_user method in the controller. You can override these by defining the "current_ability" method in your ApplicationController. def current_ability - ability = UserAbility.new # instead of Ability - ability.prepare(current_account) # instead of current_user - ability # be sure to return the ability + UserAbility.new(current_account) # instead of Ability.new(current_user) end That's it! @@ -180,7 +178,7 @@ For example, let's assume that each user has_many :permissions, and each permiss class Ability include CanCan::Ability - def prepare(user) + def initialize(user) can :manage, :all do |action, object_class, object| user.permissions.find_all_by_action(action).any? do |permission| permission.object_type.constantize == object_class && diff --git a/lib/cancan/ability.rb b/lib/cancan/ability.rb index 1e6a76c..410f294 100644 --- a/lib/cancan/ability.rb +++ b/lib/cancan/ability.rb @@ -47,9 +47,5 @@ module CanCan :update => [:edit], } end - - def prepare(user) - # to be overriden by included class - end end end diff --git a/lib/cancan/controller_additions.rb b/lib/cancan/controller_additions.rb index b1a6f99..4459e9c 100644 --- a/lib/cancan/controller_additions.rb +++ b/lib/cancan/controller_additions.rb @@ -9,9 +9,7 @@ module CanCan end def current_ability - ability = ::Ability.new - ability.prepare(current_user) - ability + ::Ability.new(current_user) end def can?(*args) @@ -43,4 +41,4 @@ if defined? ActionController ActionController::Base.class_eval do include CanCan::ControllerAdditions end -end \ No newline at end of file +end diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 614d16d..a2b172d 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -78,7 +78,7 @@ describe CanCan::Ability do @ability.can?(:edit, 123).should == :update_called end - it "should respond to prepare" do - @ability.should respond_to(:prepare) + it "should not respond to prepare (now using initialize)" do + @ability.should_not respond_to(:prepare) end end diff --git a/spec/cancan/controller_additions_spec.rb b/spec/cancan/controller_additions_spec.rb index 0b5d06e..e6de319 100644 --- a/spec/cancan/controller_additions_spec.rb +++ b/spec/cancan/controller_additions_spec.rb @@ -2,6 +2,9 @@ require File.dirname(__FILE__) + '/../spec_helper' class Ability include CanCan::Ability + + def initialize(user) + end end describe CanCan::ControllerAdditions do