diff options
Diffstat (limited to 'activerecord/lib/active_record/relation/predicate_builder/array_handler.rb')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder/array_handler.rb | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb index 2f6c34ac08..b8f3285c3e 100644 --- a/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb +++ b/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb @@ -1,29 +1,47 @@ +require 'active_support/core_ext/string/filters' + module ActiveRecord class PredicateBuilder class ArrayHandler # :nodoc: def call(attribute, value) values = value.map { |x| x.is_a?(Base) ? x.id : x } - ranges, values = values.partition { |v| v.is_a?(Range) } + nils, values = values.partition(&:nil?) + + if values.any? { |val| val.is_a?(Array) } + ActiveSupport::Deprecation.warn(<<-MSG.squish) + Passing a nested array to Active Record finder methods is + deprecated and will be removed. Flatten your array before using + it for 'IN' conditions. + MSG - values_predicate = if values.include?(nil) - values = values.compact + values = values.flatten + end + + return attribute.in([]) if values.empty? && nils.empty? + ranges, values = values.partition { |v| v.is_a?(Range) } + + values_predicate = case values.length - when 0 - attribute.eq(nil) - when 1 - attribute.eq(values.first).or(attribute.eq(nil)) - else - attribute.in(values).or(attribute.eq(nil)) + when 0 then NullPredicate + when 1 then attribute.eq(values.first) + else attribute.in(values) end - else - attribute.in(values) + + unless nils.empty? + values_predicate = values_predicate.or(attribute.eq(nil)) end - array_predicates = ranges.map { |range| attribute.in(range) } - array_predicates << values_predicate + array_predicates = ranges.map { |range| attribute.between(range) } + array_predicates.unshift(values_predicate) array_predicates.inject { |composite, predicate| composite.or(predicate) } end + + module NullPredicate + def self.or(other) + other + end + end end end end |