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
This commit is contained in:
Ryan Ahearn 2011-05-12 09:24:38 -04:00
parent ad62d60b20
commit 0882450232

View File

@ -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.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