]> git.openstreetmap.org Git - rails.git/blobdiff - app/models/relation.rb
Eager load nodes and node tags for the way when doing a getway call.
[rails.git] / app / models / relation.rb
index 464d1646cdedbec5523624fbabba614e2c46855b..d2602f497270c455d3d3a4b44062670eaac689bd 100644 (file)
@@ -87,7 +87,7 @@ class Relation < ActiveRecord::Base
     return doc
   end
 
-  def to_xml_node(user_display_name_cache = nil)
+  def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
     el1 = XML::Node.new 'relation'
     el1['id'] = self.id.to_s
     el1['visible'] = self.visible.to_s
@@ -95,19 +95,25 @@ class Relation < ActiveRecord::Base
     el1['version'] = self.version.to_s
     el1['changeset'] = self.changeset_id.to_s
 
-    user_display_name_cache = {} if user_display_name_cache.nil?
-    
-    if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
+    if changeset_cache.key?(self.changeset_id)
+      # use the cache if available
+    else
+      changeset_cache[self.changeset_id] = self.changeset.user_id
+    end
+
+    user_id = changeset_cache[self.changeset_id]
+
+    if user_display_name_cache.key?(user_id)
       # use the cache if available
     elsif self.changeset.user.data_public?
-      user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
+      user_display_name_cache[user_id] = self.changeset.user.display_name
     else
-      user_display_name_cache[self.changeset.user_id] = nil
+      user_display_name_cache[user_id] = nil
     end
 
-    if not user_display_name_cache[self.changeset.user_id].nil?
-      el1['user'] = user_display_name_cache[self.changeset.user_id]
-      el1['uid'] = self.changeset.user_id.to_s
+    if not user_display_name_cache[user_id].nil?
+      el1['user'] = user_display_name_cache[user_id]
+      el1['uid'] = user_id.to_s
     end
 
     self.relation_members.each do |member|
@@ -227,7 +233,7 @@ class Relation < ActiveRecord::Base
 
   def delete_with_history!(new_relation, user)
     unless self.visible
-      raise OSM::APIAlreadyDeletedError.new
+      raise OSM::APIAlreadyDeletedError.new("relation", new_relation.id)
     end
 
     # need to start the transaction here, so that the database can 
@@ -236,9 +242,9 @@ 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 ])
-        raise OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is a used in another relation")
-      end
+      rel = 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 relation #{rel.id}.") unless rel.nil?
+
       self.changeset_id = new_relation.changeset_id
       self.tags = {}
       self.members = []
@@ -249,8 +255,8 @@ class Relation < ActiveRecord::Base
 
   def update_from(new_relation, user)
     check_consistency(self, new_relation, user)
-    if !new_relation.preconditions_ok?
-      raise OSM::APIPreconditionFailedError.new
+    unless new_relation.preconditions_ok?
+      raise OSM::APIPreconditionFailedError.new("Cannot update relation #{self.id}: data or member data is invalid.")
     end
     self.changeset_id = new_relation.changeset_id
     self.changeset = new_relation.changeset
@@ -262,8 +268,8 @@ class Relation < ActiveRecord::Base
   
   def create_with_history(user)
     check_create_consistency(self, user)
-    if !self.preconditions_ok?
-      raise OSM::APIPreconditionFailedError.new
+    unless self.preconditions_ok?
+      raise OSM::APIPreconditionFailedError.new("Cannot create relation: data or member data is invalid.")
     end
     self.version = 0
     self.visible = true
@@ -362,7 +368,6 @@ class Relation < ActiveRecord::Base
       tags_changed |= (not tags.empty?)
       RelationTag.delete_all(:id => self.id)
       self.tags.each do |k,v|
-        logger.info "TAG added: #{k} -> #{v}"
         tag = RelationTag.new
         tag.k = k
         tag.v = v