1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Move #type_caster to alias tracker initialize

This moves the `#type_caster` from the `aliased_table_for` and into the
initialize of the `alias_tracker`.
This commit is contained in:
eileencodes 2014-12-29 14:12:39 -05:00
parent 96e277c03b
commit 39abe8355a
3 changed files with 10 additions and 13 deletions

View file

@ -7,10 +7,10 @@ module ActiveRecord
class AliasTracker # :nodoc:
attr_reader :aliases
def self.create(connection, initial_table)
def self.create(connection, initial_table, type_caster)
aliases = Hash.new(0)
aliases[initial_table] = 1
new connection, aliases
new connection, aliases, type_caster
end
def self.create_with_joins(connection, initial_table, joins, type_caster)
@ -54,16 +54,17 @@ module ActiveRecord
end
# table_joins is an array of arel joins which might conflict with the aliases we assign here
def initialize(connection, aliases)
def initialize(connection, aliases, type_caster)
@aliases = aliases
@connection = connection
@type_caster = type_caster
end
def aliased_table_for(table_name, aliased_name, **table_options)
def aliased_table_for(table_name, aliased_name)
if aliases[table_name].zero?
# If it's zero, we can have our table_name
aliases[table_name] = 1
Arel::Table.new(table_name, table_options)
Arel::Table.new(table_name, type_caster: @type_caster)
else
# Otherwise, we need to use an alias
aliased_name = @connection.table_alias_for(aliased_name)
@ -76,7 +77,7 @@ module ActiveRecord
else
aliased_name
end
Arel::Table.new(table_name, table_options).alias(table_alias)
Arel::Table.new(table_name, type_caster: @type_caster).alias(table_alias)
end
end

View file

@ -33,7 +33,7 @@ module ActiveRecord
reflection = association.reflection
scope = klass.unscoped
owner = association.owner
alias_tracker = AliasTracker.create connection, association.klass.table_name
alias_tracker = AliasTracker.create connection, association.klass.table_name, klass.type_caster
chain_head, chain_tail = get_chain(reflection, association, alias_tracker)
scope.extending! Array(reflection.options[:extend])

View file

@ -93,7 +93,7 @@ module ActiveRecord
# joins # => []
#
def initialize(base, associations, joins)
@alias_tracker = AliasTracker.create_with_joins(base.connection, base.table_name, joins)
@alias_tracker = AliasTracker.create_with_joins(base.connection, base.table_name, joins, base.type_caster)
tree = self.class.make_tree associations
@join_root = JoinBase.new base, build(tree, base)
@join_root.children.each { |child| construct_tables! @join_root, child }
@ -185,13 +185,9 @@ module ActiveRecord
def table_aliases_for(parent, node)
node.reflection.chain.map { |reflection|
if reflection.klass
type_caster = reflection.klass.type_caster
end
alias_tracker.aliased_table_for(
reflection.table_name,
table_alias_for(reflection, parent, reflection != node.reflection),
type_caster: type_caster,
table_alias_for(reflection, parent, reflection != node.reflection)
)
}
end