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
95 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
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 el1['version'] = self.version.to_s
101 el1['changeset'] = self.changeset_id.to_s
103 if changeset_cache.key?(self.changeset_id)
104 # use the cache if available
106 changeset_cache[self.changeset_id] = self.changeset.user_id
109 user_id = changeset_cache[self.changeset_id]
111 if user_display_name_cache.key?(user_id)
112 # use the cache if available
113 elsif self.changeset.user.data_public?
114 user_display_name_cache[user_id] = self.changeset.user.display_name
116 user_display_name_cache[user_id] = nil
119 if not user_display_name_cache[user_id].nil?
120 el1['user'] = user_display_name_cache[user_id]
121 el1['uid'] = user_id.to_s
124 el1['redacted'] = self.redaction.id.to_s if self.redacted?
126 self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
127 e = XML::Node.new 'nd'
128 e['ref'] = nd.node_id.to_s
132 self.old_tags.each do |tag|
133 e = XML::Node.new 'tag'
142 # Read full version of old way
143 # For get_nodes_undelete, uses same nodes, even if they've moved since
144 # For get_nodes_revert, allocates new ids
145 # Currently returns Potlatch-style array
146 # where [5] indicates whether latest version is usable as is (boolean)
147 # (i.e. is it visible? are we actually reverting to an earlier version?)
149 def get_nodes_undelete
153 points << [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
158 def get_nodes_revert(timestamp)
161 oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).unredacted.order("timestamp DESC").first
162 curnode = Node.find(n)
163 id = n; reuse = curnode.visible
164 if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
165 # node has changed: if it's in other ways, give it a new id
166 if curnode.ways-[self.way_id] then id=-1; reuse=false end
168 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
173 # Temporary method to match interface to nodes
178 # Temporary method to match interface to ways
180 return self.old_nodes
183 # Pretend we're not in any relations
184 def containing_relation_members
188 # check whether this element is the latest version - that is,
189 # has the same version as its "current" counterpart.
190 def is_latest_version?
191 current_way.version == self.version