From 6105edc6a77ffa59d87e00becc1de6d5b0ecd885 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Thu, 2 Sep 2010 14:23:00 -0700 Subject: [PATCH] skip block when only class is passed to ability check, also don't pass class to block for :all - closes #116 --- lib/cancan/can_definition.rb | 3 +-- spec/cancan/ability_spec.rb | 19 ++++++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/cancan/can_definition.rb b/lib/cancan/can_definition.rb index 876f388..ed1dfa9 100644 --- a/lib/cancan/can_definition.rb +++ b/lib/cancan/can_definition.rb @@ -25,7 +25,7 @@ module CanCan # Matches the block or conditions hash def matches_conditions?(action, subject, extra_args) - if @block + if @block && subject.class != Class call_block(action, subject, extra_args) elsif @conditions.kind_of?(Hash) && subject.class != Class matches_conditions_hash?(subject) @@ -95,7 +95,6 @@ module CanCan def call_block(action, subject, extra_args) block_args = [] 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 += extra_args @block.call(*block_args) diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 51ea9b7..ac3e03a 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -38,9 +38,8 @@ describe CanCan::Ability do @ability.can?(:read, 6).should be_false end - it "should pass class with object if :all objects are accepted" do - @ability.can :preview, :all do |object_class, object| - object_class.should == Fixnum + it "should not pass class with object if :all objects are accepted" do + @ability.can :preview, :all do |object| object.should == 123 @block_called = true end @@ -48,14 +47,13 @@ describe CanCan::Ability do @block_called.should be_true end - it "should pass class with no object if :all objects are accepted and class is passed directly" do - @ability.can :preview, :all do |object_class, object| - object_class.should == Hash - object.should be_nil + it "should not call block when only class is passed, only return true" do + @block_called = false + @ability.can :preview, :all do |object| @block_called = true end - @ability.can?(:preview, Hash) - @block_called.should be_true + @ability.can?(:preview, Hash).should be_true + @block_called.should be_false end it "should pass action and object for global manage actions" do @@ -83,9 +81,8 @@ describe CanCan::Ability do end 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 - object_class.should == Fixnum object.should == 123 @block_called = true end