Merge pull request #29557 from kamipo/extract_build_scope_and_predicate_builder

Extract `build_scope` and `predicate_builder` in `Reflection`
This commit is contained in:
Rafael França 2017-06-26 17:25:18 -04:00 committed by GitHub
commit 7b841b61f5
3 changed files with 21 additions and 20 deletions

View File

@ -142,7 +142,7 @@ module ActiveRecord
# Exclude the scope of the association itself, because that
# was already merged in the #scope method.
reflection.constraints.each do |scope_chain_item|
item = eval_scope(reflection.klass, table, scope_chain_item, owner)
item = eval_scope(reflection, table, scope_chain_item, owner)
if scope_chain_item == refl.scope
scope.merge! item.except(:where, :includes)
@ -163,9 +163,8 @@ module ActiveRecord
scope
end
def eval_scope(klass, table, scope, owner)
predicate_builder = PredicateBuilder.new(TableMetadata.new(klass, table))
ActiveRecord::Relation.create(klass, table, predicate_builder).instance_exec(owner, &scope)
def eval_scope(reflection, table, scope, owner)
reflection.build_scope(table).instance_exec(owner, &scope)
end
end
end

View File

@ -40,15 +40,7 @@ module ActiveRecord
constraint = build_constraint(klass, table, key, foreign_table, foreign_key)
predicate_builder = PredicateBuilder.new(TableMetadata.new(klass, table))
scope_chain_items = reflection.join_scopes(table, predicate_builder)
klass_scope = reflection.klass_join_scope(table, predicate_builder)
scope_chain_items.concat [klass_scope].compact
rel = scope_chain_items.inject(scope_chain_items.shift) do |left, right|
left.merge right
end
rel = reflection.join_scope(table)
if rel && !rel.arel.constraints.empty?
binds += rel.bound_attributes

View File

@ -185,10 +185,17 @@ module ActiveRecord
end
deprecate :scope_chain
def join_scope(table)
predicate_builder = predicate_builder(table)
scope_chain_items = join_scopes(table, predicate_builder)
klass_scope = klass_join_scope(table, predicate_builder)
scope_chain_items.inject(klass_scope || scope_chain_items.shift, &:merge!)
end
def join_scopes(table, predicate_builder) # :nodoc:
if scope
[ActiveRecord::Relation.create(klass, table, predicate_builder)
.instance_exec(&scope)]
[build_scope(table, predicate_builder).instance_exec(&scope)]
else
[]
end
@ -200,11 +207,7 @@ module ActiveRecord
scope.joins_values = scope.left_outer_joins_values = [].freeze
}
else
relation = ActiveRecord::Relation.create(
klass,
table,
predicate_builder,
)
relation = build_scope(table, predicate_builder)
klass.send(:build_default_scope, relation)
end
end
@ -287,12 +290,19 @@ module ActiveRecord
JoinKeys.new(join_pk(association_klass), join_fk)
end
def build_scope(table, predicate_builder = predicate_builder(table))
Relation.create(klass, table, predicate_builder)
end
protected
def actual_source_reflection # FIXME: this is a horrible name
self
end
private
def predicate_builder(table)
PredicateBuilder.new(TableMetadata.new(klass, table))
end
def join_pk(_)
foreign_key