moving can definition into ability instance instead of class, this removes ugly instance_exec command

This commit is contained in:
Ryan Bates 2009-11-16 19:59:40 -08:00
parent 7b299b50fc
commit 4b6f538663
6 changed files with 41 additions and 65 deletions

View File

@ -2,6 +2,5 @@ module CanCan
class AccessDenied < StandardError; end class AccessDenied < StandardError; end
end end
require File.dirname(__FILE__) + '/cancan/instance_exec'
require File.dirname(__FILE__) + '/cancan/ability' require File.dirname(__FILE__) + '/cancan/ability'
require File.dirname(__FILE__) + '/cancan/controller_additions' require File.dirname(__FILE__) + '/cancan/controller_additions'

View File

@ -2,15 +2,8 @@ module CanCan
module Ability module Ability
attr_accessor :user attr_accessor :user
def self.included(base)
base.extend ClassMethods
base.alias_action :index, :show, :to => :read
base.alias_action :new, :to => :create
base.alias_action :edit, :to => :update
end
def can?(original_action, target) # TODO this could use some refactoring def can?(original_action, target) # TODO this could use some refactoring
(self.class.can_history || []).reverse.each do |can_action, can_target, can_block| (@can_history || []).reverse.each do |can_action, can_target, can_block|
possible_actions_for(original_action).each do |action| possible_actions_for(original_action).each do |action|
if (can_action == :manage || can_action == action) && (can_target == :all || can_target == target || target.kind_of?(can_target)) if (can_action == :manage || can_action == action) && (can_target == :all || can_target == target || target.kind_of?(can_target))
if can_block.nil? if can_block.nil?
@ -20,7 +13,7 @@ module CanCan
block_args << action if can_action == :manage block_args << action if can_action == :manage
block_args << (target.class == Class ? target : target.class) if can_target == :all block_args << (target.class == Class ? target : target.class) if can_target == :all
block_args << (target.class == Class ? nil : target) block_args << (target.class == Class ? nil : target)
return instance_exec(*block_args, &can_block) return can_block.call(*block_args)
end end
end end
end end
@ -30,32 +23,33 @@ module CanCan
def possible_actions_for(initial_action) def possible_actions_for(initial_action)
actions = [initial_action] actions = [initial_action]
(self.class.aliased_actions || []).each do |target, aliases| (@aliased_actions || default_alias_actions).each do |target, aliases|
actions += possible_actions_for(target) if aliases.include? initial_action actions += possible_actions_for(target) if aliases.include? initial_action
end end
actions actions
end end
module ClassMethods
attr_reader :can_history
attr_reader :aliased_actions
def can(action, target, &block) def can(action, target, &block)
@can_history ||= [] @can_history ||= []
@can_history << [action, target, block] @can_history << [action, target, block]
end end
def alias_action(*args) def alias_action(*args)
@aliased_actions ||= {} @aliased_actions ||= default_alias_actions
target = args.pop[:to] target = args.pop[:to]
@aliased_actions[target] = args @aliased_actions[target] = args
end end
def for_user(user) def default_alias_actions
ability = new {
ability.user = user :read => [:index, :show],
ability :create => [:new],
:update => [:edit],
}
end end
def prepare(user)
# to be overriden by included class
end end
end end
end end

View File

@ -9,7 +9,9 @@ module CanCan
end end
def current_ability def current_ability
::Ability.for_user(current_user) ability = ::Ability.new
ability.prepare(current_user)
ability
end end
def can?(*args) def can?(*args)

View File

@ -1,15 +0,0 @@
# see http://eigenclass.org/hiki.rb?instance_exec
class Object
module InstanceExecHelper; end
include InstanceExecHelper
def instance_exec(*args, &block) # !> method redefined; discarding old instance_exec
mname = "__instance_exec_#{Thread.current.object_id.abs}_#{object_id.abs}"
InstanceExecHelper.module_eval{ define_method(mname, &block) }
begin
ret = send(mname, *args)
ensure
InstanceExecHelper.module_eval{ undef_method(mname) } rescue nil
end
ret
end
end

View File

@ -8,7 +8,7 @@ describe CanCan::Ability do
end end
it "should be able to :read anything" do it "should be able to :read anything" do
@ability_class.can :read, :all @ability.can :read, :all
@ability.can?(:read, String).should be_true @ability.can?(:read, String).should be_true
@ability.can?(:read, 123).should be_true @ability.can?(:read, 123).should be_true
end end
@ -18,8 +18,8 @@ describe CanCan::Ability do
end end
it "should return what block returns on a can call" do it "should return what block returns on a can call" do
@ability_class.can :read, :all @ability.can :read, :all
@ability_class.can :read, Symbol do |sym| @ability.can :read, Symbol do |sym|
sym sym
end end
@ability.can?(:read, Symbol).should be_nil @ability.can?(:read, Symbol).should be_nil
@ -27,21 +27,21 @@ describe CanCan::Ability do
end end
it "should pass class with object if :all objects are accepted" do it "should pass class with object if :all objects are accepted" do
@ability_class.can :preview, :all do |object_class, object| @ability.can :preview, :all do |object_class, object|
[object_class, object] [object_class, object]
end end
@ability.can?(:preview, 123).should == [Fixnum, 123] @ability.can?(:preview, 123).should == [Fixnum, 123]
end end
it "should pass class with no object if :all objects are accepted and class is passed directly" do it "should pass class with no object if :all objects are accepted and class is passed directly" do
@ability_class.can :preview, :all do |object_class, object| @ability.can :preview, :all do |object_class, object|
[object_class, object] [object_class, object]
end end
@ability.can?(:preview, Hash).should == [Hash, nil] @ability.can?(:preview, Hash).should == [Hash, nil]
end end
it "should pass action and object for global manage actions" do it "should pass action and object for global manage actions" do
@ability_class.can :manage, Array do |action, object| @ability.can :manage, Array do |action, object|
[action, object] [action, object]
end end
@ability.can?(:stuff, [1, 2]).should == [:stuff, [1, 2]] @ability.can?(:stuff, [1, 2]).should == [:stuff, [1, 2]]
@ -49,8 +49,8 @@ describe CanCan::Ability do
end end
it "should alias update or destroy actions to modify action" do it "should alias update or destroy actions to modify action" do
@ability_class.alias_action :update, :destroy, :to => :modify @ability.alias_action :update, :destroy, :to => :modify
@ability_class.can :modify, :all do |object_class, object| @ability.can :modify, :all do |object_class, object|
:modify_called :modify_called
end end
@ability.can?(:update, 123).should == :modify_called @ability.can?(:update, 123).should == :modify_called
@ -58,7 +58,7 @@ describe CanCan::Ability do
end end
it "should return block result for action, object_class, and object for any action" do it "should return block result for action, object_class, and object for any action" do
@ability_class.can :manage, :all do |action, object_class, object| @ability.can :manage, :all do |action, object_class, object|
[action, object_class, object] [action, object_class, object]
end end
@ability.can?(:foo, 123).should == [:foo, Fixnum, 123] @ability.can?(:foo, 123).should == [:foo, Fixnum, 123]
@ -66,22 +66,19 @@ describe CanCan::Ability do
end end
it "should automatically alias index and show into read calls" do it "should automatically alias index and show into read calls" do
@ability_class.can :read, :all @ability.can :read, :all
@ability.can?(:index, 123).should be_true @ability.can?(:index, 123).should be_true
@ability.can?(:show, 123).should be_true @ability.can?(:show, 123).should be_true
end end
it "should automatically alias new and edit into create and update respectively" do it "should automatically alias new and edit into create and update respectively" do
@ability_class.can(:create, :all) { :create_called } @ability.can(:create, :all) { :create_called }
@ability_class.can(:update, :all) { :update_called } @ability.can(:update, :all) { :update_called }
@ability.can?(:new, 123).should == :create_called @ability.can?(:new, 123).should == :create_called
@ability.can?(:edit, 123).should == :update_called @ability.can?(:edit, 123).should == :update_called
end end
it "should be able to access given user" do it "should respond to prepare" do
@ability_class.can(:preview, :all) { user } @ability.should respond_to(:prepare)
ability = @ability_class.for_user(:some_user)
ability.user.should == :some_user
ability.can?(:preview, 123).should == :some_user
end end
end end

View File

@ -21,7 +21,6 @@ describe CanCan::ControllerAdditions do
it "should have a current_ability method which generates an ability for the current user" do it "should have a current_ability method which generates an ability for the current user" do
stub(@controller).current_user { :current_user } stub(@controller).current_user { :current_user }
@controller.current_ability.should be_kind_of(Ability) @controller.current_ability.should be_kind_of(Ability)
@controller.current_ability.user.should == :current_user
end end
it "should provide a can? method which goes through the current ability" do it "should provide a can? method which goes through the current ability" do