1 # == Schema Information
5 # way_id :bigint(8) default(0), not null, primary key
6 # changeset_id :bigint(8) not null
7 # timestamp :datetime not null
8 # version :bigint(8) not null, primary key
9 # visible :boolean default(TRUE), not null
10 # redaction_id :integer
14 # ways_changeset_id_idx (changeset_id)
15 # ways_timestamp_idx (timestamp)
19 # ways_changeset_id_fkey (changeset_id => changesets.id)
20 # ways_redaction_id_fkey (redaction_id => redactions.id)
23 class OldWay < ApplicationRecord
24 include ConsistencyValidations
25 include ObjectMetadata
27 self.table_name = "ways"
28 self.primary_keys = "way_id", "version"
30 # note this needs to be included after the table name changes, or
31 # the queries generated by Redactable will use the wrong table name.
36 belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id"
38 has_many :old_nodes, :class_name => "OldWayNode", :foreign_key => [:way_id, :version]
39 has_many :old_tags, :class_name => "OldWayTag", :foreign_key => [:way_id, :version]
41 validates :changeset, :presence => true, :associated => true
42 validates :timestamp, :presence => true
43 validates :visible, :inclusion => [true, false]
45 def self.from_way(way)
47 old_way.visible = way.visible
48 old_way.changeset_id = way.changeset_id
49 old_way.timestamp = way.timestamp
50 old_way.way_id = way.id
51 old_way.version = way.version
53 old_way.tags = way.tags
57 def save_with_dependencies!
72 nd.id = [way_id, version, sequence]
80 @nds ||= old_nodes.order(:sequence_id).collect(&:node_id)
84 @tags ||= Hash[old_tags.collect { |t| [t.k, t.v] }]
91 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
92 el = XML::Node.new "way"
93 el["id"] = way_id.to_s
95 add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
97 old_nodes.each do |nd| # FIXME: need to make sure they come back in the right order
98 node_el = XML::Node.new "nd"
99 node_el["ref"] = nd.node_id.to_s
103 add_tags_to_xml_node(el, old_tags)
108 # Read full version of old way
109 # For get_nodes_undelete, uses same nodes, even if they've moved since
110 # For get_nodes_revert, allocates new ids
111 # Currently returns Potlatch-style array
112 # where [5] indicates whether latest version is usable as is (boolean)
113 # (i.e. is it visible? are we actually reverting to an earlier version?)
115 def get_nodes_undelete
118 [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
122 def get_nodes_revert(timestamp)
125 oldnode = OldNode.where("node_id = ? AND timestamp <= ?", n, timestamp).unredacted.order("timestamp DESC").first
126 curnode = Node.find(n)
128 reuse = curnode.visible
129 if oldnode.lat != curnode.lat || oldnode.lon != curnode.lon || oldnode.tags != curnode.tags
130 # node has changed: if it's in other ways, give it a new id
131 if curnode.ways - [way_id]
136 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
141 # Temporary method to match interface to ways
146 # Pretend we're not in any relations
147 def containing_relation_members
151 # check whether this element is the latest version - that is,
152 # has the same version as its "current" counterpart.
153 def is_latest_version?
154 current_way.version == version