Merge pull request #363 from rahearn/mongoid-conditions-empty
Fixes bug in mongoid_adapter with empty conditions hash
This commit is contained in:
commit
74c9d582b2
|
@ -28,13 +28,17 @@ module CanCan
|
|||
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
|
||||
elsif rule.base_behavior
|
||||
records.or(rule.conditions)
|
||||
# we only need to process can rules if
|
||||
# there are no rules with empty conditions
|
||||
rules = @rules.reject { |rule| rule.conditions.empty? }
|
||||
process_can_rules = @rules.count == rules.count
|
||||
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
|
||||
records.excludes(rule.conditions)
|
||||
records
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -68,6 +68,15 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
|
|||
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')
|
||||
|
|
Loading…
Reference in New Issue
Block a user