aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/alias_tracker.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-12-12 09:55:32 +0000
committerJon Leighton <j@jonathanleighton.com>2010-12-12 09:55:32 +0000
commit9a98c766e045aebc2ef6d5b716936b73407f095d (patch)
tree899834482c828f31a89ebc7bb6e19cbe0b5f18d3 /activerecord/lib/active_record/associations/alias_tracker.rb
parent3a7f43ca6ecf1735e1a82d4a68ac8f62b5cf2fcf (diff)
parent307443972c5f6de959a5401eec76ca327484b10c (diff)
downloadrails-9a98c766e045aebc2ef6d5b716936b73407f095d.tar.gz
rails-9a98c766e045aebc2ef6d5b716936b73407f095d.tar.bz2
rails-9a98c766e045aebc2ef6d5b716936b73407f095d.zip
Merge branch 'master' into nested_has_many_through
Conflicts: activerecord/CHANGELOG activerecord/lib/active_record/associations/class_methods/join_dependency.rb activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb activerecord/lib/active_record/associations/has_many_through_association.rb
Diffstat (limited to 'activerecord/lib/active_record/associations/alias_tracker.rb')
-rw-r--r--activerecord/lib/active_record/associations/alias_tracker.rb34
1 files changed, 21 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations/alias_tracker.rb b/activerecord/lib/active_record/associations/alias_tracker.rb
index 64582188b6..6428fcec0a 100644
--- a/activerecord/lib/active_record/associations/alias_tracker.rb
+++ b/activerecord/lib/active_record/associations/alias_tracker.rb
@@ -5,11 +5,10 @@ module ActiveRecord
# Keeps track of table aliases for ActiveRecord::Associations::ClassMethods::JoinDependency and
# ActiveRecord::Associations::ThroughAssociationScope
class AliasTracker # :nodoc:
- # other_sql is some other sql which might conflict with the aliases we assign here. Therefore
- # we store other_sql so that we can scan it before assigning a specific name.
- def initialize(other_sql = nil)
- @aliases = Hash.new
- @other_sql = other_sql.to_s.downcase
+ # table_joins is an array of arel joins which might conflict with the aliases we assign here
+ def initialize(table_joins = nil)
+ @aliases = Hash.new
+ @table_joins = table_joins
end
def aliased_name_for(table_name, aliased_name = nil)
@@ -47,15 +46,24 @@ module ActiveRecord
def initialize_count_for(name)
@aliases[name] = 0
- unless @other_sql.blank?
+ unless @table_joins.nil? || 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).downcase
-
- # Table names
- @aliases[name] += @other_sql.scan(/join(?:\s+\w+)?\s+#{quoted_name}\son/).size
-
- # Table aliases
- @aliases[name] += @other_sql.scan(/join(?:\s+\w+)?\s+\S+\s+#{quoted_name}\son/).size
+ 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
+ }.sum
end
@aliases[name]