Skip to content

Commit

Permalink
Initialze #alias_tracker with base table name
Browse files Browse the repository at this point in the history
Instead of initializing an empty connection use the base table name
instead. Split up and refactor `#create` to be 2 methods `#create` and
`#create_with_joins`. Removes the need to update the count by 1 on
initialzing a JoinDependency.
  • Loading branch information
eileencodes committed Jan 2, 2015
1 parent 16fafd6 commit 0408e21
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
19 changes: 11 additions & 8 deletions activerecord/lib/active_record/associations/alias_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ module Associations
class AliasTracker # :nodoc:
attr_reader :aliases

def self.empty(connection)
new connection, Hash.new(0)
def self.create(connection, initial_table)
aliases = Hash.new(0)
aliases[initial_table] = 1
new connection, aliases
end

def self.create(connection, table_joins)
if table_joins.empty?
empty connection
def self.create_with_joins(connection, initial_table, joins, type_caster)
if joins.empty?
create(connection, initial_table, type_caster)
else
aliases = Hash.new { |h,k|
h[k] = initial_count_for(connection, k, table_joins)
aliases = Hash.new { |h, k|
h[k] = initial_count_for(connection, k, joins)
}
new connection, aliases
aliases[initial_table] = 1
new connection, aliases, type_caster
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ def initialize(reflection, alias_name)
def get_chain(refl, association, tracker)
name = refl.name
runtime_reflection = ActiveRecord::Reflection::RuntimeReflection.new(refl, association)
alias_name = tracker.aliased_table_for(runtime_reflection.table_name, runtime_reflection.alias_candidate(name))
prev = runtime_reflection
refl.chain.drop(1).each { |reflection|
alias_name = tracker.aliased_table_for(reflection.table_name, reflection.alias_candidate(name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ def self.walk_tree(associations, hash)
# joins # => []
#
def initialize(base, associations, joins)
@alias_tracker = AliasTracker.create(base.connection, joins)
@alias_tracker.aliased_table_for(base.table_name, base.table_name, type_caster: base.type_caster) # Updates the count for base.table_name to 1
@alias_tracker = AliasTracker.create_with_joins(base.connection, base.table_name, joins)
tree = self.class.make_tree associations
@join_root = JoinBase.new base, build(tree, base)
@join_root.children.each { |child| construct_tables! @join_root, child }
Expand Down

0 comments on commit 0408e21

Please sign in to comment.