From 724a2684139342eb4613f78bfae723ef00911ff7 Mon Sep 17 00:00:00 2001
From: Nick Kallen <nkallen@nick-kallens-computer-2.local>
Date: Sat, 17 May 2008 20:24:22 -0700
Subject: performance enhancing drugs

---
 lib/arel/relations/aggregation.rb |  2 +-
 lib/arel/relations/compound.rb    |  2 +-
 lib/arel/relations/deletion.rb    |  2 +-
 lib/arel/relations/grouping.rb    |  2 +-
 lib/arel/relations/insertion.rb   |  2 +-
 lib/arel/relations/join.rb        |  2 +-
 lib/arel/relations/nil.rb         |  2 +-
 lib/arel/relations/order.rb       |  2 +-
 lib/arel/relations/projection.rb  |  2 +-
 lib/arel/relations/recursion.rb   |  4 ++--
 lib/arel/relations/relation.rb    | 14 +++++++++++++-
 lib/arel/relations/selection.rb   |  2 +-
 lib/arel/relations/skip.rb        |  2 +-
 lib/arel/relations/table.rb       |  4 ++--
 lib/arel/relations/take.rb        |  2 +-
 lib/arel/relations/update.rb      |  2 +-
 16 files changed, 30 insertions(+), 18 deletions(-)

(limited to 'lib/arel/relations')

diff --git a/lib/arel/relations/aggregation.rb b/lib/arel/relations/aggregation.rb
index d18ef014fa..d409c34284 100644
--- a/lib/arel/relations/aggregation.rb
+++ b/lib/arel/relations/aggregation.rb
@@ -19,7 +19,7 @@ module Arel
     end
     
     def ==(other)
-      self.class    == other.class and
+      Aggregation   == other.class and
       self.relation == other.relation
     end
   end
diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb
index 55b2bc80c7..abc5838805 100644
--- a/lib/arel/relations/compound.rb
+++ b/lib/arel/relations/compound.rb
@@ -12,7 +12,7 @@ module Arel
     end
 
     def relation_for(attribute)
-      join? && relation.relation_for(attribute) || self[attribute] && self
+      join? && relation.relation_for(attribute) || has_attribute?(attribute) && self
     end
   end
 end
\ No newline at end of file
diff --git a/lib/arel/relations/deletion.rb b/lib/arel/relations/deletion.rb
index 6c802ba905..8c9d873a02 100644
--- a/lib/arel/relations/deletion.rb
+++ b/lib/arel/relations/deletion.rb
@@ -18,7 +18,7 @@ module Arel
     end
     
     def ==(other)
-      self.class  == other.class    and
+      Deletion    == other.class    and
       relation    == other.relation
     end
   end
diff --git a/lib/arel/relations/grouping.rb b/lib/arel/relations/grouping.rb
index 2c39d23b2d..39b5942bcc 100644
--- a/lib/arel/relations/grouping.rb
+++ b/lib/arel/relations/grouping.rb
@@ -7,7 +7,7 @@ module Arel
     end
 
     def ==(other)
-      self.class  == other.class      and
+      Grouping    == other.class      and
       relation    == other.relation   and
       groupings   == other.groupings
     end
diff --git a/lib/arel/relations/insertion.rb b/lib/arel/relations/insertion.rb
index 37e7be8757..cc7fcb4fb0 100644
--- a/lib/arel/relations/insertion.rb
+++ b/lib/arel/relations/insertion.rb
@@ -20,7 +20,7 @@ module Arel
     end
     
     def ==(other)
-      self.class  == other.class    and
+      Insertion   == other.class    and
       relation    == other.relation and
       record      == other.record
     end
diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb
index 88d5f45b67..abd4eae4f6 100644
--- a/lib/arel/relations/join.rb
+++ b/lib/arel/relations/join.rb
@@ -9,7 +9,7 @@ module Arel
     end
 
     def ==(other)
-      self.class == other.class       and
+      Join       == other.class       and
       predicates == other.predicates  and (
         (relation1 == other.relation1 and relation2 == other.relation2) or
         (relation2 == other.relation1 and relation1 == other.relation2)
diff --git a/lib/arel/relations/nil.rb b/lib/arel/relations/nil.rb
index 714b11cc2a..e8d4cbc481 100644
--- a/lib/arel/relations/nil.rb
+++ b/lib/arel/relations/nil.rb
@@ -5,7 +5,7 @@ module Arel
     def name; '' end
 
     def ==(other)
-      self.class == other.class
+      Nil == other.class
     end
   end
 end
\ No newline at end of file
diff --git a/lib/arel/relations/order.rb b/lib/arel/relations/order.rb
index 2d72127722..31032e871c 100644
--- a/lib/arel/relations/order.rb
+++ b/lib/arel/relations/order.rb
@@ -11,7 +11,7 @@ module Arel
     end
     
     def ==(other)
-      self.class  == other.class    and
+      Order       == other.class    and
       relation    == other.relation and
       orderings   == other.orderings
     end
diff --git a/lib/arel/relations/projection.rb b/lib/arel/relations/projection.rb
index 7c64726852..7eea63d808 100644
--- a/lib/arel/relations/projection.rb
+++ b/lib/arel/relations/projection.rb
@@ -15,7 +15,7 @@ module Arel
     end
     
     def ==(other)
-      self.class  == other.class    and
+      Projection  == other.class    and
       relation    == other.relation and
       projections == other.projections
     end
diff --git a/lib/arel/relations/recursion.rb b/lib/arel/relations/recursion.rb
index fa4d9969c9..c2fcf1bd21 100644
--- a/lib/arel/relations/recursion.rb
+++ b/lib/arel/relations/recursion.rb
@@ -10,8 +10,8 @@ module Arel
       end
       
       def relation_for(attribute)
-        self[attribute] && self
-      end
+        has_attribute?(attribute) && self
+      end      
     end
   end
 end
\ No newline at end of file
diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb
index 920bcd2d8d..3704eb9318 100644
--- a/lib/arel/relations/relation.rb
+++ b/lib/arel/relations/relation.rb
@@ -136,10 +136,22 @@ module Arel
       end
       
       def find_attribute_matching_attribute(attribute)
-        attributes.select { |a| a.match?(attribute) }.max do |a1, a2|
+        matching_attributes(attribute).max do |a1, a2|
           (a1.original_attribute / attribute) <=> (a2.original_attribute / attribute)
         end
       end
+      
+      private
+      def matching_attributes(attribute)
+        (@matching_attributes ||= attributes.inject({}) do |hash, a|
+          (hash[a.root] ||= []) << a
+          hash
+        end)[attribute.root] || []
+      end
+      
+      def has_attribute?(attribute)
+        !matching_attributes(attribute).empty?
+      end
     end
     include AttributeAccessable
 
diff --git a/lib/arel/relations/selection.rb b/lib/arel/relations/selection.rb
index c50db1c88a..0c5956d2fc 100644
--- a/lib/arel/relations/selection.rb
+++ b/lib/arel/relations/selection.rb
@@ -13,7 +13,7 @@ module Arel
     end    
 
     def ==(other)
-      self.class == other.class and
+      Selection  == other.class and
       relation   == other.relation and
       predicate  == other.predicate
     end
diff --git a/lib/arel/relations/skip.rb b/lib/arel/relations/skip.rb
index f17e439ebf..4686e03177 100644
--- a/lib/arel/relations/skip.rb
+++ b/lib/arel/relations/skip.rb
@@ -7,7 +7,7 @@ module Arel
     end
 
     def ==(other)
-      self.class  == other.class    and
+      Skip        == other.class    and
       relation    == other.relation and
       skipped     == other.skipped
     end
diff --git a/lib/arel/relations/table.rb b/lib/arel/relations/table.rb
index b95e51a307..06625dd52a 100644
--- a/lib/arel/relations/table.rb
+++ b/lib/arel/relations/table.rb
@@ -17,7 +17,7 @@ module Arel
     end
 
     def column_for(attribute)
-      self[attribute] and columns.detect { |c| c.name == attribute.name.to_s }
+      has_attribute?(attribute) and columns.detect { |c| c.name == attribute.name.to_s }
     end
     
     def columns
@@ -29,7 +29,7 @@ module Arel
     end
     
     def ==(other)
-      self.class == other.class and
+      Table      == other.class and
       name       == other.name
     end
   end
diff --git a/lib/arel/relations/take.rb b/lib/arel/relations/take.rb
index d2743d7a6e..80aa62a878 100644
--- a/lib/arel/relations/take.rb
+++ b/lib/arel/relations/take.rb
@@ -7,7 +7,7 @@ module Arel
     end
 
     def ==(other)
-      self.class  == other.class    and
+      Take        == other.class    and
       relation    == other.relation and
       taken       == other.taken
     end
diff --git a/lib/arel/relations/update.rb b/lib/arel/relations/update.rb
index f1f6776f15..486ad2ab12 100644
--- a/lib/arel/relations/update.rb
+++ b/lib/arel/relations/update.rb
@@ -22,7 +22,7 @@ module Arel
     end
     
     def ==(other)
-      self.class  == other.class    and
+      Update      == other.class    and
       relation    == other.relation and
       assignments == other.assignments
     end
-- 
cgit v1.2.3