From a0425099d78c1884478ee3e69a09ea5f7afa1aa9 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 28 Jul 2010 13:51:51 -0700 Subject: simplifying the Christener --- lib/arel/algebra/attributes/attribute.rb | 6 ++---- lib/arel/engines/sql/christener.rb | 17 +++++++++-------- lib/arel/engines/sql/relations/table.rb | 1 + 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/arel/algebra/attributes/attribute.rb b/lib/arel/algebra/attributes/attribute.rb index 62c1a64324..90a3b13938 100644 --- a/lib/arel/algebra/attributes/attribute.rb +++ b/lib/arel/algebra/attributes/attribute.rb @@ -41,11 +41,9 @@ module Arel row[self] end - module Transformations - def self.included(klass) - klass.send :alias_method, :eql?, :== - end + alias :eql? :== + module Transformations def hash @hash ||= name.hash + root.relation.hash end diff --git a/lib/arel/engines/sql/christener.rb b/lib/arel/engines/sql/christener.rb index 6160f244f7..3c7fb0425e 100644 --- a/lib/arel/engines/sql/christener.rb +++ b/lib/arel/engines/sql/christener.rb @@ -2,17 +2,18 @@ module Arel module Sql class Christener def initialize - # FIXME: this exists because all objects hash the same. :'( - @used_names = Hash.new(0) - @relation_names = Hash.new do |hash, relation| - name = relation.table_alias || relation.name - @used_names[name] += 1 - hash[relation] = name + (@used_names[name] > 1 ? "_#{@used_names[name]}" : '') - end + @names = {} end def name_for(relation) - @relation_names[relation.table] + table = relation.table + name = table.table_alias || table.name + @names[name] ||= [] + + @names[name] << table unless @names[name].include? table + + idx = @names[name].index table + name + (idx == 0 ? '' : "_#{idx + 1}") end end end diff --git a/lib/arel/engines/sql/relations/table.rb b/lib/arel/engines/sql/relations/table.rb index 9ad939b896..d9ff95265b 100644 --- a/lib/arel/engines/sql/relations/table.rb +++ b/lib/arel/engines/sql/relations/table.rb @@ -17,6 +17,7 @@ module Arel def initialize(name, options = {}) @name = name.to_s @table_exists = nil + @table_alias = nil if options.is_a?(Hash) @options = options -- cgit v1.2.3