diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-04-27 20:40:20 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-04-27 20:40:20 -0700 |
commit | 3ae7a601e962468468eac1399ebdfd7113730310 (patch) | |
tree | 548407d22d1799f20366c3a122fb4593002e5063 | |
parent | 4a6822d5e416f2e687d32a14ff98c7b5c815369c (diff) | |
download | rails-3ae7a601e962468468eac1399ebdfd7113730310.tar.gz rails-3ae7a601e962468468eac1399ebdfd7113730310.tar.bz2 rails-3ae7a601e962468468eac1399ebdfd7113730310.zip |
attribute disambiguation
-rw-r--r-- | doc/TODO | 1 | ||||
-rw-r--r-- | lib/arel/relations/join.rb | 14 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 4 | ||||
-rw-r--r-- | spec/arel/unit/relations/join_spec.rb | 33 |
4 files changed, 31 insertions, 21 deletions
@@ -8,7 +8,6 @@ todo: - limit clauses with left outer joins ? - see add_limited_ids_condition - select_limited_ids_list -- count queries - extract adapters - arbitrary sql relationships - consider this code from has_many: diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index d5ec9d43bd..e5ca49c284 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -24,17 +24,9 @@ module Arel end def prefix_for(attribute) - if relation1[attribute] && !relation2[attribute] - externalize(relation1).prefix_for(attribute) - elsif relation2[attribute] && !relation1[attribute] - externalize(relation2).prefix_for(attribute) - else - if (attribute % relation1[attribute]).size < (attribute % relation2[attribute]).size - externalize(relation1).prefix_for(attribute) - else - externalize(relation2).prefix_for(attribute) - end - end + externalize([relation1[attribute], relation2[attribute]].select { |a| a =~ attribute }.min do |a1, a2| + (attribute % a1).size <=> (attribute % a2).size + end.relation).prefix_for(attribute) end # TESTME: Not sure which scenario needs this method, was driven by failing tests in ActiveRecord diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index c8602d22e3..3bd40e8f6b 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -139,7 +139,9 @@ module Arel end def attribute_for_attribute(attribute) - attributes.detect { |a| a =~ attribute } + attributes.select { |a| a =~ attribute }.min do |a1, a2| + (attribute % a1).size <=> (attribute % a2).size + end end end include AttributeAccessors diff --git a/spec/arel/unit/relations/join_spec.rb b/spec/arel/unit/relations/join_spec.rb index f9d0d9561e..12b7d7a593 100644 --- a/spec/arel/unit/relations/join_spec.rb +++ b/spec/arel/unit/relations/join_spec.rb @@ -145,14 +145,31 @@ module Arel end describe 'when joining aliased relations' do - it 'aliases the table and attributes properly' do - aliased_relation = @relation1.as(:alias) - @relation1.join(aliased_relation).on(@relation1[:id].eq(aliased_relation[:id])).to_sql.should be_like(" - SELECT `users`.`id`, `users`.`name`, `alias`.`id`, `alias`.`name` - FROM `users` - INNER JOIN `users` AS `alias` - ON `users`.`id` = `alias`.`id` - ") + before do + @aliased_relation = @relation1.as(:alias) + end + + describe '#to_sql' do + it 'aliases the table and attributes properly' do + @relation1.join(@aliased_relation).on(@relation1[:id].eq(@aliased_relation[:id])).to_sql.should be_like(" + SELECT `users`.`id`, `users`.`name`, `alias`.`id`, `alias`.`name` + FROM `users` + INNER JOIN `users` AS `alias` + ON `users`.`id` = `alias`.`id` + ") + end + end + + describe 'when joining the same relation to itself' do + describe '[]' do + describe 'when given an attribute belonging to both sub-relations' do + it '' do + relation = @relation1.join(@aliased_relation).on(@relation1[:id].eq(@aliased_relation[:id])) + relation[@relation1[:id]].ancestor.should == @relation1[:id] + relation[@aliased_relation[:id]].ancestor.should == @aliased_relation[:id] + end + end + end end end |