aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/arel/primitives/attribute.rb6
-rw-r--r--lib/arel/relations/relation.rb3
-rw-r--r--spec/arel/integration/joins/with_adjacency_spec.rb7
-rw-r--r--spec/matchers/disambiguate_attributes.rb28
4 files changed, 42 insertions, 2 deletions
diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb
index 9fb47d56cf..1de2bf7cb0 100644
--- a/lib/arel/primitives/attribute.rb
+++ b/lib/arel/primitives/attribute.rb
@@ -39,6 +39,11 @@ module Arel
def original_relation
relation.relation_for(self)
+ # root.relation
+ end
+
+ def original_attribute
+ original_relation[self]
end
module Transformations
@@ -85,6 +90,7 @@ module Arel
if other then (history & other.history).size.to_f / Set.new(history + other.history).size
else 0
end
+ # 1 / (history.index(other) || -1)
end
end
include Congruence
diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb
index 25dc75e8ea..490f545637 100644
--- a/lib/arel/relations/relation.rb
+++ b/lib/arel/relations/relation.rb
@@ -135,10 +135,9 @@ module Arel
attributes.detect { |a| a.named?(name) }
end
- # 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.root) <=> (attribute / a2.root)
+ (a1.original_attribute / attribute) <=> (a2.original_attribute / attribute)
end
end
end
diff --git a/spec/arel/integration/joins/with_adjacency_spec.rb b/spec/arel/integration/joins/with_adjacency_spec.rb
index 4ffdf1f64f..ab63fecb46 100644
--- a/spec/arel/integration/joins/with_adjacency_spec.rb
+++ b/spec/arel/integration/joins/with_adjacency_spec.rb
@@ -107,6 +107,13 @@ module Arel
.on(@predicate) \
.should disambiguate_attributes(@relation1[:id], @relation2[:id])
end
+
+ it '' do
+ r0 = @relation1.select(@predicate)
+ r1 = r0.alias
+ r = r0.join(r1).on(@predicate)
+ r.should disambiguate_attributes(r0[:id], r1[:id])
+ end
end
describe 'when the right relation is extremely compound' do
diff --git a/spec/matchers/disambiguate_attributes.rb b/spec/matchers/disambiguate_attributes.rb
new file mode 100644
index 0000000000..bee7d22b0c
--- /dev/null
+++ b/spec/matchers/disambiguate_attributes.rb
@@ -0,0 +1,28 @@
+module DisambiguateAttributesMatcher
+ class DisambiguateAttributes
+ def initialize(attributes)
+ @attributes = attributes
+ end
+
+ def matches?(actual)
+ @actual = actual
+ attribute1, attribute2 = @attributes
+ @actual[attribute1].descends_from?(attribute1) &&
+ !@actual[attribute1].descends_from?(attribute2) &&
+ @actual[attribute2].descends_from?(attribute2)
+ end
+
+ def failure_message
+ ""
+ # "expected #{@actual} to disambiguate its attributes"
+ end
+
+ def negative_failure_message
+ "expected #{@actual} to not disambiguate its attributes"
+ end
+ end
+
+ def disambiguate_attributes(*attributes)
+ DisambiguateAttributes.new(attributes)
+ end
+end \ No newline at end of file