Permalink
Browse files

Hook ThroughAssociationScope up to use the AliasTracker class

  • Loading branch information...
1 parent 3f2e258 commit 199db8c8c006a5f3bcbbe2a32d39444a741c5843 @jonleighton jonleighton committed Oct 12, 2010
Showing with 30 additions and 9 deletions.
  1. +30 −9 activerecord/lib/active_record/associations/through_association_scope.rb
@@ -20,6 +20,7 @@ def construct_scope
end
# Build SQL conditions from attributes, qualified by table name.
+ # TODO: Conditions on joins
def construct_conditions
reflection = @reflection.through_reflection_chain.last
conditions = construct_quoted_owner_attributes(reflection).map do |attr, value|
@@ -134,24 +135,44 @@ def construct_through_joins
joins.join(" ")
end
- # TODO: Use the same aliasing strategy (and code?) as JoinAssociation (as this is the
- # documented behaviour)
+ def alias_tracker
+ @alias_tracker ||= AliasTracker.new
+ end
+
def table_aliases
@table_aliases ||= begin
- tally = {}
@reflection.through_reflection_chain.inject({}) do |aliases, reflection|
- if tally[reflection.table_name].nil?
- tally[reflection.table_name] = 1
- aliases[reflection] = reflection.quoted_table_name
+ table_alias = quote_table_name(alias_tracker.aliased_name_for(
+ reflection.table_name,
+ table_alias_for(reflection, reflection != @reflection)
+ ))
+
+ if reflection.macro == :has_and_belongs_to_many
+ join_table_alias = quote_table_name(alias_tracker.aliased_name_for(
+ reflection.options[:join_table],
+ table_alias_for(reflection, true)
+ ))
+
+ aliases[reflection] = [table_alias, join_table_alias]
else
- tally[reflection.table_name] += 1
- aliased_table_name = reflection.table_name + "_#{tally[reflection.table_name]}"
- aliases[reflection] = reflection.klass.connection.quote_table_name(aliased_table_name)
+ aliases[reflection] = table_alias
end
+
aliases
end
end
end
+
+ def table_alias_for(reflection, join = false)
+ name = alias_tracker.pluralize(reflection.name)
+ name << "_#{@reflection.name}"
+ name << "_join" if join
+ name
+ end
+
+ def quote_table_name(table_name)
+ @reflection.klass.connection.quote_table_name(table_name)
+ end
# Construct attributes for associate pointing to owner.
def construct_owner_attributes(reflection)

0 comments on commit 199db8c

Please sign in to comment.