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 = OSM::API.new.get_xml_doc
49 doc.root << to_xml_node()
54 el1 = XML::Node.new 'way'
55 el1['id'] = self.id.to_s
56 el1['visible'] = self.visible.to_s
57 el1['timestamp'] = self.timestamp.xmlschema
58 el1['user'] = self.user.display_name if self.user.data_public?
59 # make sure segments are output in sequence_id order
\r
60 ordered_segments = []
\r
61 self.way_segments.each do |seg|
\r
62 ordered_segments[seg.sequence_id] = seg.segment_id.to_s
64 ordered_segments.each do |seg_id|
\r
65 e = XML::Node.new 'seg'
\r
70 self.way_tags.each do |tag|
71 e = XML::Node.new 'tag'
83 self.way_segments.each do |seg|
84 @segs += [seg.segment_id]
93 self.way_tags.each do |tag|
109 @segs = Array.new unless @segs
113 def add_tag_keyval(k, v)
114 @tags = Hash.new unless @tags
118 def save_with_history
123 WayTag.delete_all(['id = ?', self.id])
125 self.tags.each do |k,v|
133 WaySegment.delete_all(['id = ?', self.id])
136 self.segs.each do |n|
145 old_way = OldWay.from_way(self)
146 old_way.timestamp = t
147 old_way.save_with_dependencies
150 def preconditions_ok?
151 self.segs.each do |n|
152 segment = Segment.find(n)
153 unless segment and segment.visible and segment.preconditions_ok?