Skip to content
This repository
Browse code

Fixed bug where conditions on an optionally associated object would t…

…hrow exceptions if the associated object was not present at the rule match time.
  • Loading branch information...
commit 6aaab9e440848b05ccc457c2260f1597c37bc35b 1 parent 7bcfd3d
Mitch Williams thatothermitch authored

Showing 2 changed files with 8 additions and 1 deletion. Show diff stats Hide diff stats

  1. +1 1  lib/cancan/rule.rb
  2. +7 0 spec/cancan/ability_spec.rb
2  lib/cancan/rule.rb
@@ -109,7 +109,7 @@ def matches_conditions_hash?(subject, conditions = @conditions)
109 109 if attribute.kind_of? Array
110 110 attribute.any? { |element| matches_conditions_hash? element, value }
111 111 else
112   - matches_conditions_hash? attribute, value
  112 + !attribute.nil? && matches_conditions_hash?(attribute, value)
113 113 end
114 114 elsif value.kind_of?(Array) || value.kind_of?(Range)
115 115 value.include? attribute
7 spec/cancan/ability_spec.rb
@@ -249,6 +249,13 @@
249 249 @ability.can?(:read, 1..5).should be_true
250 250 @ability.can?(:read, 4..6).should be_false
251 251 end
  252 +
  253 + it "should not match subjects return nil for methods that must match nested a nested conditions hash" do
  254 + mock(object_with_foo = Object.new).foo { :bar }
  255 + @ability.can :read, Array, :first => { :foo => :bar }
  256 + @ability.can?(:read, [object_with_foo]).should be_true
  257 + @ability.can?(:read, []).should be_false
  258 + end
252 259
253 260 it "should not stop at cannot definition when comparing class" do
254 261 @ability.can :read, Range

0 comments on commit 6aaab9e

Please sign in to comment.
Something went wrong with that request. Please try again.