Fix error resolving relation parents
authorTom Hughes <tom@compton.nu>
Wed, 23 Jul 2014 23:23:45 +0000 (00:23 +0100)
committerTom Hughes <tom@compton.nu>
Wed, 23 Jul 2014 23:23:45 +0000 (00:23 +0100)
It's not entirely clear why specifying the foreign key explicitly
fixes this - the value given is what it should default to - but
without this it finds the child relation instead when looking at
the parents of a relation.

Fixes #789

app/models/relation_member.rb
test/models/node_test.rb
test/models/relation_test.rb
test/models/way_test.rb

index aba3851..e5c783f 100644 (file)
@@ -2,6 +2,6 @@ class RelationMember < ActiveRecord::Base
   self.table_name = "current_relation_members"
   self.primary_keys = "relation_id", "sequence_id"
 
-  belongs_to :relation
+  belongs_to :relation, :foreign_key => :relation_id
   belongs_to :member, :polymorphic => true
 end
index 9ad968a..7acc616 100644 (file)
@@ -331,4 +331,31 @@ class NodeTest < ActiveSupport::TestCase
     assert_equal "added in node version 3", tags["testing"]
     assert_equal "modified in node version 4", tags["testing two"]
   end
+
+  def test_containing_relation_members
+    node = current_nodes(:node_used_by_relationship)
+    crm = Node.find(node.id).containing_relation_members.order(:relation_id)
+#    assert_equal 3, crm.size
+    assert_equal 1, crm.first.relation_id
+    assert_equal "Node", crm.first.member_type
+    assert_equal node.id, crm.first.member_id
+    assert_equal 1, crm.first.relation.id
+    assert_equal 2, crm.second.relation_id
+    assert_equal "Node", crm.second.member_type
+    assert_equal node.id, crm.second.member_id
+    assert_equal 2, crm.second.relation.id
+    assert_equal 3, crm.third.relation_id
+    assert_equal "Node", crm.third.member_type
+    assert_equal node.id, crm.third.member_id
+    assert_equal 3, crm.third.relation.id
+  end
+
+  def test_containing_relations
+    node = current_nodes(:node_used_by_relationship)
+    cr = Node.find(node.id).containing_relations.order(:id)
+    assert_equal 3, cr.size
+    assert_equal 1, cr.first.id
+    assert_equal 2, cr.second.id
+    assert_equal 3, cr.third.id
+  end
 end
index 7141f54..5d60ebb 100644 (file)
@@ -144,4 +144,21 @@ class RelationTest < ActiveSupport::TestCase
     assert_equal "added in relation version 3", tags["testing"]
     assert_equal "modified in relation version 4", tags["testing two"]
   end
+
+  def test_containing_relation_members
+    relation = current_relations(:used_relation)
+    crm = Relation.find(relation.id).containing_relation_members.order(:relation_id)
+#    assert_equal 1, crm.size
+    assert_equal 1, crm.first.relation_id
+    assert_equal "Relation", crm.first.member_type
+    assert_equal relation.id, crm.first.member_id
+    assert_equal 1, crm.first.relation.id
+  end
+
+  def test_containing_relations
+    relation = current_relations(:used_relation)
+    cr = Relation.find(relation.id).containing_relations.order(:id)
+    assert_equal 1, cr.size
+    assert_equal 1, cr.first.id
+  end
 end
index eb5baef..de0eb61 100644 (file)
@@ -180,4 +180,21 @@ class WayTest < ActiveSupport::TestCase
     assert_equal "added in way version 3", tags["testing"]
     assert_equal "modified in way version 4", tags["testing two"]
   end
+
+  def test_containing_relation_members
+    way = current_ways(:used_way)
+    crm = Way.find(way.id).containing_relation_members.order(:relation_id)
+#    assert_equal 1, crm.size
+    assert_equal 1, crm.first.relation_id
+    assert_equal "Way", crm.first.member_type
+    assert_equal way.id, crm.first.member_id
+    assert_equal 1, crm.first.relation.id
+  end
+
+  def test_containing_relations
+    way = current_ways(:used_way)
+    cr = Way.find(way.id).containing_relations.order(:id)
+    assert_equal 1, cr.size
+    assert_equal 1, cr.first.id
+  end
 end