From 6aaab9e440848b05ccc457c2260f1597c37bc35b Mon Sep 17 00:00:00 2001 From: Mitch Williams Date: Fri, 1 Apr 2011 13:19:15 -0700 Subject: [PATCH] Fixed bug where conditions on an optionally associated object would throw exceptions if the associated object was not present at the rule match time. --- lib/cancan/rule.rb | 2 +- spec/cancan/ability_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/cancan/rule.rb b/lib/cancan/rule.rb index 2ab2b01..44c14af 100644 --- a/lib/cancan/rule.rb +++ b/lib/cancan/rule.rb @@ -109,7 +109,7 @@ module CanCan if attribute.kind_of? Array attribute.any? { |element| matches_conditions_hash? element, value } else - matches_conditions_hash? attribute, value + !attribute.nil? && matches_conditions_hash?(attribute, value) end elsif value.kind_of?(Array) || value.kind_of?(Range) value.include? attribute diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index c71f9fa..48b6782 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -249,6 +249,13 @@ describe CanCan::Ability do @ability.can?(:read, 1..5).should be_true @ability.can?(:read, 4..6).should be_false end + + it "should not match subjects return nil for methods that must match nested a nested conditions hash" do + mock(object_with_foo = Object.new).foo { :bar } + @ability.can :read, Array, :first => { :foo => :bar } + @ability.can?(:read, [object_with_foo]).should be_true + @ability.can?(:read, []).should be_false + end it "should not stop at cannot definition when comparing class" do @ability.can :read, Range