mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
remove klass delegator
This commit is contained in:
parent
22a1a5ac8c
commit
40a015f730
1 changed files with 11 additions and 10 deletions
|
@ -3,21 +3,22 @@ module ActiveRecord
|
||||||
class AssociationScope #:nodoc:
|
class AssociationScope #:nodoc:
|
||||||
attr_reader :association, :alias_tracker
|
attr_reader :association, :alias_tracker
|
||||||
|
|
||||||
delegate :klass, :reflection, :to => :association
|
delegate :reflection, :to => :association
|
||||||
|
|
||||||
def initialize(association)
|
def initialize(association)
|
||||||
@association = association
|
@association = association
|
||||||
@alias_tracker = AliasTracker.new klass.connection
|
@alias_tracker = AliasTracker.new association.klass.connection
|
||||||
end
|
end
|
||||||
|
|
||||||
def scope
|
def scope
|
||||||
|
klass = association.klass
|
||||||
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)
|
add_constraints(scope, owner, scope_chain, chain, klass)
|
||||||
end
|
end
|
||||||
|
|
||||||
def join_type
|
def join_type
|
||||||
|
@ -26,10 +27,10 @@ module ActiveRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def construct_tables(chain)
|
def construct_tables(chain, klass)
|
||||||
chain.map do |reflection|
|
chain.map do |reflection|
|
||||||
alias_tracker.aliased_table_for(
|
alias_tracker.aliased_table_for(
|
||||||
table_name_for(reflection),
|
table_name_for(reflection, klass),
|
||||||
table_alias_for(reflection, reflection != self.reflection)
|
table_alias_for(reflection, reflection != self.reflection)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -62,15 +63,15 @@ module ActiveRecord
|
||||||
bind_value scope, column, value
|
bind_value scope, column, value
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_constraints(scope, owner, scope_chain, chain)
|
def add_constraints(scope, owner, scope_chain, chain, assoc_klass)
|
||||||
tables = construct_tables(chain)
|
tables = construct_tables(chain, assoc_klass)
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
if reflection.source_macro == :belongs_to
|
if reflection.source_macro == :belongs_to
|
||||||
if reflection.options[:polymorphic]
|
if reflection.options[:polymorphic]
|
||||||
key = reflection.association_primary_key(self.klass)
|
key = reflection.association_primary_key(assoc_klass)
|
||||||
else
|
else
|
||||||
key = reflection.association_primary_key
|
key = reflection.association_primary_key
|
||||||
end
|
end
|
||||||
|
@ -103,7 +104,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
is_first_chain = i == 0
|
is_first_chain = i == 0
|
||||||
klass = is_first_chain ? self.klass : reflection.klass
|
klass = is_first_chain ? assoc_klass : reflection.klass
|
||||||
|
|
||||||
# Exclude the scope of the association itself, because that
|
# Exclude the scope of the association itself, because that
|
||||||
# was already merged in the #scope method.
|
# was already merged in the #scope method.
|
||||||
|
@ -130,7 +131,7 @@ module ActiveRecord
|
||||||
reflection.name
|
reflection.name
|
||||||
end
|
end
|
||||||
|
|
||||||
def table_name_for(reflection)
|
def table_name_for(reflection, klass)
|
||||||
if reflection == self.reflection
|
if reflection == self.reflection
|
||||||
# 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
|
||||||
|
|
Loading…
Reference in a new issue