1 class Way < ActiveRecord::Base
6 has_many :way_segments, :foreign_key => 'id'
7 has_many :way_tags, :foreign_key => 'id'
9 has_many :old_ways, :foreign_key => :id
11 set_table_name 'current_ways'
13 def self.from_xml(xml, create=false)
20 doc.find('//osm/way').each do |pt|
21 if !create and pt['id'] != '0'
22 way.id = pt['id'].to_i
26 way.timestamp = Time.now
30 way.timestamp = Time.parse(pt['timestamp'])
34 pt.find('tag').each do |tag|
35 way.add_tag_keyval(tag['k'], tag['v'])
38 pt.find('seg').each do |seg|
39 way.add_seg_num(seg['id'])
48 doc = XML::Document.new
49 doc.encoding = 'UTF-8'
50 root = XML::Node.new 'osm'
51 root['version'] = '0.4'
52 root['generator'] = 'OpenStreetMap server'
61 el1 = XML::Node.new 'way'
62 el1['id'] = self.id.to_s
63 el1['visible'] = self.visible.to_s
64 el1['timestamp'] = self.timestamp.xmlschema
66 self.way_segments.each do |seg| # FIXME need to make sure they come back in the right order
67 e = XML::Node.new 'seg'
68 e['id'] = seg.segment_id.to_s
72 self.way_tags.each do |tag|
73 e = XML::Node.new 'tag'
83 @segs = Array.new unless @segs
88 @tags = Hash.new unless @tags
101 @segs = Array.new unless @segs
105 def add_tag_keyval(k, v)
106 @tags = Hash.new unless @tags
110 def save_with_history
115 WayTag.delete_all(['id = ?', self.id])
117 self.tags.each do |k,v|
125 WaySegment.delete_all(['id = ?', self.id])
128 self.segs.each do |n|
137 old_way = OldWay.from_way(self)
138 old_way.timestamp = t
139 old_way.save_with_dependencies
142 def preconditions_ok?
143 self.segs.each do |n|
144 segment = Segment.find(n)
145 unless segment and segment.visible and segment.preconditions_ok?