From 17c52a798309377d2f841dc4fa6d4a2e033b1c6c Mon Sep 17 00:00:00 2001 From: John Feminella Date: Wed, 27 Apr 2011 09:54:37 -0400 Subject: [PATCH] Augments Mongoid adapter by handling case where attribute is an array --- lib/cancan/model_adapters/mongoid_adapter.rb | 9 ++++++++- spec/cancan/model_adapters/mongoid_adapter_spec.rb | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/cancan/model_adapters/mongoid_adapter.rb b/lib/cancan/model_adapters/mongoid_adapter.rb index 25bb3f4..fb45c86 100644 --- a/lib/cancan/model_adapters/mongoid_adapter.rb +++ b/lib/cancan/model_adapters/mongoid_adapter.rb @@ -6,7 +6,14 @@ module CanCan end def self.override_conditions_hash_matching?(subject, conditions) - conditions.any? { |k,v| !k.kind_of?(Symbol) } + conditions.any? do |k,v| + key_is_not_symbol = lambda { !k.kind_of?(Symbol) } + subject_value_is_array = lambda do + subject.respond_to?(k) && subject.send(k).is_a?(Array) + end + + key_is_not_symbol.call || subject_value_is_array.call + end end def self.matches_conditions_hash?(subject, conditions) diff --git a/spec/cancan/model_adapters/mongoid_adapter_spec.rb b/spec/cancan/model_adapters/mongoid_adapter_spec.rb index 2bf4610..175a846 100644 --- a/spec/cancan/model_adapters/mongoid_adapter_spec.rb +++ b/spec/cancan/model_adapters/mongoid_adapter_spec.rb @@ -42,6 +42,15 @@ if ENV["MODEL_ADAPTER"] == "mongoid" @ability.should be_able_to(:read, model) end + it "should be able to read hashes when field is array" do + one_to_three = MongoidProject.create(:numbers => ['one', 'two', 'three']) + two_to_five = MongoidProject.create(:numbers => ['two', 'three', 'four', 'five']) + + @ability.can :foo, MongoidProject, :numbers => 'one' + @ability.should be_able_to(:foo, one_to_three) + @ability.should_not be_able_to(:foo, two_to_five) + end + it "should return [] when no ability is defined so no records are found" do MongoidProject.create(:title => 'Sir') MongoidProject.create(:title => 'Lord')