Skip to content
Newer
Older
100644 30 lines (26 sloc) 844 Bytes
92a6033 @sgrif Add ability to specify how a class is converted to Arel predicate
sgrif authored
1 module ActiveRecord
2 class PredicateBuilder
3 class ArrayHandler # :nodoc:
4 def call(attribute, value)
5 values = value.map { |x| x.is_a?(Base) ? x.id : x }
6 ranges, values = values.partition { |v| v.is_a?(Range) }
7
8 values_predicate = if values.include?(nil)
9 values = values.compact
10
11 case values.length
12 when 0
13 attribute.eq(nil)
14 when 1
15 attribute.eq(values.first).or(attribute.eq(nil))
16 else
17 attribute.in(values).or(attribute.eq(nil))
18 end
19 else
20 attribute.in(values)
21 end
22
23 array_predicates = ranges.map { |range| attribute.in(range) }
24 array_predicates << values_predicate
25 array_predicates.inject { |composite, predicate| composite.or(predicate) }
26 end
27 end
28 end
29 end
Something went wrong with that request. Please try again.