1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

remove the reflection delegate

This commit is contained in:
Aaron Patterson 2014-02-14 14:02:18 -08:00
parent 40a015f730
commit a35325e324

View file

@ -3,8 +3,6 @@ module ActiveRecord
class AssociationScope #:nodoc: class AssociationScope #:nodoc:
attr_reader :association, :alias_tracker attr_reader :association, :alias_tracker
delegate :reflection, :to => :association
def initialize(association) def initialize(association)
@association = association @association = association
@alias_tracker = AliasTracker.new association.klass.connection @alias_tracker = AliasTracker.new association.klass.connection
@ -12,13 +10,14 @@ module ActiveRecord
def scope def scope
klass = association.klass klass = association.klass
reflection = association.reflection
scope = klass.unscoped scope = klass.unscoped
scope.extending! Array(reflection.options[:extend]) scope.extending! Array(reflection.options[:extend])
owner = association.owner owner = association.owner
scope_chain = reflection.scope_chain scope_chain = reflection.scope_chain
chain = reflection.chain chain = reflection.chain
add_constraints(scope, owner, scope_chain, chain, klass) add_constraints(scope, owner, scope_chain, chain, klass, reflection)
end end
def join_type def join_type
@ -27,17 +26,17 @@ module ActiveRecord
private private
def construct_tables(chain, klass) def construct_tables(chain, klass, refl)
chain.map do |reflection| chain.map do |reflection|
alias_tracker.aliased_table_for( alias_tracker.aliased_table_for(
table_name_for(reflection, klass), table_name_for(reflection, klass, refl),
table_alias_for(reflection, reflection != self.reflection) table_alias_for(reflection, refl, reflection != refl)
) )
end end
end end
def table_alias_for(reflection, join = false) def table_alias_for(reflection, refl, join = false)
name = "#{reflection.plural_name}_#{alias_suffix}" name = "#{reflection.plural_name}_#{alias_suffix(refl)}"
name << "_join" if join name << "_join" if join
name name
end end
@ -63,8 +62,8 @@ module ActiveRecord
bind_value scope, column, value bind_value scope, column, value
end end
def add_constraints(scope, owner, scope_chain, chain, assoc_klass) def add_constraints(scope, owner, scope_chain, chain, assoc_klass, refl)
tables = construct_tables(chain, assoc_klass) tables = construct_tables(chain, assoc_klass, refl)
chain.each_with_index do |reflection, i| chain.each_with_index do |reflection, i|
table, foreign_table = tables.shift, tables.first table, foreign_table = tables.shift, tables.first
@ -111,7 +110,7 @@ module ActiveRecord
scope_chain[i].each do |scope_chain_item| scope_chain[i].each do |scope_chain_item|
item = eval_scope(klass, scope_chain_item, owner) item = eval_scope(klass, scope_chain_item, owner)
if scope_chain_item == self.reflection.scope if scope_chain_item == refl.scope
scope.merge! item.except(:where, :includes, :bind) scope.merge! item.except(:where, :includes, :bind)
end end
@ -127,12 +126,12 @@ module ActiveRecord
scope scope
end end
def alias_suffix def alias_suffix(refl)
reflection.name refl.name
end end
def table_name_for(reflection, klass) def table_name_for(reflection, klass, refl)
if reflection == self.reflection if reflection == refl
# If this is a polymorphic belongs_to, we want to get the klass from the # If this is a polymorphic belongs_to, we want to get the klass from the
# association because it depends on the polymorphic_type attribute of # association because it depends on the polymorphic_type attribute of
# the owner # the owner