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] }]
87 attr_writer :nds, :tags
89 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
90 el = XML::Node.new "way"
91 el["id"] = way_id.to_s
93 add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
95 old_nodes.each do |nd| # FIXME: need to make sure they come back in the right order
96 node_el = XML::Node.new "nd"
97 node_el["ref"] = nd.node_id.to_s
101 add_tags_to_xml_node(el, old_tags)
106 # Read full version of old way
107 # For get_nodes_undelete, uses same nodes, even if they've moved since
108 # For get_nodes_revert, allocates new ids
109 # Currently returns Potlatch-style array
110 # where [5] indicates whether latest version is usable as is (boolean)
111 # (i.e. is it visible? are we actually reverting to an earlier version?)
113 def get_nodes_undelete
116 [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
120 def get_nodes_revert(timestamp)
123 oldnode = OldNode.where("node_id = ? AND timestamp <= ?", n, timestamp).unredacted.order("timestamp DESC").first
124 curnode = Node.find(n)
126 reuse = curnode.visible
127 # if node has changed and it's in other ways, give it a new id
128 if !curnode.ways.all?(way_id) && (oldnode.lat != curnode.lat || oldnode.lon != curnode.lon || oldnode.tags != curnode.tags)
132 points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
137 # Temporary method to match interface to ways
142 # Pretend we're not in any relations
143 def containing_relation_members
147 # check whether this element is the latest version - that is,
148 # has the same version as its "current" counterpart.
149 def is_latest_version?
150 current_way.version == version