adding manage action which applies to everything
This commit is contained in:
parent
0cfb8c7c41
commit
6c89c32059
|
@ -6,21 +6,15 @@ module CanCan
|
|||
|
||||
def can?(action, target)
|
||||
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?
|
||||
return true
|
||||
else
|
||||
if can_target == :all
|
||||
if target.class == Class
|
||||
return can_block.call(target, nil)
|
||||
else
|
||||
return can_block.call(target.class, target)
|
||||
end
|
||||
elsif can_target == target
|
||||
return can_block.call(nil)
|
||||
else
|
||||
return can_block.call(target)
|
||||
end
|
||||
block_args = []
|
||||
block_args << action if can_action == :manage
|
||||
block_args << (target.class == Class ? target : target.class) if can_target == :all
|
||||
block_args << (target.class == Class ? nil : target)
|
||||
return can_block.call(*block_args)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,9 +9,19 @@ class Ability
|
|||
can :preview, :all do |object_class, object|
|
||||
[object_class, object]
|
||||
end
|
||||
can :manage, Array do |action, object|
|
||||
[action, object]
|
||||
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
|
||||
@ability = Ability.new
|
||||
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
|
||||
@ability.can?(:preview, Hash).should == [Hash, nil]
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user