From 39abe8355a56992b32ed95e1ea1eb588c0ad7a6f Mon Sep 17 00:00:00 2001 From: eileencodes Date: Mon, 29 Dec 2014 14:12:39 -0500 Subject: [PATCH] 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`. --- .../lib/active_record/associations/alias_tracker.rb | 13 +++++++------ .../active_record/associations/association_scope.rb | 2 +- .../active_record/associations/join_dependency.rb | 8 ++------ 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/activerecord/lib/active_record/associations/alias_tracker.rb b/activerecord/lib/active_record/associations/alias_tracker.rb index 36e62fe6b1..2b7e4f28c5 100644 --- a/activerecord/lib/active_record/associations/alias_tracker.rb +++ b/activerecord/lib/active_record/associations/alias_tracker.rb @@ -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 diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 4fa227342f..170ae58b9f 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -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]) diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index 634abfac96..4b75370171 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -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