aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-05-26 13:47:27 -0700
committerSean Griffin <sean@thoughtbot.com>2014-05-26 13:47:27 -0700
commit2f0956cde5b27dc9ad1e3ef54094b4cd37edc058 (patch)
tree346046671fa18c6a66d0fd0a8f095d61814ec9af /activerecord
parentcfd9d1d3e1e1d6bd51216d2e458337b5e904b7b0 (diff)
downloadrails-2f0956cde5b27dc9ad1e3ef54094b4cd37edc058.tar.gz
rails-2f0956cde5b27dc9ad1e3ef54094b4cd37edc058.tar.bz2
rails-2f0956cde5b27dc9ad1e3ef54094b4cd37edc058.zip
Refactor the handling of arrays in where predicates
Simplifies the code slightly, isolates non-nil non-range values into a single array, which will make it easier to do things like apply type casting to them in the future.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/predicate_builder/array_handler.rb25
1 files changed, 14 insertions, 11 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..7f937e1875 100644
--- a/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb
+++ b/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb
@@ -4,26 +4,29 @@ module ActiveRecord
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?)
- values_predicate = if values.include?(nil)
- values = values.compact
-
+ 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.inject { |composite, predicate| composite.or(predicate) }
end
+
+ module NullPredicate
+ def self.or(other)
+ other
+ end
+ end
end
end
end