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!
36 self.tags.each do |k,v|
40 tag.way_id = self.way_id
41 tag.version = self.version
48 nd.id = [self.way_id, self.version, sequence]
56 @nds ||= self.old_nodes.order(:sequence_id).collect { |n| n.node_id }
60 @tags ||= Hash[self.old_tags.collect { |t| [t.k, t.v] }]
71 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
72 el = XML::Node.new 'way'
73 el['id'] = self.way_id.to_s
75 add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
77 self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
78 node_el = XML::Node.new 'nd'
79 node_el['ref'] = nd.node_id.to_s
83 add_tags_to_xml_node(el, self.old_tags)
88 # Read full version of old way
89 # For get_nodes_undelete, uses same nodes, even if they've moved since
90 # For get_nodes_revert, allocates new ids
91 # Currently returns Potlatch-style array
92 # where [5] indicates whether latest version is usable as is (boolean)
93 # (i.e. is it visible? are we actually reverting to an earlier version?)
95 def get_nodes_undelete
96 self.nds.collect do |n|
98 [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
102 def get_nodes_revert(timestamp)
105 oldnode = OldNode.where('node_id = ? AND timestamp <= ?', n, timestamp).unredacted.order("timestamp DESC").first
106 curnode = Node.find(n)
107 id = n; reuse = curnode.visible
108 if oldnode.lat != curnode.lat or oldnode.lon != curnode.lon or oldnode.tags != curnode.tags then
109 # node has changed: if it's in other ways, give it a new id
110 if curnode.ways-[self.way_id] then id=-1; reuse=false end
112 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
117 # Temporary method to match interface to nodes
122 # Temporary method to match interface to ways
124 return self.old_nodes
127 # Pretend we're not in any relations
128 def containing_relation_members
132 # check whether this element is the latest version - that is,
133 # has the same version as its "current" counterpart.
134 def is_latest_version?
135 current_way.version == self.version