123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::PredicateBuilder::ArrayHandler

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Modules:
Inherits: Object
Defined in: activerecord/lib/active_record/relation/predicate_builder/array_handler.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(predicate_builder) ⇒ ArrayHandler

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/array_handler.rb', line 8

def initialize(predicate_builder)
  @predicate_builder = predicate_builder
end

Instance Attribute Details

#predicate_builder (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/array_handler.rb', line 39

attr_reader :predicate_builder

Instance Method Details

#call(attribute, value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/array_handler.rb', line 12

def call(attribute, value)
  return attribute.in([]) if value.empty?

  values = value.map { |x| x.is_a?(Base) ? x.id : x }
  nils = values.compact!
  ranges = values.extract! { |v| v.is_a?(Range) }

  values_predicate =
    case values.length
    when 0 then NullPredicate
    when 1 then predicate_builder.build(attribute, values.first)
    else Arel::Nodes::HomogeneousIn.new(values, attribute, :in)
    end

  if nils
    values_predicate = values_predicate.or(attribute.eq(nil))
  end

  if ranges.empty?
    values_predicate
  else
    array_predicates = ranges.map! { |range| predicate_builder.build(attribute, range) }
    array_predicates.inject(values_predicate, &:or)
  end
end