aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/join_dependency.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/join_dependency.rb')
-rw-r--r--activerecord/lib/active_record/associations/join_dependency.rb92
1 files changed, 45 insertions, 47 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb
index c1faa021aa..0835d2cc9b 100644
--- a/activerecord/lib/active_record/associations/join_dependency.rb
+++ b/activerecord/lib/active_record/associations/join_dependency.rb
@@ -67,42 +67,31 @@ module ActiveRecord
end
end
- def initialize(base, table, associations, alias_tracker)
- @alias_tracker = alias_tracker
+ def initialize(base, table, associations)
tree = self.class.make_tree associations
@join_root = JoinBase.new(base, table, build(tree, base))
- @join_root.children.each { |child| construct_tables! @join_root, child }
end
def reflections
join_root.drop(1).map!(&:reflection)
end
- def join_constraints(joins_to_add, join_type)
- joins = join_root.children.flat_map { |child|
- make_join_constraints(join_root, child, join_type)
- }
+ def join_constraints(joins_to_add, join_type, alias_tracker)
+ @alias_tracker = alias_tracker
+
+ construct_tables!(join_root)
+ joins = make_join_constraints(join_root, join_type)
joins.concat joins_to_add.flat_map { |oj|
+ construct_tables!(oj.join_root)
if join_root.match? oj.join_root
walk join_root, oj.join_root
else
- oj.join_root.children.flat_map { |child|
- make_join_constraints(oj.join_root, child, join_type, true)
- }
+ make_join_constraints(oj.join_root, join_type)
end
}
end
- def aliases
- @aliases ||= Aliases.new join_root.each_with_index.map { |join_part, i|
- columns = join_part.column_names.each_with_index.map { |column_name, j|
- Aliases::Column.new column_name, "t#{i}_r#{j}"
- }
- Aliases::Table.new(join_part, columns)
- }
- end
-
def instantiate(result_set, &block)
primary_key = aliases.column_alias(join_root, join_root.primary_key)
@@ -127,35 +116,49 @@ module ActiveRecord
result_set.each { |row_hash|
parent_key = primary_key ? row_hash[primary_key] : row_hash
parent = parents[parent_key] ||= join_root.instantiate(row_hash, column_aliases, &block)
- construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases)
+ construct(parent, join_root, row_hash, seen, model_cache)
}
end
parents.values
end
+ def apply_column_aliases(relation)
+ relation._select!(-> { aliases.columns })
+ end
+
protected
- attr_reader :alias_tracker, :base_klass, :join_root
+ attr_reader :join_root
private
+ attr_reader :alias_tracker
- def make_constraints(parent, child, tables, join_type)
- chain = child.reflection.chain
- foreign_table = parent.table
- foreign_klass = parent.base_klass
- child.join_constraints(foreign_table, foreign_klass, join_type, tables, chain)
+ def aliases
+ @aliases ||= Aliases.new join_root.each_with_index.map { |join_part, i|
+ columns = join_part.column_names.each_with_index.map { |column_name, j|
+ Aliases::Column.new column_name, "t#{i}_r#{j}"
+ }
+ Aliases::Table.new(join_part, columns)
+ }
end
- def make_outer_joins(parent, child)
- join_type = Arel::Nodes::OuterJoin
- make_join_constraints(parent, child, join_type, true)
+ def construct_tables!(join_root)
+ join_root.each_children do |parent, child|
+ child.tables = table_aliases_for(parent, child)
+ end
end
- def make_join_constraints(parent, child, join_type, aliasing = false)
- tables = aliasing ? table_aliases_for(parent, child) : child.tables
- joins = make_constraints(parent, child, tables, join_type)
+ def make_join_constraints(join_root, join_type)
+ join_root.children.flat_map do |child|
+ make_constraints(join_root, child, join_type)
+ end
+ end
- joins.concat child.children.flat_map { |c| make_join_constraints(child, c, join_type, aliasing) }
+ def make_constraints(parent, child, join_type = Arel::Nodes::OuterJoin)
+ foreign_table = parent.table
+ foreign_klass = parent.base_klass
+ joins = child.join_constraints(foreign_table, foreign_klass, join_type, alias_tracker)
+ joins.concat child.children.flat_map { |c| make_constraints(child, c, join_type) }
end
def table_aliases_for(parent, node)
@@ -168,11 +171,6 @@ module ActiveRecord
}
end
- def construct_tables!(parent, node)
- node.tables = table_aliases_for(parent, node)
- node.children.each { |child| construct_tables! node, child }
- end
-
def table_alias_for(reflection, parent, join)
name = "#{reflection.plural_name}_#{parent.table_name}"
join ? "#{name}_join" : name
@@ -183,8 +181,8 @@ module ActiveRecord
[left.children.find { |node2| node1.match? node2 }, node1]
}.partition(&:first)
- ojs = missing.flat_map { |_, n| make_outer_joins left, n }
- intersection.flat_map { |l, r| walk l, r }.concat ojs
+ joins = intersection.flat_map { |l, r| r.table = l.table; walk(l, r) }
+ joins.concat missing.flat_map { |_, n| make_constraints(left, n) }
end
def find_reflection(klass, name)
@@ -202,11 +200,11 @@ module ActiveRecord
raise EagerLoadPolymorphicError.new(reflection)
end
- JoinAssociation.new(reflection, build(right, reflection.klass), alias_tracker)
+ JoinAssociation.new(reflection, build(right, reflection.klass))
end
end
- def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
+ def construct(ar_parent, parent, row, seen, model_cache)
return if ar_parent.nil?
parent.children.each do |node|
@@ -215,7 +213,7 @@ module ActiveRecord
other.loaded!
elsif ar_parent.association_cached?(node.reflection.name)
model = ar_parent.association(node.reflection.name).target
- construct(model, node, row, rs, seen, model_cache, aliases)
+ construct(model, node, row, seen, model_cache)
next
end
@@ -230,9 +228,9 @@ module ActiveRecord
model = seen[ar_parent.object_id][node.base_klass][id]
if model
- construct(model, node, row, rs, seen, model_cache, aliases)
+ construct(model, node, row, seen, model_cache)
else
- model = construct_model(ar_parent, node, row, model_cache, id, aliases)
+ model = construct_model(ar_parent, node, row, model_cache, id)
if node.reflection.scope &&
node.reflection.scope_for(node.base_klass.unscoped).readonly_value
@@ -240,12 +238,12 @@ module ActiveRecord
end
seen[ar_parent.object_id][node.base_klass][id] = model
- construct(model, node, row, rs, seen, model_cache, aliases)
+ construct(model, node, row, seen, model_cache)
end
end
end
- def construct_model(record, node, row, model_cache, id, aliases)
+ def construct_model(record, node, row, model_cache, id)
other = record.association(node.reflection.name)
model = model_cache[node][id] ||=