1 class OldWay < ActiveRecord::Base
2 include ConsistencyValidations
5 set_primary_keys :way_id, :version
9 has_many :old_nodes, :class_name => 'OldWayNode', :foreign_key => [:way_id, :version]
10 has_many :old_tags, :class_name => 'OldWayTag', :foreign_key => [:way_id, :version]
12 validates_associated :changeset
14 def self.from_way(way)
16 old_way.visible = way.visible
17 old_way.changeset_id = way.changeset_id
18 old_way.timestamp = way.timestamp
19 old_way.way_id = way.id
20 old_way.version = way.version
22 old_way.tags = way.tags
26 def save_with_dependencies!
28 # dont touch this unless you really have figured out why it's called
29 # (Rails doesn't deal well with the old ways table (called 'ways') because
30 # it doesn't have a unique key. It knows how to insert and auto_increment
31 # id and get it back but we have that and we want to get the 'version' back
32 # we could add another column but thats a lot of data. No, set_primary_key
33 # doesn't work either.
35 clear_aggregation_cache
36 clear_association_cache
37 @attributes.update(OldWay.where(:way_id => self.way_id, :timestamp => self.timestamp).order("version DESC").first.instance_variable_get('@attributes'))
39 # ok, you can touch from here on
41 self.tags.each do |k,v|
45 tag.way_id = self.way_id
46 tag.version = self.version
53 nd.id = [self.way_id, self.version, sequence]
63 OldWayNode.where(:way_id => self.way_id, :version => self.version).order(:sequence_id).each do |nd|
73 OldWayTag.where(:way_id => self.way_id, :version => self.version).each do |tag|
77 @tags = Hash.new unless @tags
90 el1 = XML::Node.new 'way'
91 el1['id'] = self.way_id.to_s
92 el1['visible'] = self.visible.to_s
93 el1['timestamp'] = self.timestamp.xmlschema
94 if self.changeset.user.data_public?
95 el1['user'] = self.changeset.user.display_name
96 el1['uid'] = self.changeset.user.id.to_s
98 el1['version'] = self.version.to_s
99 el1['changeset'] = self.changeset.id.to_s
101 self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
102 e = XML::Node.new 'nd'
103 e['ref'] = nd.node_id.to_s
107 self.old_tags.each do |tag|
108 e = XML::Node.new 'tag'
116 # Read full version of old way
117 # For get_nodes_undelete, uses same nodes, even if they've moved since
118 # For get_nodes_revert, allocates new ids
119 # Currently returns Potlatch-style array
120 # where [5] indicates whether latest version is usable as is (boolean)
121 # (i.e. is it visible? are we actually reverting to an earlier version?)
123 def get_nodes_undelete
127 points << [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
132 def get_nodes_revert(timestamp)
135 oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).order("timestamp DESC").first
136 curnode = Node.find(n)
137 id = n; reuse = curnode.visible
138 if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
139 # node has changed: if it's in other ways, give it a new id
140 if curnode.ways-[self.node_id] then id=-1; reuse=false end
142 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
147 # Temporary method to match interface to nodes
152 # Temporary method to match interface to ways
154 return self.old_nodes
157 # Pretend we're not in any relations
158 def containing_relation_members