Railsify the relation member model, type attribute, by putting it into class case...
authorShaun McDonald <shaun@shaunmcdonald.me.uk>
Wed, 15 Apr 2009 14:49:21 +0000 (14:49 +0000)
committerShaun McDonald <shaun@shaunmcdonald.me.uk>
Wed, 15 Apr 2009 14:49:21 +0000 (14:49 +0000)
app/controllers/relation_controller.rb
app/models/node.rb
app/models/old_relation.rb
app/models/relation.rb
app/models/relation_member.rb
app/models/way.rb
lib/migrate.rb
test/fixtures/current_relation_members.yml
test/fixtures/relation_members.yml

index 93573b9..ec86bb6 100644 (file)
@@ -97,12 +97,12 @@ class RelationController < ApplicationController
         # first collect nodes, ways, and relations referenced by this relation.
         
         ways = Way.find_by_sql("select w.* from current_ways w,current_relation_members rm where "+
-            "rm.member_type='way' and rm.member_id=w.id and rm.id=#{relation.id}");
+            "rm.member_type='Way' and rm.member_id=w.id and rm.id=#{relation.id}");
         nodes = Node.find_by_sql("select n.* from current_nodes n,current_relation_members rm where "+
-            "rm.member_type='node' and rm.member_id=n.id and rm.id=#{relation.id}");
+            "rm.member_type='Node' and rm.member_id=n.id and rm.id=#{relation.id}");
         # note query is built to exclude self just in case.
         relations = Relation.find_by_sql("select r.* from current_relations r,current_relation_members rm where "+
-            "rm.member_type='relation' and rm.member_id=r.id and rm.id=#{relation.id} and r.id<>rm.id");
+            "rm.member_type='Relation' and rm.member_id=r.id and rm.id=#{relation.id} and r.id<>rm.id");
 
         # now additionally collect nodes referenced by ways. Note how we recursively 
         # evaluate ways but NOT relations.
@@ -172,13 +172,13 @@ class RelationController < ApplicationController
   end
 
   def relations_for_way
-    relations_for_object("way")
+    relations_for_object("Way")
   end
   def relations_for_node
-    relations_for_object("node")
+    relations_for_object("Node")
   end
   def relations_for_relation
-    relations_for_object("relation")
+    relations_for_object("Relation")
   end
 
   def relations_for_object(objtype)
index c45057e..1392fc6 100644 (file)
@@ -133,7 +133,7 @@ class Node < ActiveRecord::Base
       check_consistency(self, new_node, user)
       if WayNode.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_nodes.id", :conditions => [ "current_ways.visible = ? AND current_way_nodes.node_id = ?", true, self.id ])
         raise OSM::APIPreconditionFailedError.new
-      elsif RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = ? AND member_type='node' and member_id=? ", true, self.id])
+      elsif RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = ? AND member_type='Node' and member_id=? ", true, self.id])
         raise OSM::APIPreconditionFailedError.new
       else
         self.changeset_id = new_node.changeset_id
index e2a6505..b2fdf92 100644 (file)
@@ -41,7 +41,7 @@ class OldRelation < ActiveRecord::Base
     self.members.each_with_index do |m,i|
       member = OldRelationMember.new
       member.id = [self.id, self.version, i]
-      member.member_type = m[0]
+      member.member_type = m[0].classify
       member.member_id = m[1]
       member.member_role = m[2]
       member.save!
@@ -108,7 +108,7 @@ class OldRelation < ActiveRecord::Base
     
     self.old_members.each do |member|
       e = XML::Node.new 'member'
-      e['type'] = member.member_type.to_s
+      e['type'] = member.member_type.to_s.downcase
       e['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
       e['role'] = member.member_role.to_s
       el1 << e
index a8789bc..36d6943 100644 (file)
@@ -74,7 +74,7 @@ class Relation < ActiveRecord::Base
       #member_role
       member['role'] ||= "" # Allow  the upload to not include this, in which case we default to an empty string.
       logger.debug member['role']
-      relation.add_member(member['type'], member['ref'], member['role'])
+      relation.add_member(member['type'].classify, member['ref'], member['role'])
     end
     raise OSM::APIBadUserInput.new("Some bad xml in relation") if relation.nil?
 
@@ -125,7 +125,7 @@ class Relation < ActiveRecord::Base
       #end
       if p
         e = XML::Node.new 'member'
-        e['type'] = member.member_type
+        e['type'] = member.member_type.downcase
         e['ref'] = member.member_id.to_s 
         e['role'] = member.member_role
         el1 << e
@@ -145,7 +145,7 @@ class Relation < ActiveRecord::Base
     if ids.empty?
       return []
     else
-      self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'node' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
+      self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'Node' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
         return self.find(:all, options)
       end
     end
@@ -155,7 +155,7 @@ class Relation < ActiveRecord::Base
     if ids.empty?
       return []
     else
-      self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'way' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
+      self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'Way' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
         return self.find(:all, options)
       end
     end
@@ -165,7 +165,7 @@ class Relation < ActiveRecord::Base
     if ids.empty?
       return []
     else
-      self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'relation' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
+      self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'Relation' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
         return self.find(:all, options)
       end
     end
@@ -236,7 +236,7 @@ class Relation < ActiveRecord::Base
     Relation.transaction do
       check_consistency(self, new_relation, user)
       # This will check to see if this relation is used by another relation
-      if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = ? AND member_type='relation' and member_id=? ", true, self.id ])
+      if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = ? AND member_type='Relation' and member_id=? ", true, self.id ])
         raise OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is a used in another relation")
       end
       self.changeset_id = new_relation.changeset_id
@@ -281,13 +281,12 @@ class Relation < ActiveRecord::Base
     elements = { :node => Hash.new, :way => Hash.new, :relation => Hash.new }
     self.members.each do |m|
       # find the hash for the element type or die
-      hash = elements[m[0].to_sym] or return false
-
+      logger.debug m[0]
+      hash = elements[m[0].downcase.to_sym] or return false
       # unless its in the cache already
       unless hash.key? m[1]
         # use reflection to look up the appropriate class
         model = Kernel.const_get(m[0].capitalize)
-
         # get the element with that ID
         element = model.find(m[1])
 
@@ -317,7 +316,7 @@ class Relation < ActiveRecord::Base
     self.members.map! do |type, id, role|
       old_id = id.to_i
       if old_id < 0
-        new_id = id_map[type.to_sym][old_id]
+        new_id = id_map[type.downcase.to_sym][old_id]
         raise "invalid placeholder" if new_id.nil?
         [type, new_id, role]
       else
@@ -435,14 +434,16 @@ class Relation < ActiveRecord::Base
         # FIXME: check for tag changes along with element deletions and
         # make sure that the deleted element's bounding box is hit.
         self.members.each do |type, id, role|
-          if type != "relation"
+          if type != "Relation"
             update_changeset_element(type, id)
           end
         end
       else
         # add only changed members to the changeset
         changed_members.each do |id, type|
-          update_changeset_element(type, id)
+          if type != "Relation"
+            update_changeset_element(type, id)
+          end
         end
       end
 
index f3033d1..b385dd6 100644 (file)
@@ -6,15 +6,15 @@ class RelationMember < ActiveRecord::Base
   belongs_to :relation, :foreign_key => :id
 
   def after_find
-    self[:member_class] = self.member_type.capitalize
+    self[:member_class] = self.member_type.classify
   end
 
   def after_initialize
-    self[:member_class] = self.member_type.capitalize unless self.member_type.nil?
+    self[:member_class] = self.member_type.classify unless self.member_type.nil?
   end
 
   def before_save
-    self.member_type = self[:member_class].downcase
+    self.member_type = self[:member_class].classify
   end
 
   def member_type=(type)
index 6e4f30d..e134d69 100644 (file)
@@ -246,7 +246,7 @@ class Way < ActiveRecord::Base
     Way.transaction do
       check_consistency(self, new_way, user)
       if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
-                             :conditions => [ "visible = ? AND member_type='way' and member_id=? ", true, self.id])
+                             :conditions => [ "visible = ? AND member_type='Way' and member_id=? ", true, self.id])
         raise OSM::APIPreconditionFailedError.new("You need to make sure that this way is not a member of a relation.")
       else
         self.changeset_id = new_way.changeset_id
index d43982e..488c6d3 100644 (file)
@@ -92,7 +92,7 @@ module ActiveRecord
       end
 
       def alter_column_nwr_enum (table_name, column)
-        execute "alter table #{table_name} change column #{column} #{column} enum('node','way','relation');"
+        execute "alter table #{table_name} change column #{column} #{column} enum('Node','Way','Relation');"
       end
 
       def alter_primary_key(table_name, new_columns)
@@ -141,7 +141,7 @@ module ActiveRecord
       def alter_column_nwr_enum (table_name, column)
         response = select_one("select count(*) as count from pg_type where typname = 'nwr_enum'")
         if response['count'] == "0" #yep, as a string
-          execute "create type nwr_enum as ENUM ('node', 'way', 'relation')"
+          execute "create type nwr_enum as ENUM ('Node', 'Way', 'Relation')"
         end
         execute        "alter table #{table_name} drop #{column}"
         execute "alter table #{table_name} add #{column} nwr_enum"
index 5696a36..f055371 100644 (file)
@@ -1,29 +1,29 @@
 t1:
   id: 1
   member_role: "some"
-  member_type: "way"
+  member_type: "Way"
   member_id: 3
 
 t2:
   id: 1
   member_role: "some"
-  member_type: "node"
+  member_type: "Node"
   member_id: 5
 
 t3:
   id: 1
   member_role: "some"
-  member_type: "relation"
+  member_type: "Relation"
   member_id: 3
 
 t4:
   id: 3
   member_role: "some"
-  member_type: "node"
+  member_type: "Node"
   member_id: 5
 
 t5:
   id: 2
   member_role: "some"
-  member_type: "node"
+  member_type: "Node"
   member_id: 5
index 27e8b53..5a19bf3 100644 (file)
@@ -1,24 +1,24 @@
 t1:
   id: 1
   member_role: "some"
-  member_type: "way"
+  member_type: "Way"
   member_id: 3
   version: 1
 t2:
   id: 1
   member_role: "some"
-  member_type: "node"
+  member_type: "Node"
   member_id: 5
   version: 1
 t3:
   id: 1
   member_role: "some"
-  member_type: "relation"
+  member_type: "Relation"
   member_id: 3
   version: 1
 t4:
   id: 3
   member_role: "some"
-  member_type: "node"
+  member_type: "Node"
   member_id: 5
   version: 1