Class: ActiveRecord::PredicateBuilder::ArrayHandler

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

Overview

:nodoc:

Defined Under Namespace

Modules: NullPredicate

Instance Method Summary collapse

Instance Method Details

#call(attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_record/relation/predicate_builder/array_handler.rb', line 6

def call(attribute, value)
  values = value.map { |x| x.is_a?(Base) ? x.id : x }
  nils, values = values.partition(&:nil?)

  if values.any? { |val| val.is_a?(Array) }
    ActiveSupport::Deprecation.warn("      Passing a nested array to Active Record finder methods is\n      deprecated and will be removed. Flatten your array before using\n      it for 'IN' conditions.\n    MSG\n\n    values = values.flatten\n  end\n\n  return attribute.in([]) if values.empty? && nils.empty?\n\n  ranges, values = values.partition { |v| v.is_a?(Range) }\n\n  values_predicate =\n    case values.length\n    when 0 then NullPredicate\n    when 1 then attribute.eq(values.first)\n    else attribute.in(values)\n    end\n\n  unless nils.empty?\n    values_predicate = values_predicate.or(attribute.eq(nil))\n  end\n\n  array_predicates = ranges.map { |range| attribute.between(range) }\n  array_predicates.unshift(values_predicate)\n  array_predicates.inject { |composite, predicate| composite.or(predicate) }\nend\n".squish)