Skip to content
Browse files

Augments Mongoid adapter by handling case where attribute is an array

  • Loading branch information...
1 parent 18c1007 commit 17c52a798309377d2f841dc4fa6d4a2e033b1c6c John Feminella committed Apr 27, 2011
View
9 lib/cancan/model_adapters/mongoid_adapter.rb
@@ -6,7 +6,14 @@ def self.for_class?(model_class)
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)
View
9 spec/cancan/model_adapters/mongoid_adapter_spec.rb
@@ -42,6 +42,15 @@ class MongoidProject
@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')

0 comments on commit 17c52a7

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