1 class Changeset < ActiveRecord::Base
6 has_many :changeset_tags, :foreign_key => 'id'
13 has_many :old_relations
15 validates_presence_of :user_id, :created_at
16 validates_inclusion_of :open, :in => [ true, false ]
18 # Use a method like this, so that we can easily change how we
19 # determine whether a changeset is open, without breaking code in at
25 def self.from_xml(xml, create=false)
33 doc.find('//osm/changeset').each do |pt|
35 cs.created_at = Time.now
38 pt.find('tag').each do |tag|
39 cs.add_tag_keyval(tag['k'], tag['v'])
42 rescue Exception => ex
56 self.changeset_tags.each do |tag|
67 def add_tag_keyval(k, v)
68 @tags = Hash.new unless @tags
75 Changeset.transaction do
76 # fixme update modified_at time?
77 # FIXME there is no modified_at time, should it be added
81 ChangesetTag.transaction do
83 ChangesetTag.delete_all(['id = ?', self.id])
86 tag = ChangesetTag.new
96 doc = OSM::API.new.get_xml_doc
97 doc.root << to_xml_node()
101 def to_xml_node(user_display_name_cache = nil)
102 el1 = XML::Node.new 'changeset'
103 el1['id'] = self.id.to_s
105 user_display_name_cache = {} if user_display_name_cache.nil?
107 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
108 # use the cache if available
109 elsif self.user.data_public?
110 user_display_name_cache[self.user_id] = self.user.display_name
112 user_display_name_cache[self.user_id] = nil
115 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
117 self.tags.each do |k,v|
118 el2 = XML::Node.new('tag')
124 el1['created_at'] = self.created_at.xmlschema
125 el1['open'] = self.open.to_s
127 # FIXME FIXME FIXME: This does not include changes yet! There is
128 # currently no changeset_id column in the tables as far as I can tell,
129 # so this is just a scaffold to build on, not a complete to_xml