adding manage action which applies to everything

This commit is contained in:
Ryan Bates 2009-11-16 14:42:41 -08:00
parent 0cfb8c7c41
commit 6c89c32059
2 changed files with 30 additions and 13 deletions

View File

@ -6,21 +6,15 @@ module CanCan
def can?(action, target) def can?(action, target)
self.class.can_history.reverse.each do |can_action, can_target, can_block| self.class.can_history.reverse.each do |can_action, can_target, can_block|
if 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?
return true return true
else else
if can_target == :all block_args = []
if target.class == Class block_args << action if can_action == :manage
return can_block.call(target, nil) block_args << (target.class == Class ? target : target.class) if can_target == :all
else block_args << (target.class == Class ? nil : target)
return can_block.call(target.class, target) return can_block.call(*block_args)
end
elsif can_target == target
return can_block.call(nil)
else
return can_block.call(target)
end
end end
end end
end end

View File

@ -9,9 +9,19 @@ class Ability
can :preview, :all do |object_class, object| can :preview, :all do |object_class, object|
[object_class, object] [object_class, object]
end end
can :manage, Array do |action, object|
[action, object]
end
end end
describe CanCan::Ability do class AdminAbility
include CanCan::Ability
can :manage, :all do |action, object_class, object|
[action, object_class, object]
end
end
describe Ability do
before(:each) do before(:each) do
@ability = Ability.new @ability = Ability.new
end end
@ -37,4 +47,17 @@ describe CanCan::Ability do
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.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
@ability.can?(:stuff, [1, 2]).should == [:stuff, [1, 2]]
@ability.can?(:stuff, Array).should == [:stuff, nil]
end
end
describe AdminAbility do
it "should return block result for action, object_class, and object for any action" do
@ability = AdminAbility.new
@ability.can?(:foo, 123).should == [:foo, Fixnum, 123]
@ability.can?(:bar, Fixnum).should == [:bar, Fixnum, nil]
end
end end