Skip to content
Browse files

Processes can rules only if no empty conditions rules are present

1) remove all empty conditions hashes from the rules, they are included
 in the records through `@model_class.all`
2) only process can rules if the new and old rules lists are the same
  length (meaning there were no empty conditions hashes)
3) always process cannot rules
  • Loading branch information...
1 parent ad62d60 commit 08824502326b7c050dd8759db3850433e4e8c556 Ryan Ahearn committed May 12, 2011
Showing with 10 additions and 6 deletions.
  1. +10 −6 lib/cancan/model_adapters/mongoid_adapter.rb
View
16 lib/cancan/model_adapters/mongoid_adapter.rb
@@ -28,13 +28,17 @@ def database_records
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.or(:_id => {'$exists' => true}) # match everything in Mongoid
- 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

0 comments on commit 0882450

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