Skip to content

Commit

Permalink
Move constructing join scope to Reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Jun 25, 2017
1 parent cc93d0d commit c9cf8b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,7 @@ def join_constraints(foreign_table, foreign_klass, join_type, tables, chain)

constraint = build_constraint(klass, table, key, foreign_table, foreign_key)

predicate_builder = reflection.predicate_builder(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
Expand Down
15 changes: 11 additions & 4 deletions activerecord/lib/active_record/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ def scope_chain
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
[build_scope(table, predicate_builder).instance_exec(&scope)]
Expand Down Expand Up @@ -286,16 +294,15 @@ def build_scope(table, predicate_builder = predicate_builder(table))
Relation.create(klass, table, predicate_builder)
end

def predicate_builder(table)
PredicateBuilder.new(TableMetadata.new(klass, table))
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
Expand Down

0 comments on commit c9cf8b8

Please sign in to comment.