Move join scopes on to the reflection object

Scopes can only ever be *not* reflection objects when they are passed in
to the Reflection constructor.  Given this fact, we can eliminate is_a
checks and an intermediate array object by just asking the reflection
object for join scopes.
上级 76be78c5
......@@ -41,14 +41,7 @@ def join_constraints(foreign_table, foreign_klass, join_type, tables, chain)
constraint = build_constraint(klass, table, key, foreign_table, foreign_key)
predicate_builder = PredicateBuilder.new(TableMetadata.new(klass, table))
scope_chain_items = reflection.scopes.map do |item|
if item.is_a?(Relation)
item
else
ActiveRecord::Relation.create(klass, table, predicate_builder)
.instance_exec(&item)
end
end
scope_chain_items = reflection.join_scopes(table, predicate_builder)
klass_scope =
if klass.current_scope
......
......@@ -187,6 +187,15 @@ def scope_chain
end
deprecate :scope_chain
def join_scopes(table, predicate_builder) # :nodoc:
if scope
[ActiveRecord::Relation.create(klass, table, predicate_builder)
.instance_exec(&scope)]
else
[]
end
end
def constraints
chain.map(&:scopes).flatten
end
......@@ -806,6 +815,10 @@ def scopes
source_reflection.scopes + super
end
def join_scopes(table, predicate_builder) # :nodoc:
source_reflection.join_scopes(table, predicate_builder) + super
end
def source_type_scope
through_reflection.klass.where(foreign_type => options[:source_type])
end
......@@ -990,6 +1003,15 @@ def scopes
end
end
def join_scopes(table, predicate_builder) # :nodoc:
scopes = @previous_reflection.join_scopes(table, predicate_builder) + super
if @previous_reflection.options[:source_type]
scopes + [@previous_reflection.source_type_scope]
else
scopes
end
end
def klass
@reflection.klass
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册