提交 b4aae5a8 编写于 作者: A Aaron Patterson

fix variable names and speed up relation ordering

上级 e5299c1e
...@@ -72,17 +72,9 @@ def options ...@@ -72,17 +72,9 @@ def options
reflection.options reflection.options
end end
def preloaded_records1
associated_records_by_owner.values.flatten
end
def preloaded_records def preloaded_records
if owners.first.association(reflection.name).loaded?
[]
else
associated_records_by_owner.values.flatten associated_records_by_owner.values.flatten
end end
end
def loaded? def loaded?
@loaded @loaded
......
...@@ -27,7 +27,7 @@ def associated_records_by_owner ...@@ -27,7 +27,7 @@ def associated_records_by_owner
through_records = owners.map do |owner, h| through_records = owners.map do |owner, h|
association = owner.association through_reflection.name association = owner.association through_reflection.name
x = [owner, Array(association.reader), association] x = [owner, Array(association.reader)]
# Dont cache the association - we would only be caching a subset # Dont cache the association - we would only be caching a subset
association.reset if should_reset association.reset if should_reset
...@@ -35,7 +35,7 @@ def associated_records_by_owner ...@@ -35,7 +35,7 @@ def associated_records_by_owner
x x
end end
middle_records = through_records.map { |rec| rec[1] }.flatten middle_records = through_records.map { |(_,rec)| rec }.flatten
preloader = Preloader.new(middle_records, preloader = Preloader.new(middle_records,
source_reflection.name, source_reflection.name,
...@@ -49,17 +49,21 @@ def associated_records_by_owner ...@@ -49,17 +49,21 @@ def associated_records_by_owner
} }
end end
@associated_records_by_owner = through_records.each_with_object({}) { |(lhs,middles,assoc),h| @associated_records_by_owner = through_records.each_with_object({}) { |(lhs,middles),h|
x = middle_to_pl[middles.first] preloader = middle_to_pl[middles.first]
rhs_records = middles.flat_map { |r| rhs_records = middles.flat_map { |r|
r.send(source_reflection.name) r.send(source_reflection.name)
}.compact }.compact
if x && x.loaded? if preloader && preloader.loaded?
rs = rhs_records.sort_by { |rhs| loaded_records = preloader.preloaded_records
x.preloaded_records1.index(rhs) i = 0
record_index = loaded_records.each_with_object({}) { |r,indexes|
indexes[r] = i
i += 1
} }
rs = rhs_records.sort_by { |rhs| record_index[rhs] }
else else
rs = rhs_records rs = rhs_records
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册