Class: ActiveRecord::PredicateBuilder::ArrayHandler

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/relation/predicate_builder/array_handler.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#call(attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'activerecord/lib/active_record/relation/predicate_builder/array_handler.rb', line 4

def call(attribute, value)
  values = value.map { |x| x.is_a?(Base) ? x.id : x }
  ranges, values = values.partition { |v| v.is_a?(Range) }

  values_predicate = if values.include?(nil)
    values = values.compact

    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))
    end
  else
    attribute.in(values)
  end

  array_predicates = ranges.map { |range| attribute.in(range) }
  array_predicates << values_predicate
  array_predicates.inject { |composite, predicate| composite.or(predicate) }
end