From 85fe5edcec77f96923ed4f4e8b63a247ebe6b055 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 16 Jul 2013 13:41:24 -0700 Subject: decouple the manager class from building join constraints --- .../associations/join_dependency/join_association.rb | 12 ++++++------ activerecord/lib/active_record/relation/query_methods.rb | 9 +++++---- 2 files changed, 11 insertions(+), 10 deletions(-) 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 ee1fcc17d8..3205cd41a2 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -64,7 +64,8 @@ module ActiveRecord end end - def join_to(manager) + def join_constraints + joins = [] tables = @tables.dup foreign_table = parent_table @@ -84,11 +85,10 @@ module ActiveRecord foreign_key = reflection.foreign_key when :has_and_belongs_to_many # Join the join table first... - manager.from(join( + joins << join( table, table[reflection.foreign_key]. - eq(foreign_table[reflection.active_record_primary_key]) - )) + eq(foreign_table[reflection.active_record_primary_key])) foreign_table, table = table, tables.shift @@ -125,13 +125,13 @@ module ActiveRecord constraint = constraint.and rel.arel.constraints end - manager.from(join(table, constraint)) + joins << join(table, constraint) # The current table in this iteration becomes the foreign table in the next foreign_table, foreign_klass = table, klass end - manager + joins end # Builds equality condition. diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 612c7d17a3..fb585ab8ab 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -948,10 +948,11 @@ module ActiveRecord join_dependency.graft(*stashed_association_joins) - # FIXME: refactor this to build an AST - join_dependency.join_associations.each do |association| - association.join_to(manager) - end + joins = join_dependency.join_associations.map { |association| + association.join_constraints + }.flatten + + joins.each { |join| manager.from join } manager.join_sources.concat join_list -- cgit v1.2.3