From db994076946f44d9b0fea23ca8715a74cb1f2b87 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 21 Oct 2013 15:08:51 -0700 Subject: build the association graph functionally This lets us avoid the constant calls to Array#<< --- .../active_record/associations/join_dependency.rb | 25 ++++++++-------------- .../join_dependency/join_association.rb | 4 ++-- .../associations/join_dependency/join_part.rb | 4 ++-- 3 files changed, 13 insertions(+), 20 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index 20457e9e57..4bcd69c69b 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -52,11 +52,10 @@ module ActiveRecord # 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 @@ module ActiveRecord 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) diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb index d2c090ae56..191d430636 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -9,8 +9,8 @@ module ActiveRecord attr_accessor :tables - def initialize(reflection) - super(reflection.klass) + def initialize(reflection, children) + super(reflection.klass, children) @reflection = reflection @tables = nil diff --git a/activerecord/lib/active_record/associations/join_dependency/join_part.rb b/activerecord/lib/active_record/associations/join_dependency/join_part.rb index e22232168d..91e1c6a9d7 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_part.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_part.rb @@ -17,10 +17,10 @@ module ActiveRecord 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 -- cgit v1.2.3