123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::PredicateBuilder::AssociationQueryValue

Do not use. This class is for internal use only.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(associated_table, value) ⇒ AssociationQueryValue

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb', line 6

def initialize(associated_table, value)
  @associated_table = associated_table
  @value = value
end

Instance Attribute Details

#associated_table (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb', line 23

attr_reader :associated_table, :value

#polymorphic_clause?Boolean (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb', line 55

def polymorphic_clause?
  primary_type && !value.where_values_hash.has_key?(primary_type)
end

#select_clause?Boolean (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb', line 51

def select_clause?
  value.select_values.empty?
end

#value (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb', line 23

attr_reader :associated_table, :value

Instance Method Details

#convert_to_id(value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb', line 59

def convert_to_id(value)
  if primary_key.is_a?(Array)
    primary_key.map do |attribute|
      if attribute == "id"
        value.id_value
      else
        value.public_send(attribute)
      end
    end
  elsif value.respond_to?(primary_key)
    value.public_send(primary_key)
  else
    value
  end
end

#ids (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb', line 25

def ids
  case value
  when Relation
    relation = value
    relation = relation.select(primary_key) if select_clause?
    relation = relation.where(primary_type => polymorphic_name) if polymorphic_clause?
    relation
  when Array
    value.map { |v| convert_to_id(v) }
  else
    [convert_to_id(value)]
  end
end

#polymorphic_name (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb', line 47

def polymorphic_name
  associated_table.polymorphic_name_association
end

#primary_key (private)

[ GitHub ]

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

def primary_key
  associated_table.join_primary_key
end

#primary_type (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb', line 43

def primary_type
  associated_table.join_primary_type
end

#queries

[ GitHub ]

  
# File 'activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb', line 11

def queries
  if associated_table.join_foreign_key.is_a?(Array)
    id_list = ids
    id_list = id_list.pluck(primary_key) if id_list.is_a?(Relation)

    id_list.map { |ids_set| associated_table.join_foreign_key.zip(ids_set).to_h }
  else
    [ associated_table.join_foreign_key => ids ]
  end
end