1 class OldWay < ActiveRecord::Base
2 include ConsistencyValidations
5 self.table_name = "ways"
6 self.primary_keys = "way_id", "version"
10 belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id"
12 has_many :old_nodes, :class_name => 'OldWayNode', :foreign_key => [:way_id, :version]
13 has_many :old_tags, :class_name => 'OldWayTag', :foreign_key => [:way_id, :version]
15 validates_associated :changeset
17 def self.from_way(way)
19 old_way.visible = way.visible
20 old_way.changeset_id = way.changeset_id
21 old_way.timestamp = way.timestamp
22 old_way.way_id = way.id
23 old_way.version = way.version
25 old_way.tags = way.tags
29 def save_with_dependencies!
31 # dont touch this unless you really have figured out why it's called
32 # (Rails doesn't deal well with the old ways table (called 'ways') because
33 # it doesn't have a unique key. It knows how to insert and auto_increment
34 # id and get it back but we have that and we want to get the 'version' back
35 # we could add another column but thats a lot of data. No, set_primary_key
36 # doesn't work either.
38 clear_aggregation_cache
39 clear_association_cache
40 @attributes.update(OldWay.where(:way_id => self.way_id, :timestamp => self.timestamp).order("version DESC").first.instance_variable_get('@attributes'))
42 # ok, you can touch from here on
44 self.tags.each do |k,v|
48 tag.way_id = self.way_id
49 tag.version = self.version
56 nd.id = [self.way_id, self.version, sequence]
66 OldWayNode.where(:way_id => self.way_id, :version => self.version).order(:sequence_id).each do |nd|
76 OldWayTag.where(:way_id => self.way_id, :version => self.version).each do |tag|
80 @tags = Hash.new unless @tags
93 el1 = XML::Node.new 'way'
94 el1['id'] = self.way_id.to_s
95 el1['visible'] = self.visible.to_s
96 el1['timestamp'] = self.timestamp.xmlschema
97 if self.changeset.user.data_public?
98 el1['user'] = self.changeset.user.display_name
99 el1['uid'] = self.changeset.user.id.to_s
101 el1['version'] = self.version.to_s
102 el1['changeset'] = self.changeset.id.to_s
105 el1['redacted'] = self.redaction.title
108 unless self.redacted? and (@user.nil? or not @user.moderator?)
109 # If a way is redacted and the user isn't a moderator, only show
110 # meta-data from this revision, but no real data.
111 self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
112 e = XML::Node.new 'nd'
113 e['ref'] = nd.node_id.to_s
117 self.old_tags.each do |tag|
118 e = XML::Node.new 'tag'
127 # Read full version of old way
128 # For get_nodes_undelete, uses same nodes, even if they've moved since
129 # For get_nodes_revert, allocates new ids
130 # Currently returns Potlatch-style array
131 # where [5] indicates whether latest version is usable as is (boolean)
132 # (i.e. is it visible? are we actually reverting to an earlier version?)
134 def get_nodes_undelete
138 points << [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
143 def get_nodes_revert(timestamp)
146 oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).order("timestamp DESC").first
147 curnode = Node.find(n)
148 id = n; reuse = curnode.visible
149 if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
150 # node has changed: if it's in other ways, give it a new id
151 if curnode.ways-[self.way_id] then id=-1; reuse=false end
153 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
158 # Temporary method to match interface to nodes
163 # Temporary method to match interface to ways
165 return self.old_nodes
168 # Pretend we're not in any relations
169 def containing_relation_members
173 # check whether this element is the latest version - that is,
174 # has the same version as its "current" counterpart.
175 def is_latest_version?
176 current_way.version == self.version