1 class Way < ActiveRecord::Base
6 has_many :way_segments, :foreign_key => 'id', :order => 'sequence_id'
7 has_many :way_tags, :foreign_key => 'id'
9 has_many :old_ways, :foreign_key => 'id', :order => 'version'
11 set_table_name 'current_ways'
13 def self.from_xml(xml, create=false)
21 doc.find('//osm/way').each do |pt|
22 if !create and pt['id'] != '0'
23 way.id = pt['id'].to_i
27 way.timestamp = Time.now
31 way.timestamp = Time.parse(pt['timestamp'])
35 pt.find('tag').each do |tag|
36 way.add_tag_keyval(tag['k'], tag['v'])
39 pt.find('seg').each do |seg|
40 way.add_seg_num(seg['id'])
51 doc = OSM::API.new.get_xml_doc
52 doc.root << to_xml_node()
56 def to_xml_node(visible_segments = nil, user_display_name_cache = nil)
57 el1 = XML::Node.new 'way'
58 el1['id'] = self.id.to_s
59 el1['visible'] = self.visible.to_s
60 el1['timestamp'] = self.timestamp.xmlschema
62 user_display_name_cache = {} if user_display_name_cache.nil?
64 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
65 # use the cache if available
66 elsif self.user.data_public?
67 user_display_name_cache[self.user_id] = self.user.display_name
69 user_display_name_cache[self.user_id] = nil
72 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
74 # make sure segments are output in sequence_id order
76 self.way_segments.each do |seg|
78 # if there is a list of visible segments then use that to weed out deleted segments
79 if visible_segments[seg.segment_id]
80 ordered_segments[seg.sequence_id] = seg.segment_id.to_s
83 # otherwise, manually go to the db to check things
84 if seg.segment.visible? and seg.segment.from_node.visible? and seg.segment.to_node.visible?
85 ordered_segments[seg.sequence_id] = seg.segment_id.to_s
90 ordered_segments.each do |seg_id|
91 if seg_id and seg_id != '0'
92 e = XML::Node.new 'seg'
98 self.way_tags.each do |tag|
99 e = XML::Node.new 'tag'
110 self.way_segments.each do |seg|
111 @segs += [seg.segment_id]
120 self.way_tags.each do |tag|
136 @segs = Array.new unless @segs
140 def add_tag_keyval(k, v)
141 @tags = Hash.new unless @tags
145 def save_with_history
154 WayTag.delete_all(['id = ?', self.id])
166 WaySegment.delete_all(['id = ?', self.id])
178 old_way = OldWay.from_way(self)
179 old_way.timestamp = t
180 old_way.save_with_dependencies!
189 def preconditions_ok?
190 return false if self.segs.empty?
191 self.segs.each do |n|
192 segment = Segment.find(:first, :conditions => ["id = ?", n])
193 unless segment and segment.visible and segment.preconditions_ok?