1 class Way < ActiveRecord::Base
4 set_table_name 'current_ways'
8 has_many :old_ways, :foreign_key => 'id', :order => 'version'
10 has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
11 has_many :nodes, :through => :way_nodes, :order => 'sequence_id'
13 has_many :way_tags, :foreign_key => 'id'
15 has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
16 has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
18 def self.from_xml(xml, create=false)
26 doc.find('//osm/way').each do |pt|
27 if !create and pt['id'] != '0'
28 way.id = pt['id'].to_i
32 way.timestamp = Time.now
36 way.timestamp = Time.parse(pt['timestamp'])
40 pt.find('tag').each do |tag|
41 way.add_tag_keyval(tag['k'], tag['v'])
44 pt.find('nd').each do |nd|
45 way.add_nd_num(nd['ref'])
55 # Find a way given it's ID, and in a single SQL call also grab its nodes
58 # You can't pull in all the tags too unless we put a sequence_id on the way_tags table and have a multipart key
59 def self.find_eager(id)
60 way = Way.find(id, :include => {:way_nodes => :node})
61 #If waytag had a multipart key that was real, you could do this:
62 #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
65 # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
67 doc = OSM::API.new.get_xml_doc
68 doc.root << to_xml_node()
72 def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
73 el1 = XML::Node.new 'way'
74 el1['id'] = self.id.to_s
75 el1['visible'] = self.visible.to_s
76 el1['timestamp'] = self.timestamp.xmlschema
78 user_display_name_cache = {} if user_display_name_cache.nil?
80 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
81 # use the cache if available
82 elsif self.user.data_public?
83 user_display_name_cache[self.user_id] = self.user.display_name
85 user_display_name_cache[self.user_id] = nil
88 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
90 # make sure nodes are output in sequence_id order
92 self.way_nodes.each do |nd|
94 # if there is a list of visible nodes then use that to weed out deleted nodes
95 if visible_nodes[nd.node_id]
96 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
99 # otherwise, manually go to the db to check things
100 if nd.node.visible? and nd.node.visible?
101 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
106 ordered_nodes.each do |nd_id|
107 if nd_id and nd_id != '0'
108 e = XML::Node.new 'nd'
114 self.way_tags.each do |tag|
115 e = XML::Node.new 'tag'
126 self.way_nodes.each do |nd|
136 self.way_tags.each do |tag|
152 @nds = Array.new unless @nds
156 def add_tag_keyval(k, v)
157 @tags = Hash.new unless @tags
161 def save_with_history!
169 WayTag.transaction do
172 WayTag.delete_all(['id = ?', self.id])
183 WayNode.transaction do
186 WayNode.delete_all(['id = ?', self.id])
191 nd.id = [self.id, sequence]
198 old_way = OldWay.from_way(self)
199 old_way.timestamp = t
200 old_way.save_with_dependencies!
203 def preconditions_ok?
204 return false if self.nds.empty?
206 node = Node.find(:first, :conditions => ["id = ?", n])
207 unless node and node.visible
214 # Delete the way and it's relations, but don't really delete it - set its visibility to false and update the history etc to maintain wiki-like functionality.
215 def delete_with_relations_and_history(user)
218 # this should actually delete the relations,
219 # not just throw a PreconditionFailed if it's a member of a relation!!
220 if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
221 :conditions => [ "visible = 1 AND member_type='way' and member_id=?", self.id])
222 raise OSM::APIPreconditionFailedError
225 self.user_id = user.id
229 self.save_with_history!
232 raise OSM::APIAlreadyDeletedError
236 # delete a way and it's nodes that aren't part of other ways, with history
237 def delete_with_relations_and_nodes_and_history(user)
238 # delete the nodes not used by other ways
239 self.unshared_node_ids.each do |node_id|
240 n = Node.find(node_id)
246 self.user_id = user.id
248 self.delete_with_relations_and_history(user)
251 # Find nodes that belong to this way only
252 def unshared_node_ids
253 node_ids = self.nodes.collect { |node| node.id }
255 unless node_ids.empty?
256 way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}")
257 node_ids = node_ids - way_nodes.collect { |way_node| way_node.node_id }
263 # Temporary method to match interface to nodes