diff options
-rw-r--r-- | lib/arel/primitives/attribute.rb | 23 | ||||
-rw-r--r-- | lib/arel/relations/compound.rb | 4 | ||||
-rw-r--r-- | lib/arel/relations/recursion.rb | 10 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 2 | ||||
-rw-r--r-- | spec/arel/integration/joins/with_adjacency_spec.rb | 25 | ||||
-rw-r--r-- | spec/arel/unit/primitives/expression_spec.rb | 1 |
6 files changed, 55 insertions, 10 deletions
diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index 9d5b98474f..9fb47d56cf 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -37,16 +37,9 @@ module Arel ancestor == other.ancestor end - module Origin - def original_relation - relation.relation_for(self) - end - - def original_attribute - original_relation[self] - end + def original_relation + relation.relation_for(self) end - include Origin module Transformations def self.included(klass) @@ -76,6 +69,18 @@ module Arel !(history & other.history).empty? end + def descends_from?(other) + history.include?(other) + end + + def root? + relation.root? + end + + def root + @root ||= history.detect(&:root?) + end + def /(other) if other then (history & other.history).size.to_f / Set.new(history + other.history).size else 0 diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index 1459c163cf..6f230c3f57 100644 --- a/lib/arel/relations/compound.rb +++ b/lib/arel/relations/compound.rb @@ -11,6 +11,10 @@ module Arel @attributes ||= relation.attributes.collect { |a| a.bind(self) } end + def selects + @selects ||= relation.selects.collect { |s| s.bind(self) } + end + # XXX def relation_for(attribute) join? && relation.relation_for(attribute) || self[attribute] && self diff --git a/lib/arel/relations/recursion.rb b/lib/arel/relations/recursion.rb index fa4d9969c9..ea8109d87d 100644 --- a/lib/arel/relations/recursion.rb +++ b/lib/arel/relations/recursion.rb @@ -9,9 +9,19 @@ module Arel formatter.table self end + def root? + true + end + def relation_for(attribute) self[attribute] && self end end end + + class Relation + def root? + false + end + end end
\ No newline at end of file diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 884b743f20..25dc75e8ea 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -138,7 +138,7 @@ module Arel # TESTME - added original_attribute because of AR def find_attribute_matching_attribute(attribute) attributes.select { |a| a.match?(attribute) }.max do |a1, a2| - (attribute / a1.original_attribute) <=> (attribute / a2.original_attribute) + (attribute / a1.root) <=> (attribute / a2.root) end end end diff --git a/spec/arel/integration/joins/with_adjacency_spec.rb b/spec/arel/integration/joins/with_adjacency_spec.rb index 74d461319c..56ead0993a 100644 --- a/spec/arel/integration/joins/with_adjacency_spec.rb +++ b/spec/arel/integration/joins/with_adjacency_spec.rb @@ -96,6 +96,31 @@ module Arel relation[@relation1[:id]].ancestor.should == @relation1[:id] relation[@relation2[:id]].ancestor.should == @relation2[:id] end + + describe 'when the left relation is compound' do + it '' do + relation = @relation1 \ + .select(@predicate) \ + .select(@predicate) \ + .join(@relation2) \ + .on(@predicate) + relation[@relation1[:id]].should be_descends_from(@relation1[:id]) + relation[@relation1[:id]].should_not be_descends_from(@relation2[:id]) + end + end + + describe 'when the right relation is compound' do + it '' do + relation = @relation1 \ + .join( \ + @relation2 \ + .select(@predicate) \ + .select(@predicate) \ + .select(@predicate)) \ + .on(@predicate) + relation[@relation2[:id]].should be_descends_from(@relation2[:id]) + end + end end end end diff --git a/spec/arel/unit/primitives/expression_spec.rb b/spec/arel/unit/primitives/expression_spec.rb index 8a990231f6..ace439b952 100644 --- a/spec/arel/unit/primitives/expression_spec.rb +++ b/spec/arel/unit/primitives/expression_spec.rb @@ -14,6 +14,7 @@ module Arel describe '#bind' do it "manufactures an attribute with a rebound relation and self as the ancestor" do + pending derived_relation = @relation.select(@relation[:id].eq(1)) @expression.bind(derived_relation).should == Expression.new(@attribute.bind(derived_relation), "COUNT", nil, @expression) end |