提交 743b6750 编写于 作者: A Aaron Patterson

cache the klass member of the reflection

上级 56051768
...@@ -66,6 +66,7 @@ def find_parent_in(other_join_dependency) ...@@ -66,6 +66,7 @@ def find_parent_in(other_join_dependency)
def join_to(manager) def join_to(manager)
tables = @tables.dup tables = @tables.dup
foreign_table = parent_table foreign_table = parent_table
foreign_klass = parent.base_klass foreign_klass = parent.base_klass
...@@ -75,6 +76,7 @@ def join_to(manager) ...@@ -75,6 +76,7 @@ def join_to(manager)
# more sense in this context), so we reverse # more sense in this context), so we reverse
chain.reverse_each do |reflection| chain.reverse_each do |reflection|
table = tables.shift table = tables.shift
klass = reflection.klass
case reflection.source_macro case reflection.source_macro
when :belongs_to when :belongs_to
...@@ -97,23 +99,23 @@ def join_to(manager) ...@@ -97,23 +99,23 @@ def join_to(manager)
foreign_key = reflection.active_record_primary_key foreign_key = reflection.active_record_primary_key
end end
constraint = build_constraint(reflection, table, key, foreign_table, foreign_key) constraint = build_constraint(klass, table, key, foreign_table, foreign_key)
scope_chain_items = scope_chain_iter.next.map do |item| scope_chain_items = scope_chain_iter.next.map do |item|
if item.is_a?(Relation) if item.is_a?(Relation)
item item
else else
ActiveRecord::Relation.new(reflection.klass, table).instance_exec(self, &item) ActiveRecord::Relation.new(klass, table).instance_exec(self, &item)
end end
end end
if reflection.type if reflection.type
scope_chain_items << scope_chain_items <<
ActiveRecord::Relation.new(reflection.klass, table) ActiveRecord::Relation.new(klass, table)
.where(reflection.type => foreign_klass.base_class.name) .where(reflection.type => foreign_klass.base_class.name)
end end
scope_chain_items.concat [reflection.klass.send(:build_default_scope)].compact scope_chain_items.concat [klass.send(:build_default_scope)].compact
rel = scope_chain_items.inject(scope_chain_items.shift) do |left, right| rel = scope_chain_items.inject(scope_chain_items.shift) do |left, right|
left.merge right left.merge right
...@@ -126,7 +128,7 @@ def join_to(manager) ...@@ -126,7 +128,7 @@ def join_to(manager)
manager.from(join(table, constraint)) manager.from(join(table, constraint))
# The current table in this iteration becomes the foreign table in the next # The current table in this iteration becomes the foreign table in the next
foreign_table, foreign_klass = table, reflection.klass foreign_table, foreign_klass = table, klass
end end
manager manager
...@@ -147,13 +149,13 @@ def join_to(manager) ...@@ -147,13 +149,13 @@ def join_to(manager)
# foreign_table #=> #<Arel::Table @name="physicians" ...> # foreign_table #=> #<Arel::Table @name="physicians" ...>
# foreign_key #=> id # foreign_key #=> id
# #
def build_constraint(reflection, table, key, foreign_table, foreign_key) def build_constraint(klass, table, key, foreign_table, foreign_key)
constraint = table[key].eq(foreign_table[foreign_key]) constraint = table[key].eq(foreign_table[foreign_key])
if reflection.klass.finder_needs_type_condition? if klass.finder_needs_type_condition?
constraint = table.create_and([ constraint = table.create_and([
constraint, constraint,
reflection.klass.send(:type_condition, table) klass.send(:type_condition, table)
]) ])
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册