From 06296b0a40a1ce7e575441d113504b4ae0702fdc Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Thu, 22 Apr 2010 17:39:22 -0700 Subject: [PATCH] support has_many association or arrays in can conditions hash --- CHANGELOG.rdoc | 2 ++ lib/cancan/can_definition.rb | 6 +++++- spec/cancan/ability_spec.rb | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 080bcd1..6e1738c 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,5 @@ +* Support has_many association or arrays in can conditions hash + * Adding joins clause to accessible_by when conditions are across associations 1.1.1 (April 17, 2010) diff --git a/lib/cancan/can_definition.rb b/lib/cancan/can_definition.rb index 8e65569..9b3b945 100644 --- a/lib/cancan/can_definition.rb +++ b/lib/cancan/can_definition.rb @@ -65,7 +65,11 @@ module CanCan conditions.all? do |name, value| attribute = subject.send(name) if value.kind_of?(Hash) - matches_conditions? attribute, value + if attribute.kind_of? Array + attribute.any? { |element| matches_conditions? element, value } + else + matches_conditions? attribute, value + end elsif value.kind_of?(Array) || value.kind_of?(Range) value.include? attribute else diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 4aaa3ba..169c040 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -168,6 +168,12 @@ describe CanCan::Ability do @ability.can?(:read, ["test1", "foo"]).should be_true end + it "should allow nested hash of arrays and match any element" do + @ability.can :read, Array, :first => { :to_i => 3 } + @ability.can?(:read, [[1, 2, 3]]).should be_true + @ability.can?(:read, [[4, 5, 6]]).should be_false + end + it "should return conditions for a given ability" do @ability.can :read, Array, :first => 1, :last => 3 @ability.conditions(:show, Array).should == {:first => 1, :last => 3}