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
43 print "noes "+ ex.to_s + "\n"
57 self.changeset_tags.each do |tag|
68 def add_tag_keyval(k, v)
69 @tags = Hash.new unless @tags
76 Changeset.transaction do
77 # fixme update modified_at time?
78 # FIXME there is no modified_at time, should it be added
82 ChangesetTag.transaction do
84 ChangesetTag.delete_all(['id = ?', self.id])
87 tag = ChangesetTag.new
97 doc = OSM::API.new.get_xml_doc
98 doc.root << to_xml_node()
102 def to_xml_node(user_display_name_cache = nil)
103 el1 = XML::Node.new 'changeset'
104 el1['id'] = self.id.to_s
106 user_display_name_cache = {} if user_display_name_cache.nil?
108 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
109 # use the cache if available
110 elsif self.user.data_public?
111 user_display_name_cache[self.user_id] = self.user.display_name
113 user_display_name_cache[self.user_id] = nil
116 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
118 self.tags.each do |k,v|
119 el2 = XML::Node.new('tag')
125 el1['created_at'] = self.created_at.xmlschema
126 el1['open'] = self.open.to_s
128 # FIXME FIXME FIXME: This does not include changes yet! There is
129 # currently no changeset_id column in the tables as far as I can tell,
130 # so this is just a scaffold to build on, not a complete to_xml