]> git.openstreetmap.org Git - rails.git/blobdiff - app/models/relation.rb
Some improvements to the error messages that are returned by the api.
[rails.git] / app / models / relation.rb
index 2a2ec3dcae843dd10bc8f8c9c7ecdc2583ccea3f..0159c60b875d791ccb7b690ea12a263d436f78d2 100644 (file)
@@ -15,6 +15,14 @@ class Relation < 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 :timestamp,:version,  :changeset_id 
+  validates_uniqueness_of :id
+  validates_inclusion_of :visible, :in => [ true, false ]
+  validates_numericality_of :id, :on => :update, :integer_only => true
+  validates_numericality_of :changeset_id, :version, :integer_only => true
+  validates_associated :changeset
+  
   TYPES = ["node", "way", "relation"]
 
   def self.from_xml(xml, create=false)
@@ -27,7 +35,6 @@ class Relation < ActiveRecord::Base
         return Relation.from_xml_node(pt, create)
       end
     rescue LibXML::XML::Error => ex
-      #return nil
       raise OSM::APIBadXMLError.new("relation", xml, ex.message)
     end
   end
@@ -39,16 +46,18 @@ class Relation < ActiveRecord::Base
       relation.id = pt['id'].to_i
     end
 
-    relation.version = pt['version']
+    raise OSM::APIBadXMLError.new("relation", pt, "You are missing the required changeset in the relation") if pt['changeset'].nil?
     relation.changeset_id = pt['changeset']
 
     if create
       relation.timestamp = Time.now
       relation.visible = true
+      relation.version = 0
     else
       if pt['timestamp']
         relation.timestamp = Time.parse(pt['timestamp'])
       end
+      relation.version = pt['version']
     end
 
     pt.find('tag').each do |tag|
@@ -340,15 +349,15 @@ class Relation < ActiveRecord::Base
   def delete_with_history!(new_relation, user)
     if self.visible
       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 ])
-        raise OSM::APIPreconditionFailedError.new
-      else
-        self.changeset_id = new_relation.changeset_id
-        self.tags = {}
-        self.members = []
-        self.visible = false
-        save_with_history!
+        raise OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is a used in another relation")
       end
+      self.changeset_id = new_relation.changeset_id
+      self.tags = {}
+      self.members = []
+      self.visible = false
+      save_with_history!
     else
       raise OSM::APIAlreadyDeletedError.new
     end