skip block when only class is passed to ability check, also don't pass class to block for :all - closes #116

This commit is contained in:
Ryan Bates 2010-09-02 14:23:00 -07:00
parent 04b523eea4
commit 6105edc6a7
2 changed files with 9 additions and 13 deletions

View File

@ -25,7 +25,7 @@ module CanCan
# Matches the block or conditions hash # Matches the block or conditions hash
def matches_conditions?(action, subject, extra_args) def matches_conditions?(action, subject, extra_args)
if @block if @block && subject.class != Class
call_block(action, subject, extra_args) call_block(action, subject, extra_args)
elsif @conditions.kind_of?(Hash) && subject.class != Class elsif @conditions.kind_of?(Hash) && subject.class != Class
matches_conditions_hash?(subject) matches_conditions_hash?(subject)
@ -95,7 +95,6 @@ module CanCan
def call_block(action, subject, extra_args) def call_block(action, subject, extra_args)
block_args = [] block_args = []
block_args << action if @expanded_actions.include?(:manage) block_args << action if @expanded_actions.include?(:manage)
block_args << (subject.class == Class ? subject : subject.class) if @subjects.include?(:all)
block_args << (subject.class == Class ? nil : subject) block_args << (subject.class == Class ? nil : subject)
block_args += extra_args block_args += extra_args
@block.call(*block_args) @block.call(*block_args)

View File

@ -38,9 +38,8 @@ describe CanCan::Ability do
@ability.can?(:read, 6).should be_false @ability.can?(:read, 6).should be_false
end end
it "should pass class with object if :all objects are accepted" do it "should not pass class with object if :all objects are accepted" do
@ability.can :preview, :all do |object_class, object| @ability.can :preview, :all do |object|
object_class.should == Fixnum
object.should == 123 object.should == 123
@block_called = true @block_called = true
end end
@ -48,14 +47,13 @@ describe CanCan::Ability do
@block_called.should be_true @block_called.should be_true
end end
it "should pass class with no object if :all objects are accepted and class is passed directly" do it "should not call block when only class is passed, only return true" do
@ability.can :preview, :all do |object_class, object| @block_called = false
object_class.should == Hash @ability.can :preview, :all do |object|
object.should be_nil
@block_called = true @block_called = true
end end
@ability.can?(:preview, Hash) @ability.can?(:preview, Hash).should be_true
@block_called.should be_true @block_called.should be_false
end end
it "should pass action and object for global manage actions" do it "should pass action and object for global manage actions" do
@ -83,9 +81,8 @@ 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.can :manage, :all do |action, object_class, object| @ability.can :manage, :all do |action, object|
action.should == :foo action.should == :foo
object_class.should == Fixnum
object.should == 123 object.should == 123
@block_called = true @block_called = true
end end