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]
68 @nds ||= self.old_nodes.order(:sequence_id).collect { |n| n.node_id }
72 @tags ||= Hash[self.old_tags.collect { |t| [t.k, t.v] }]
83 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
84 el = XML::Node.new 'way'
85 el['id'] = self.way_id.to_s
87 add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
89 self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
90 node_el = XML::Node.new 'nd'
91 node_el['ref'] = nd.node_id.to_s
95 add_tags_to_xml_node(el, self.old_tags)
100 # Read full version of old way
101 # For get_nodes_undelete, uses same nodes, even if they've moved since
102 # For get_nodes_revert, allocates new ids
103 # Currently returns Potlatch-style array
104 # where [5] indicates whether latest version is usable as is (boolean)
105 # (i.e. is it visible? are we actually reverting to an earlier version?)
107 def get_nodes_undelete
108 self.nds.collect do |n|
110 [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
114 def get_nodes_revert(timestamp)
117 oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).unredacted.order("timestamp DESC").first
118 curnode = Node.find(n)
119 id = n; reuse = curnode.visible
120 if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
121 # node has changed: if it's in other ways, give it a new id
122 if curnode.ways-[self.way_id] then id=-1; reuse=false end
124 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
129 # Temporary method to match interface to nodes
134 # Temporary method to match interface to ways
136 return self.old_nodes
139 # Pretend we're not in any relations
140 def containing_relation_members
144 # check whether this element is the latest version - that is,
145 # has the same version as its "current" counterpart.
146 def is_latest_version?
147 current_way.version == self.version