Permalink
Browse files

generate the association hash from the top down

This prevents us from walking back up the parent tree once for each
leaf.
  • Loading branch information...
1 parent 6484ab2 commit 868457488d3c43ff4e9c28e34883620d90345185 @tenderlove tenderlove committed Oct 9, 2013
Showing with 15 additions and 17 deletions.
  1. +15 −17 activerecord/lib/active_record/associations/join_dependency.rb
@@ -71,6 +71,10 @@ def initialize(join_part)
@children = []
end
+ def association_hash
+ association_hash_iter children, {}
+ end
+
def each
yield self
iter = lambda { |list|
@@ -81,6 +85,15 @@ def each
}
iter.call children
end
+
+ private
+ def association_hash_iter nodes, acc
+ nodes.each { |node|
+ h = acc[node.join_part.reflection.name] ||= {}
+ association_hash_iter node.children, h
+ }
+ acc
+ end
end
def join_parts
@@ -93,7 +106,7 @@ def graft(*associations)
associations.reject { |association|
join_assocs.detect { |a| association == a }
}.each { |association|
- join_node = find_parent_node(association.parent) || @join_part
+ join_node = find_parent_node(association.parent) || @join_parts
type = association.join_type
find_or_build_scalar association.reflection, join_node, type
}
@@ -144,9 +157,7 @@ def instantiate(result_set)
private
def associations
- join_associations.each_with_object({}) do |assoc, tree|
- cache_joined_association assoc, tree
- end
+ @join_parts.association_hash
end
def find_parent_node(parent)
@@ -187,19 +198,6 @@ def remove_duplicate_results!(base, records, associations)
end
end
- def cache_joined_association(association, tree)
- associations = []
- parent = association.parent
- while parent != join_base
- associations.unshift(parent.reflection.name)
- parent = parent.parent
- end
- ref = associations.inject(tree) do |cache,key|
- cache[key]
- end
- ref[association.reflection.name] ||= {}
- end
-
def find_reflection(klass, name)
klass.reflect_on_association(name.intern) or
raise ConfigurationError, "Association named '#{ name }' was not found on #{ klass.name }; perhaps you misspelled it?"

0 comments on commit 8684574

Please sign in to comment.