Skip to content

Commit

Permalink
association in TableMetadata is not association but reflection
Browse files Browse the repository at this point in the history
It is very confusing.
  • Loading branch information
kamipo committed May 28, 2020
1 parent 10f6cb8 commit ccb13cb
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions activerecord/lib/active_record/table_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

module ActiveRecord
class TableMetadata # :nodoc:
delegate :foreign_type, :foreign_key, :join_primary_key, :join_foreign_key, to: :association, prefix: true
delegate :foreign_type, :foreign_key, :join_primary_key, :join_foreign_key, to: :reflection, prefix: :association

def initialize(klass, arel_table, association = nil, types = klass)
def initialize(klass, arel_table, reflection = nil, types = klass)
@klass = klass
@types = types
@arel_table = arel_table
@association = association
@reflection = reflection
end

def arel_attribute(column_name)
Expand All @@ -32,18 +32,18 @@ def associated_with?(association_name)
end

def associated_table(table_name)
association = klass._reflect_on_association(table_name) || klass._reflect_on_association(table_name.to_s.singularize)
reflection = klass._reflect_on_association(table_name) || klass._reflect_on_association(table_name.to_s.singularize)

if !association && table_name == arel_table.name
if !reflection && table_name == arel_table.name
self
elsif association && !association.polymorphic?
association_klass = association.klass
elsif reflection && !reflection.polymorphic?
association_klass = reflection.klass
arel_table = association_klass.arel_table.alias(table_name)
TableMetadata.new(association_klass, arel_table, association)
TableMetadata.new(association_klass, arel_table, reflection)
else
type_caster = TypeCaster::Connection.new(klass, table_name)
arel_table = Arel::Table.new(table_name, type_caster: type_caster)
TableMetadata.new(nil, arel_table, association, type_caster)
TableMetadata.new(nil, arel_table, reflection, type_caster)
end
end

Expand All @@ -52,7 +52,7 @@ def associated_predicate_builder(table_name)
end

def polymorphic_association?
association && association.polymorphic?
reflection&.polymorphic?
end

def aggregated_with?(aggregation_name)
Expand All @@ -75,6 +75,6 @@ def predicate_builder
end

private
attr_reader :klass, :types, :arel_table, :association
attr_reader :klass, :types, :arel_table, :reflection
end
end

0 comments on commit ccb13cb

Please sign in to comment.