Merge pull request #363 from rahearn/mongoid-conditions-empty

Fixes bug in mongoid_adapter with empty conditions hash
This commit is contained in:
Ryan Bates 2011-05-17 10:22:19 -07:00
commit 74c9d582b2
2 changed files with 20 additions and 7 deletions

View File

@ -23,18 +23,22 @@ module CanCan
end end
def database_records def database_records
if @rules.size == 0 if @rules.size == 0
@model_class.where(:_id => {'$exists' => false, '$type' => 7}) # return no records in Mongoid @model_class.where(:_id => {'$exists' => false, '$type' => 7}) # return no records in Mongoid
elsif @rules.size == 1 && @rules[0].conditions.is_a?(Mongoid::Criteria) elsif @rules.size == 1 && @rules[0].conditions.is_a?(Mongoid::Criteria)
@rules[0].conditions @rules[0].conditions
else else
@rules.inject(@model_class.all) do |records, rule| # we only need to process can rules if
if rule.conditions.empty? # there are no rules with empty conditions
records rules = @rules.reject { |rule| rule.conditions.empty? }
elsif rule.base_behavior process_can_rules = @rules.count == rules.count
records.or(rule.conditions) rules.inject(@model_class.all) do |records, rule|
if process_can_rules && rule.base_behavior
records.or rule.conditions
elsif !rule.base_behavior
records.excludes rule.conditions
else else
records.excludes(rule.conditions) records
end end
end end
end end

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')