Browse files

move column_names_with_alias on to the alias cache object

  • Loading branch information...
1 parent 3d7efb1 commit 704bf0ecc550552f1cc5e453771917e8782b64ad @tenderlove tenderlove committed Oct 15, 2013
View
26 activerecord/lib/active_record/associations/join_dependency.rb
@@ -90,12 +90,22 @@ def initialize(tables)
i[column.name] = column.alias
}
}
+ @name_and_alias_cache = tables.each_with_object({}) { |table,h|
+ h[table.name] = table.columns.map { |column|
+ [column.name, column.alias]
+ }
+ }
end
def columns
@tables.flat_map { |t| t.column_aliases }
end
+ # An array of [column_name, alias] pairs for the table
+ def column_aliases(table)
+ @name_and_alias_cache[table]
+ end
+
def column_alias(table, column)
@alias_cache[table][column]
end
@@ -128,18 +138,17 @@ def instantiate(result_set, aliases)
seen = Hash.new { |h,parent_klass|
h[parent_klass] = Hash.new { |i,parent_id|
- i[parent_id] = Hash.new { |j,child_klass|
- j[child_klass] = {}
- }
+ i[parent_id] = Hash.new { |j,child_klass| j[child_klass] = {} }
}
}
model_cache = Hash.new { |h,klass| h[klass] = {} }
parents = model_cache[join_root]
+ column_aliases = aliases.column_aliases join_root.table
result_set.each { |row_hash|
primary_id = type_caster.type_cast row_hash[primary_key]
- parent = parents[primary_id] ||= join_root.instantiate(row_hash)
+ parent = parents[primary_id] ||= join_root.instantiate(row_hash, column_aliases)
construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases)
}
@@ -206,7 +215,7 @@ def build_join_association(reflection, parent, join_type)
raise EagerLoadPolymorphicError.new(reflection)
end
- node = JoinAssociation.new(reflection, join_root.to_a.length, join_type)
+ node = JoinAssociation.new(reflection, join_type)
construct_tables!(parent, node)
node
end
@@ -235,15 +244,16 @@ def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
if model
construct(model, node, row, rs, seen, model_cache, aliases)
else
- model = construct_model(ar_parent, node, row, model_cache, id)
+ model = construct_model(ar_parent, node, row, model_cache, id, aliases)
seen[parent.base_klass][primary_id][node.base_klass][id] = model
construct(model, node, row, rs, seen, model_cache, aliases)
end
end
end
- def construct_model(record, node, row, model_cache, id)
- model = model_cache[node][id] ||= node.instantiate(row)
+ def construct_model(record, node, row, model_cache, id, aliases)
+ model = model_cache[node][id] ||= node.instantiate(row,
+ aliases.column_aliases(node.table))
other = record.association(node.reflection.name)
if node.reflection.collection?
View
6 activerecord/lib/active_record/associations/join_dependency/join_association.rb
@@ -10,17 +10,13 @@ class JoinAssociation < JoinPart # :nodoc:
# What type of join will be generated, either Arel::InnerJoin (default) or Arel::OuterJoin
attr_accessor :join_type
- # These implement abstract methods from the superclass
- attr_reader :aliased_prefix
-
attr_accessor :tables
- def initialize(reflection, index, join_type)
+ def initialize(reflection, join_type)
super(reflection.klass)
@reflection = reflection
@join_type = join_type
- @aliased_prefix = "t#{ index }"
@tables = nil
end
View
4 activerecord/lib/active_record/associations/join_dependency/join_base.rb
@@ -9,10 +9,6 @@ def match?(other)
super && base_klass == other.base_klass
end
- def aliased_prefix
- "t0"
- end
-
def table
Arel::Table.new(table_name, arel_engine)
end
View
23 activerecord/lib/active_record/associations/join_dependency/join_part.rb
@@ -41,29 +41,12 @@ def table
raise NotImplementedError
end
- # The prefix to be used when aliasing columns in the active_record's table
- def aliased_prefix
- raise NotImplementedError
- end
-
# The alias for the active_record's table
def aliased_table_name
raise NotImplementedError
end
- # An array of [column_name, alias] pairs for the table
- def column_names_with_alias
- unless @column_names_with_alias
- @column_names_with_alias = []
-
- column_names.each_with_index do |column_name, i|
- @column_names_with_alias << [column_name, "#{aliased_prefix}_r#{i}"]
- end
- end
- @column_names_with_alias
- end
-
- def extract_record(row)
+ def extract_record(row, column_names_with_alias)
# This code is performance critical as it is called per row.
# see: https://github.com/rails/rails/pull/12185
hash = {}
@@ -80,8 +63,8 @@ def extract_record(row)
hash
end
- def instantiate(row)
- base_klass.instantiate(extract_record(row))
+ def instantiate(row, aliases)
+ base_klass.instantiate(extract_record(row, aliases))
end
end
end

0 comments on commit 704bf0e

Please sign in to comment.