X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/9ff7ea8d71dedb5b29f6ff0b007568b3e699b196..bf31791f65fee149b660c1395d59204309f906fa:/app/models/relation.rb diff --git a/app/models/relation.rb b/app/models/relation.rb index 2032e6770..d2602f497 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -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 @@ -249,7 +255,7 @@ class Relation < ActiveRecord::Base def update_from(new_relation, user) check_consistency(self, new_relation, user) - unless preconditions_ok? + 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 @@ -338,7 +344,7 @@ class Relation < ActiveRecord::Base self.timestamp = t self.save! - tags = self.tags + tags = self.tags.clone self.relation_tags.each do |old_tag| key = old_tag.k # if we can match the tags we currently have to the list @@ -346,11 +352,7 @@ class Relation < ActiveRecord::Base # if any are different then set the flag and do the DB # update. if tags.has_key? key - # rails 2.1 dirty handling should take care of making this - # somewhat efficient... hopefully... - old_tag.v = tags[key] - tags_changed |= old_tag.changed? - old_tag.save! + tags_changed |= (old_tag.v != tags[key]) # remove from the map, so that we can expect an empty map # at the end if there are no new tags @@ -359,13 +361,13 @@ class Relation < ActiveRecord::Base else # this means a tag was deleted tags_changed = true - RelationTag.delete_all ['id = ? and k = ?', self.id, old_tag.k] end end # if there are left-over tags then they are new and will have to # be added. tags_changed |= (not tags.empty?) - tags.each do |k,v| + RelationTag.delete_all(:id => self.id) + self.tags.each do |k,v| tag = RelationTag.new tag.k = k tag.v = v @@ -373,10 +375,6 @@ class Relation < ActiveRecord::Base tag.save! end - # reload, so that all of the members are accessible in their - # new state. - self.reload - # same pattern as before, but this time we're collecting the # changed members in an array, as the bounding box updates for # elements are per-element, not blanked on/off like for tags.