Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

build the association graph functionally

This lets us avoid the constant calls to Array#<<
  • Loading branch information...
commit db994076946f44d9b0fea23ca8715a74cb1f2b87 1 parent 8f5647e
@tenderlove tenderlove authored
View
25 activerecord/lib/active_record/associations/join_dependency.rb
@@ -52,11 +52,10 @@ def self.walk_tree(associations, hash)
# joins #=> []
#
def initialize(base, associations, joins)
- @join_root = JoinBase.new(base)
@alias_tracker = AliasTracker.new(base.connection, joins)
@alias_tracker.aliased_name_for(base.table_name) # Updates the count for base.table_name to 1
tree = self.class.make_tree associations
- build tree, @join_root
+ @join_root = JoinBase.new base, build(tree, base)
@join_root.children.each { |child| construct_tables! @join_root, child }
end
@@ -212,23 +211,17 @@ def find_reflection(klass, name)
raise ConfigurationError, "Association named '#{ name }' was not found on #{ klass.name }; perhaps you misspelled it?"
end
- def build(associations, parent)
- associations.each do |name, right|
- reflection = find_reflection parent.base_klass, name
- join_association = build_join_association reflection
- parent.children << join_association
- build right, join_association
- end
- end
+ def build(associations, base_klass)
+ associations.map do |name, right|
+ reflection = find_reflection base_klass, name
+ reflection.check_validity!
- def build_join_association(reflection)
- reflection.check_validity!
+ if reflection.options[:polymorphic]
+ raise EagerLoadPolymorphicError.new(reflection)
+ end
- if reflection.options[:polymorphic]
- raise EagerLoadPolymorphicError.new(reflection)
+ JoinAssociation.new reflection, build(right, reflection.klass)
end
-
- JoinAssociation.new(reflection)
end
def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
View
4 activerecord/lib/active_record/associations/join_dependency/join_association.rb
@@ -9,8 +9,8 @@ class JoinAssociation < JoinPart # :nodoc:
attr_accessor :tables
- def initialize(reflection)
- super(reflection.klass)
+ def initialize(reflection, children)
+ super(reflection.klass, children)
@reflection = reflection
@tables = nil
View
4 activerecord/lib/active_record/associations/join_dependency/join_part.rb
@@ -17,10 +17,10 @@ class JoinPart # :nodoc:
delegate :table_name, :column_names, :primary_key, :to => :base_klass
- def initialize(base_klass)
+ def initialize(base_klass, children)
@base_klass = base_klass
@column_names_with_alias = nil
- @children = []
+ @children = children
end
def name
Please sign in to comment.
Something went wrong with that request. Please try again.