]> git.openstreetmap.org Git - rails.git/blobdiff - app/models/way.rb
ensure that uploads that don't supply a lat and lon for a node. Adding related test...
[rails.git] / app / models / way.rb
index 3b3f9243283e64eee1ec27b7d34ac97e2db7d0df..d6aa12af15730b32fa35aa71191c48a515dc95a4 100644 (file)
@@ -4,9 +4,6 @@ class Way < ActiveRecord::Base
   include ConsistencyValidations
 
   set_table_name 'current_ways'
-
-  validates_presence_of :changeset_id, :timestamp
-  validates_inclusion_of :visible, :in => [ true, false ]
   
   belongs_to :changeset
 
@@ -20,6 +17,14 @@ class Way < ActiveRecord::Base
   has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
   has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
 
+  validates_presence_of :id, :on => :update
+  validates_presence_of :changeset_id,:version,  :timestamp
+  validates_uniqueness_of :id
+  validates_inclusion_of :visible, :in => [ true, false ]
+  validates_numericality_of :changeset_id, :version, :integer_only => true
+  validates_numericality_of :id, :on => :update, :integer_only => true
+  validates_associated :changeset
+
   def self.from_xml(xml, create=false)
     begin
       p = XML::Parser.new
@@ -29,8 +34,8 @@ class Way < ActiveRecord::Base
       doc.find('//osm/way').each do |pt|
         return Way.from_xml_node(pt, create)
       end
-    rescue
-      return nil
+    rescue LibXML::XML::Error => ex
+      raise OSM::APIBadXMLError.new("way", xml, ex.message)
     end
   end
 
@@ -177,7 +182,7 @@ class Way < ActiveRecord::Base
 
     # duplicate tags are now forbidden, so we can't allow values
     # in the hash to be overwritten.
-    raise OSM::APIDuplicateTagsError.new if @tags.include? k
+    raise OSM::APIDuplicateTagsError.new("way", self.id, k) if @tags.include? k
 
     @tags[k] = v
   end
@@ -278,8 +283,15 @@ class Way < ActiveRecord::Base
   end
 
   def delete_with_history!(new_way, user)
-    check_consistency(self, new_way, user)
-    if self.visible
+    unless self.visible
+      raise OSM::APIAlreadyDeletedError
+    end
+    
+    # need to start the transaction here, so that the database can 
+    # provide repeatable reads for the used-by checks. this means it
+    # shouldn't be possible to get race conditions.
+    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])
         raise OSM::APIPreconditionFailedError
@@ -290,8 +302,6 @@ class Way < ActiveRecord::Base
         self.visible = false
         self.save_with_history!
       end
-    else
-      raise OSM::APIAlreadyDeletedError
     end
   end