提交 2f0956cd 编写于 作者: S Sean Griffin

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.
上级 cfd9d1d3
......@@ -4,26 +4,29 @@ 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?)
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册