1 class OldWay < ActiveRecord::Base
2 include ConsistencyValidations
5 self.table_name = "ways"
6 self.primary_keys = "way_id", "version"
8 # note this needs to be included after the table name changes, or
9 # the queries generated by Redactable will use the wrong table name.
14 belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id"
16 has_many :old_nodes, :class_name => 'OldWayNode', :foreign_key => [:way_id, :version]
17 has_many :old_tags, :class_name => 'OldWayTag', :foreign_key => [:way_id, :version]
19 validates_associated :changeset
21 def self.from_way(way)
23 old_way.visible = way.visible
24 old_way.changeset_id = way.changeset_id
25 old_way.timestamp = way.timestamp
26 old_way.way_id = way.id
27 old_way.version = way.version
29 old_way.tags = way.tags
33 def save_with_dependencies!
35 # dont touch this unless you really have figured out why it's called
36 # (Rails doesn't deal well with the old ways table (called 'ways') because
37 # it doesn't have a unique key. It knows how to insert and auto_increment
38 # id and get it back but we have that and we want to get the 'version' back
39 # we could add another column but thats a lot of data. No, set_primary_key
40 # doesn't work either.
42 clear_aggregation_cache
43 clear_association_cache
44 @attributes.update(OldWay.where(:way_id => self.way_id, :timestamp => self.timestamp).order("version DESC").first.instance_variable_get('@attributes'))
46 # ok, you can touch from here on
48 self.tags.each do |k,v|
52 tag.way_id = self.way_id
53 tag.version = self.version
60 nd.id = [self.way_id, self.version, sequence]
70 self.old_nodes.order(:sequence_id).each do |nd|
80 self.old_tags.each do |tag|
84 @tags = Hash.new unless @tags
96 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
97 el = XML::Node.new 'way'
98 el['id'] = self.way_id.to_s
100 add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
102 self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
103 node_el = XML::Node.new 'nd'
104 node_el['ref'] = nd.node_id.to_s
108 add_tags_to_xml_node(el, self.old_tags)
113 # Read full version of old way
114 # For get_nodes_undelete, uses same nodes, even if they've moved since
115 # For get_nodes_revert, allocates new ids
116 # Currently returns Potlatch-style array
117 # where [5] indicates whether latest version is usable as is (boolean)
118 # (i.e. is it visible? are we actually reverting to an earlier version?)
120 def get_nodes_undelete
124 points << [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
129 def get_nodes_revert(timestamp)
132 oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).unredacted.order("timestamp DESC").first
133 curnode = Node.find(n)
134 id = n; reuse = curnode.visible
135 if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
136 # node has changed: if it's in other ways, give it a new id
137 if curnode.ways-[self.way_id] then id=-1; reuse=false end
139 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
144 # Temporary method to match interface to nodes
149 # Temporary method to match interface to ways
151 return self.old_nodes
154 # Pretend we're not in any relations
155 def containing_relation_members
159 # check whether this element is the latest version - that is,
160 # has the same version as its "current" counterpart.
161 def is_latest_version?
162 current_way.version == self.version