Refactor construct_tables method
Move method structure into reflection classes for accessibly on each reflection rather than by traversing the chain.
This commit is contained in:
parent
69d05ae3e5
commit
076682692c
@ -63,22 +63,12 @@ def self.get_bind_values(owner, chain)
|
||||
|
||||
private
|
||||
|
||||
def construct_tables(chain, klass, refl, alias_tracker)
|
||||
def construct_tables(chain, alias_tracker, name)
|
||||
chain.map do |reflection|
|
||||
alias_tracker.aliased_table_for(
|
||||
table_name_for(reflection, klass, refl),
|
||||
table_alias_for(reflection, refl, reflection != refl),
|
||||
type_caster: klass.type_caster,
|
||||
)
|
||||
reflection.alias_name(name, alias_tracker)
|
||||
end
|
||||
end
|
||||
|
||||
def table_alias_for(reflection, refl, join = false)
|
||||
name = "#{reflection.plural_name}_#{alias_suffix(refl)}"
|
||||
name << "_join" if join
|
||||
name
|
||||
end
|
||||
|
||||
def join(table, constraint)
|
||||
table.create_join(table, table.create_on(constraint), join_type)
|
||||
end
|
||||
@ -145,14 +135,7 @@ def scope
|
||||
end
|
||||
|
||||
def table_name
|
||||
if @reflection.polymorphic?
|
||||
# If this is a polymorphic belongs_to, we want to get the klass from the
|
||||
# association because it depends on the polymorphic_type attribute of
|
||||
# the owner
|
||||
klass.table_name
|
||||
else
|
||||
@reflection.table_name
|
||||
end
|
||||
klass.table_name
|
||||
end
|
||||
|
||||
def plural_name
|
||||
@ -174,6 +157,12 @@ def constraints
|
||||
def source_type_info
|
||||
@reflection.source_type_info
|
||||
end
|
||||
|
||||
def alias_name(name, alias_tracker)
|
||||
alias_name = "#{plural_name}_#{name}_join"
|
||||
table_name = klass.table_name
|
||||
alias_tracker.aliased_table_for(table_name, alias_name)
|
||||
end
|
||||
end
|
||||
|
||||
def get_chain(reflection, association)
|
||||
@ -183,7 +172,7 @@ def get_chain(reflection, association)
|
||||
end
|
||||
|
||||
def add_constraints(scope, owner, assoc_klass, refl, tracker, chain)
|
||||
tables = construct_tables(chain, assoc_klass, refl, tracker)
|
||||
tables = construct_tables(chain, tracker, refl.name)
|
||||
|
||||
owner_reflection = chain.last
|
||||
table = tables.last
|
||||
@ -225,21 +214,6 @@ def add_constraints(scope, owner, assoc_klass, refl, tracker, chain)
|
||||
scope
|
||||
end
|
||||
|
||||
def alias_suffix(refl)
|
||||
refl.name
|
||||
end
|
||||
|
||||
def table_name_for(reflection, klass, refl)
|
||||
if reflection == refl
|
||||
# If this is a polymorphic belongs_to, we want to get the klass from the
|
||||
# association because it depends on the polymorphic_type attribute of
|
||||
# the owner
|
||||
klass.table_name
|
||||
else
|
||||
reflection.table_name
|
||||
end
|
||||
end
|
||||
|
||||
def eval_scope(klass, scope, owner)
|
||||
klass.unscoped.instance_exec(owner, &scope)
|
||||
end
|
||||
|
@ -165,6 +165,11 @@ def source_macro
|
||||
def constraints
|
||||
scope ? [scope] : []
|
||||
end
|
||||
|
||||
def alias_name(name, alias_tracker)
|
||||
alias_name = "#{plural_name}_#{name}"
|
||||
alias_tracker.aliased_table_for(table_name, alias_name)
|
||||
end
|
||||
end
|
||||
|
||||
# Base class for AggregateReflection and AssociationReflection. Objects of
|
||||
@ -753,6 +758,11 @@ def source_type_info
|
||||
source_type = @prev_reflection.options[:source_type]
|
||||
lambda { |object| where(type => source_type) }
|
||||
end
|
||||
|
||||
def alias_name(name, alias_tracker)
|
||||
alias_name = "#{plural_name}_#{name}"
|
||||
alias_tracker.aliased_table_for(table_name, alias_name)
|
||||
end
|
||||
end
|
||||
|
||||
# Consider the following example:
|
||||
|
Loading…
Reference in New Issue
Block a user