From 0de43c445b0b81bf59ca1737edf38f6f38e30621 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 8 Mar 2011 17:20:32 -0800 Subject: [PATCH] raise an error when trying to make a rule with both hash conditions and a block - closes #269 --- lib/cancan/rule.rb | 1 + spec/cancan/ability_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/cancan/rule.rb b/lib/cancan/rule.rb index 42bd478..f8e045a 100644 --- a/lib/cancan/rule.rb +++ b/lib/cancan/rule.rb @@ -11,6 +11,7 @@ module CanCan # and subject respectively (such as :read, @project). The third argument is a hash # of conditions and the last one is the block passed to the "can" call. def initialize(base_behavior, action, subject, conditions, block) + raise Error, "You are not able to supply a block with a hash of conditions in #{action} #{subject} ability. Use either one." if conditions.kind_of?(Hash) && !block.nil? @match_all = action.nil? && subject.nil? @base_behavior = base_behavior @actions = [action].flatten diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 5dd4e48..84e2216 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -357,6 +357,14 @@ describe CanCan::Ability do @ability.model_adapter(model_class, :read).should == :adapter_instance end + it "should raise an error when attempting to use a block with a hash condition since it's not likely what they want" do + lambda { + @ability.can :read, Array, :published => true do + false + end + }.should raise_error(CanCan::Error, "You are not able to supply a block with a hash of conditions in read Array ability. Use either one.") + end + describe "unauthorized message" do after(:each) do I18n.backend = nil