1 class Way < ActiveRecord::Base
6 has_many :nodes, :through => :way_nodes, :order => 'sequence_id'
7 has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
8 has_many :way_tags, :foreign_key => 'id'
10 has_many :old_ways, :foreign_key => 'id', :order => 'version'
12 set_table_name 'current_ways'
14 def self.from_xml(xml, create=false)
20 doc.find('//osm/way').each do |pt|
21 return Way.from_xml_node(pt, create)
28 def self.from_xml_node(pt, create=false)
31 if !create and pt['id'] != '0'
32 way.id = pt['id'].to_i
35 way.version = pt['version']
38 way.timestamp = Time.now
42 way.timestamp = Time.parse(pt['timestamp'])
46 pt.find('tag').each do |tag|
47 way.add_tag_keyval(tag['k'], tag['v'])
50 pt.find('nd').each do |nd|
51 way.add_nd_num(nd['ref'])
57 # Find a way given it's ID, and in a single SQL call also grab its nodes
60 # 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
61 def self.find_eager(id)
62 way = Way.find(id, :include => {:way_nodes => :node})
63 #If waytag had a multipart key that was real, you could do this:
64 #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
67 # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
69 doc = OSM::API.new.get_xml_doc
70 doc.root << to_xml_node()
74 def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
75 el1 = XML::Node.new 'way'
76 el1['id'] = self.id.to_s
77 el1['visible'] = self.visible.to_s
78 el1['timestamp'] = self.timestamp.xmlschema
79 el1['version'] = self.version.to_s
81 user_display_name_cache = {} if user_display_name_cache.nil?
83 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
84 # use the cache if available
85 elsif self.user.data_public?
86 user_display_name_cache[self.user_id] = self.user.display_name
88 user_display_name_cache[self.user_id] = nil
91 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
93 # make sure nodes are output in sequence_id order
95 self.way_nodes.each do |nd|
97 # if there is a list of visible nodes then use that to weed out deleted nodes
98 if visible_nodes[nd.node_id]
99 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
102 # otherwise, manually go to the db to check things
103 if nd.node.visible? and nd.node.visible?
104 ordered_nodes[nd.sequence_id] = nd.node_id.to_s
109 ordered_nodes.each do |nd_id|
110 if nd_id and nd_id != '0'
111 e = XML::Node.new 'nd'
117 self.way_tags.each do |tag|
118 e = XML::Node.new 'tag'
129 self.way_nodes.each do |nd|
139 self.way_tags.each do |tag|
155 @nds = Array.new unless @nds
159 def add_tag_keyval(k, v)
160 @tags = Hash.new unless @tags
164 def save_with_history!
173 WayTag.delete_all(['id = ?', self.id])
183 WayNode.delete_all(['id = ?', self.id])
187 nd.id = [self.id, sequence]
193 old_way = OldWay.from_way(self)
194 old_way.timestamp = t
195 old_way.save_with_dependencies!
199 def update_from(new_way, user)
200 if !new_way.preconditions_ok?
201 raise OSM::APIPreconditionFailedError.new
203 self.user_id = user.id
204 self.tags = new_way.tags
205 self.nds = new_way.nds
211 def preconditions_ok?
212 return false if self.nds.empty?
214 node = Node.find(:first, :conditions => ["id = ?", n])
215 unless node and node.visible
222 # 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.
223 def delete_with_relations_and_history(user)
226 # this should actually delete the relations,
227 # not just throw a PreconditionFailed if it's a member of a relation!!
229 # FIXME: this should probably renamed to delete_with_history
230 if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
231 :conditions => [ "visible = 1 AND member_type='way' and member_id=?", self.id])
232 raise OSM::APIPreconditionFailedError
235 self.user_id = user.id
239 self.save_with_history!
242 raise OSM::APIAlreadyDeletedError
246 # delete a way and it's nodes that aren't part of other ways, with history
247 def delete_with_relations_and_nodes_and_history(user)
249 node_ids = self.nodes.collect {|node| node.id }
250 node_ids_not_to_delete = []
251 way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}")
253 node_ids_not_to_delete = way_nodes.collect {|way_node| way_node.node_id}
255 node_ids_to_delete = node_ids - node_ids_not_to_delete
257 # delete the nodes not used by other ways
258 node_ids_to_delete.each do |node_id|
259 n = Node.find(node_id)
265 self.user_id = user.id
267 self.delete_with_relations_and_history(user)