Fixes bug in mongoid_adapter with empty conditions hash

* adds mongoid query that matches every record when
rule.conditions.empty? is true
This commit is contained in:
Ryan Ahearn 2011-05-10 11:52:29 -04:00
parent ff13a82dda
commit ad62d60b20
2 changed files with 11 additions and 2 deletions

View File

@ -30,7 +30,7 @@ module CanCan
else else
@rules.inject(@model_class.all) do |records, rule| @rules.inject(@model_class.all) do |records, rule|
if rule.conditions.empty? if rule.conditions.empty?
records records.or(:_id => {'$exists' => true}) # match everything in Mongoid
elsif rule.base_behavior elsif rule.base_behavior
records.or(rule.conditions) records.or(rule.conditions)
else else

View File

@ -68,6 +68,15 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
MongoidProject.accessible_by(@ability, :read).entries.should == [sir] MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
end 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 it "should return everything when the defined ability is manage all" do
@ability.can :manage, :all @ability.can :manage, :all
sir = MongoidProject.create(:title => 'Sir') sir = MongoidProject.create(:title => 'Sir')