+
+ ##
+ # if any members are referenced by placeholder IDs (i.e: negative) then
+ # this calling this method will fix them using the map from placeholders
+ # to IDs +id_map+.
+ def fix_placeholders!(id_map)
+ 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]
+ raise "invalid placeholder" if new_id.nil?
+ [type, new_id, role]
+ else
+ [type, id, role]
+ end
+ end
+ end
+
+ private
+
+ def save_with_history!
+ Relation.transaction do
+ # have to be a little bit clever here - to detect if any tags
+ # changed then we have to monitor their before and after state.
+ tags_changed = false
+
+ t = Time.now.getutc
+ self.version += 1
+ self.timestamp = t
+ self.save!
+
+ tags = self.tags
+ self.relation_tags.each do |old_tag|
+ key = old_tag.k
+ # if we can match the tags we currently have to the list
+ # of old tags, then we never set the tags_changed flag. but
+ # 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!
+
+ # remove from the map, so that we can expect an empty map
+ # at the end if there are no new tags
+ tags.delete key
+
+ 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|
+ tag = RelationTag.new
+ tag.k = k
+ tag.v = v
+ tag.id = self.id
+ 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.
+ changed_members = Array.new
+ members = Hash.new
+ self.members.each do |m|
+ # should be: h[[m.id, m.type]] = m.role, but someone prefers arrays
+ members[[m[1], m[0]]] = m[2]
+ end
+ relation_members.each do |old_member|
+ key = [old_member.member_id.to_s, old_member.member_type]
+ if members.has_key? key
+ members.delete key
+ else
+ changed_members << key
+ end
+ end
+ # any remaining members must be new additions
+ changed_members += members.keys
+
+ # update the members. first delete all the old members, as the new
+ # members may be in a different order and i don't feel like implementing
+ # a longest common subsequence algorithm to optimise this.
+ members = self.members
+ RelationMember.delete_all(:id => self.id)
+ members.each_with_index do |m,i|
+ mem = RelationMember.new
+ mem.id = [self.id, i]
+ mem.member_type = m[0]
+ mem.member_id = m[1]
+ mem.member_role = m[2]
+ mem.save!
+ end
+
+ old_relation = OldRelation.from_relation(self)
+ old_relation.timestamp = t
+ old_relation.save_with_dependencies!
+
+ # update the bbox of the changeset and save it too.
+ # discussion on the mailing list gave the following definition for
+ # the bounding box update procedure of a relation:
+ #
+ # adding or removing nodes or ways from a relation causes them to be
+ # added to the changeset bounding box. adding a relation member or
+ # changing tag values causes all node and way members to be added to the
+ # bounding box. this is similar to how the map call does things and is
+ # reasonable on the assumption that adding or removing members doesn't
+ # materially change the rest of the relation.
+ any_relations =
+ changed_members.collect { |id,type| type == "relation" }.
+ inject(false) { |b,s| b or s }
+
+ if tags_changed or any_relations
+ # add all non-relation bounding boxes to the changeset
+ # 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"
+ 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)
+ end
+ end
+
+ # tell the changeset we updated one element only
+ changeset.add_changes! 1
+
+ # save the (maybe updated) changeset bounding box
+ changeset.save!
+ end
+ end
+