Skip to content

Commit

Permalink
remove klass delegator
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Feb 14, 2014
1 parent 22a1a5a commit 40a015f
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions activerecord/lib/active_record/associations/association_scope.rb
Expand Up @@ -3,21 +3,22 @@ module Associations
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
Expand All @@ -26,10 +27,10 @@ def join_type


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
Expand Down Expand Up @@ -62,15 +63,15 @@ def bind(scope, table_name, column_name, value)
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
Expand Down Expand Up @@ -103,7 +104,7 @@ def add_constraints(scope, owner, scope_chain, chain)
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.
Expand All @@ -130,7 +131,7 @@ def alias_suffix
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
Expand Down

0 comments on commit 40a015f

Please sign in to comment.