diff options
Diffstat (limited to 'activerecord/lib/active_record/associations/alias_tracker.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/alias_tracker.rb | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/activerecord/lib/active_record/associations/alias_tracker.rb b/activerecord/lib/active_record/associations/alias_tracker.rb index 6428fcec0a..6fc2bfdb31 100644 --- a/activerecord/lib/active_record/associations/alias_tracker.rb +++ b/activerecord/lib/active_record/associations/alias_tracker.rb @@ -5,32 +5,44 @@ module ActiveRecord # Keeps track of table aliases for ActiveRecord::Associations::ClassMethods::JoinDependency and # ActiveRecord::Associations::ThroughAssociationScope class AliasTracker # :nodoc: + attr_reader :aliases, :table_joins + # table_joins is an array of arel joins which might conflict with the aliases we assign here - def initialize(table_joins = nil) + def initialize(table_joins = []) @aliases = Hash.new @table_joins = table_joins end + def aliased_table_for(table_name, aliased_name = nil) + table_alias = aliased_name_for(table_name, aliased_name) + + if table_alias == table_name # TODO: Is this conditional necessary? + Arel::Table.new(table_name) + else + Arel::Table.new(table_name).alias(table_alias) + end + end + def aliased_name_for(table_name, aliased_name = nil) aliased_name ||= table_name - initialize_count_for(table_name) if @aliases[table_name].nil? + initialize_count_for(table_name) if aliases[table_name].nil? - if @aliases[table_name].zero? + if aliases[table_name].zero? # If it's zero, we can have our table_name - @aliases[table_name] = 1 + aliases[table_name] = 1 table_name else # Otherwise, we need to use an alias aliased_name = connection.table_alias_for(aliased_name) - initialize_count_for(aliased_name) if @aliases[aliased_name].nil? + initialize_count_for(aliased_name) if aliases[aliased_name].nil? # Update the count - @aliases[aliased_name] += 1 + aliases[aliased_name] += 1 - if @aliases[aliased_name] > 1 - "#{truncate(aliased_name)}_#{@aliases[aliased_name]}" + if aliases[aliased_name] > 1 + "#{truncate(aliased_name)}_#{aliases[aliased_name]}" else aliased_name end @@ -44,29 +56,21 @@ module ActiveRecord private def initialize_count_for(name) - @aliases[name] = 0 + aliases[name] = 0 - unless @table_joins.nil? || Arel::Table === @table_joins + unless Arel::Table === table_joins # quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase quoted_name = connection.quote_table_name(name).downcase - @aliases[name] += @table_joins.grep(Arel::Nodes::Join).map { |join| - right = join.right - case right - when Arel::Table - right.name.downcase == name ? 1 : 0 - when String - # Table names + table aliases - right.downcase.scan( - /join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/ - ).size - else - 0 - end + aliases[name] += table_joins.map { |join| + # Table names + table aliases + join.left.downcase.scan( + /join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/ + ).size }.sum end - @aliases[name] + aliases[name] end def truncate(name) |