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 :changeset, :presence => true, :associated => true
20 validates :timestamp, :presence => true
21 validates :visible, :inclusion => [true, false]
23 def self.from_way(way)
25 old_way.visible = way.visible
26 old_way.changeset_id = way.changeset_id
27 old_way.timestamp = way.timestamp
28 old_way.way_id = way.id
29 old_way.version = way.version
31 old_way.tags = way.tags
35 def save_with_dependencies!
50 nd.id = [way_id, version, sequence]
58 @nds ||= old_nodes.order(:sequence_id).collect(&:node_id)
62 @tags ||= Hash[old_tags.collect { |t| [t.k, t.v] }]
69 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
70 el = XML::Node.new "way"
71 el["id"] = way_id.to_s
73 add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
75 old_nodes.each do |nd| # FIXME: need to make sure they come back in the right order
76 node_el = XML::Node.new "nd"
77 node_el["ref"] = nd.node_id.to_s
81 add_tags_to_xml_node(el, old_tags)
86 # Read full version of old way
87 # For get_nodes_undelete, uses same nodes, even if they've moved since
88 # For get_nodes_revert, allocates new ids
89 # Currently returns Potlatch-style array
90 # where [5] indicates whether latest version is usable as is (boolean)
91 # (i.e. is it visible? are we actually reverting to an earlier version?)
93 def get_nodes_undelete
96 [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
100 def get_nodes_revert(timestamp)
103 oldnode = OldNode.where("node_id = ? AND timestamp <= ?", n, timestamp).unredacted.order("timestamp DESC").first
104 curnode = Node.find(n)
106 reuse = curnode.visible
107 if oldnode.lat != curnode.lat || oldnode.lon != curnode.lon || oldnode.tags != curnode.tags
108 # node has changed: if it's in other ways, give it a new id
109 if curnode.ways - [way_id]
114 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
119 # Temporary method to match interface to ways
124 # Pretend we're not in any relations
125 def containing_relation_members
129 # check whether this element is the latest version - that is,
130 # has the same version as its "current" counterpart.
131 def is_latest_version?
132 current_way.version == version