Skip to content
Browse files

Fixes bug in mongoid_adapter with empty conditions hash

* adds mongoid query that matches every record when
rule.conditions.empty? is true
  • Loading branch information...
1 parent ff13a82 commit ad62d60b200c53ac167b01c7f2832849465799f8 Ryan Ahearn committed May 10, 2011
View
4 lib/cancan/model_adapters/mongoid_adapter.rb
@@ -23,14 +23,14 @@ def self.matches_conditions_hash?(subject, conditions)
end
def database_records
- if @rules.size == 0
+ if @rules.size == 0
@model_class.where(:_id => {'$exists' => false, '$type' => 7}) # return no records in Mongoid
elsif @rules.size == 1 && @rules[0].conditions.is_a?(Mongoid::Criteria)
@rules[0].conditions
else
@rules.inject(@model_class.all) do |records, rule|
if rule.conditions.empty?
- records
+ records.or(:_id => {'$exists' => true}) # match everything in Mongoid
elsif rule.base_behavior
records.or(rule.conditions)
else
View
9 spec/cancan/model_adapters/mongoid_adapter_spec.rb
@@ -68,6 +68,15 @@ class MongoidProject
MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
end
+ it "should be able to mix empty conditions and hashes" do
+ @ability.can :read, MongoidProject
+ @ability.can :read, MongoidProject, :title => 'Sir'
+ sir = MongoidProject.create(:title => 'Sir')
+ lord = MongoidProject.create(:title => 'Lord')
+
+ MongoidProject.accessible_by(@ability, :read).count.should == 2
+ end
+
it "should return everything when the defined ability is manage all" do
@ability.can :manage, :all
sir = MongoidProject.create(:title => 'Sir')

0 comments on commit ad62d60

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