1 class Way < ActiveRecord::Base
6 has_many :way_segments, :foreign_key => 'id'
7 has_many :way_tags, :foreign_key => 'id'
9 set_table_name 'current_ways'
11 def self.from_xml(xml, create=false)
18 doc.find('//osm/way').each do |pt|
19 unless create and pt['id'] == '0'
20 way.id = pt['id'].to_i
24 way.timestamp = Time.now
28 way.timestamp = Time.parse(pt['timestamp'])
32 pt.find('tag').each do |tag|
33 way.add_tag_keyval(tag['k'], tag['v'])
36 pt.find('seg').each do |seg|
37 way.add_seg_num(seg['id'])
46 doc = XML::Document.new
47 doc.encoding = 'UTF-8'
48 root = XML::Node.new 'osm'
49 root['version'] = '0.4'
50 root['generator'] = 'OpenStreetMap server'
59 el1 = XML::Node.new 'way'
60 el1['id'] = self.id.to_s
61 el1['visible'] = self.visible.to_s
62 el1['timestamp'] = self.timestamp.xmlschema
64 self.way_segments.each do |seg| # FIXME need to make sure they come back in the right order
65 e = XML::Node.new 'seg'
66 e['id'] = seg.segment_id.to_s
70 self.way_tags.each do |tag|
71 e = XML::Node.new 'tag'
81 @segs = Array.new unless @segs
86 @tags = Hash.new unless @tags
99 @segs = Array.new unless @segs
103 def add_tag_keyval(k, v)
104 @tags = Hash.new unless @tags
108 def save_with_history
113 WayTag.delete_all(['id = ?', self.id])
115 self.tags.each do |k,v|
123 WaySegment.delete_all(['id = ?', self.id])
126 self.segs.each do |n|
135 old_way = OldWay.from_way(self)
136 old_way.timestamp = t
137 old_way.save_with_dependencies