1 class OldWay < ActiveRecord::Base
2 include ConsistencyValidations
4 self.table_name = "ways"
5 self.primary_keys = "way_id", "version"
7 # note this needs to be included after the table name changes, or
8 # the queries generated by Redactable will use the wrong table name.
13 belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id"
15 has_many :old_nodes, :class_name => 'OldWayNode', :foreign_key => [:way_id, :version]
16 has_many :old_tags, :class_name => 'OldWayTag', :foreign_key => [:way_id, :version]
18 validates_associated :changeset
20 def self.from_way(way)
22 old_way.visible = way.visible
23 old_way.changeset_id = way.changeset_id
24 old_way.timestamp = way.timestamp
25 old_way.way_id = way.id
26 old_way.version = way.version
28 old_way.tags = way.tags
32 def save_with_dependencies!
34 # dont touch this unless you really have figured out why it's called
35 # (Rails doesn't deal well with the old ways table (called 'ways') because
36 # it doesn't have a unique key. It knows how to insert and auto_increment
37 # id and get it back but we have that and we want to get the 'version' back
38 # we could add another column but thats a lot of data. No, set_primary_key
39 # doesn't work either.
41 clear_aggregation_cache
42 clear_association_cache
43 @attributes.update(OldWay.where(:way_id => self.way_id, :timestamp => self.timestamp).order("version DESC").first.instance_variable_get('@attributes'))
45 # ok, you can touch from here on
47 self.tags.each do |k,v|
51 tag.way_id = self.way_id
52 tag.version = self.version
59 nd.id = [self.way_id, self.version, sequence]
69 OldWayNode.where(:way_id => self.way_id, :version => self.version).order(:sequence_id).each do |nd|
79 OldWayTag.where(:way_id => self.way_id, :version => self.version).each do |tag|
83 @tags = Hash.new unless @tags
96 el1 = XML::Node.new 'way'
97 el1['id'] = self.way_id.to_s
98 el1['visible'] = self.visible.to_s
99 el1['timestamp'] = self.timestamp.xmlschema
100 if self.changeset.user.data_public?
101 el1['user'] = self.changeset.user.display_name
102 el1['uid'] = self.changeset.user.id.to_s
104 el1['version'] = self.version.to_s
105 el1['changeset'] = self.changeset.id.to_s
107 el1['redacted'] = self.redaction.id.to_s if self.redacted?
109 self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
110 e = XML::Node.new 'nd'
111 e['ref'] = nd.node_id.to_s
115 self.old_tags.each do |tag|
116 e = XML::Node.new 'tag'
125 # Read full version of old way
126 # For get_nodes_undelete, uses same nodes, even if they've moved since
127 # For get_nodes_revert, allocates new ids
128 # Currently returns Potlatch-style array
129 # where [5] indicates whether latest version is usable as is (boolean)
130 # (i.e. is it visible? are we actually reverting to an earlier version?)
132 def get_nodes_undelete
136 points << [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
141 def get_nodes_revert(timestamp)
144 oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).unredacted.order("timestamp DESC").first
145 curnode = Node.find(n)
146 id = n; reuse = curnode.visible
147 if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
148 # node has changed: if it's in other ways, give it a new id
149 if curnode.ways-[self.way_id] then id=-1; reuse=false end
151 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
156 # Temporary method to match interface to nodes
161 # Temporary method to match interface to ways
163 return self.old_nodes
166 # Pretend we're not in any relations
167 def containing_relation_members
171 # check whether this element is the latest version - that is,
172 # has the same version as its "current" counterpart.
173 def is_latest_version?
174 current_way.version == self.version