Class: ActiveRecord::PredicateBuilder::PolymorphicArrayHandler

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(predicate_builder) ⇒ PolymorphicArrayHandler

Returns a new instance of PolymorphicArrayHandler.


4
5
6
# File 'activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb', line 4

def initialize(predicate_builder)
  @predicate_builder = predicate_builder
end

Instance Method Details

#call(attribute, value) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb', line 8

def call(attribute, value)
  table = value.associated_table
  queries = value.type_to_ids_mapping.map do |type, ids|
    { table.association_foreign_type.to_s => type, table.association_foreign_key.to_s => ids }
  end

  predicates = queries.map { |query| predicate_builder.build_from_hash(query) }

  if predicates.size > 1
    type_and_ids_predicates = predicates.map { |type_predicate, id_predicate| Arel::Nodes::Grouping.new(type_predicate.and(id_predicate)) }
    type_and_ids_predicates.inject(&:or)
  else
    predicates.first
  end
end