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
108 el1['redacted'] = self.redaction.title
111 unless self.redacted? and (@user.nil? or not @user.moderator?)
112 # If a way is redacted and the user isn't a moderator, only show
113 # meta-data from this revision, but no real data.
114 self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
115 e = XML::Node.new 'nd'
116 e['ref'] = nd.node_id.to_s
120 self.old_tags.each do |tag|
121 e = XML::Node.new 'tag'
130 # Read full version of old way
131 # For get_nodes_undelete, uses same nodes, even if they've moved since
132 # For get_nodes_revert, allocates new ids
133 # Currently returns Potlatch-style array
134 # where [5] indicates whether latest version is usable as is (boolean)
135 # (i.e. is it visible? are we actually reverting to an earlier version?)
137 def get_nodes_undelete
141 points << [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
146 def get_nodes_revert(timestamp)
149 oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).order("timestamp DESC").first
150 curnode = Node.find(n)
151 id = n; reuse = curnode.visible
152 if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
153 # node has changed: if it's in other ways, give it a new id
154 if curnode.ways-[self.way_id] then id=-1; reuse=false end
156 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
161 # Temporary method to match interface to nodes
166 # Temporary method to match interface to ways
168 return self.old_nodes
171 # Pretend we're not in any relations
172 def containing_relation_members
176 # check whether this element is the latest version - that is,
177 # has the same version as its "current" counterpart.
178 def is_latest_version?
179 current_way.version == self.version