]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
991925102c4fa9813ca4bccdae1e2f2a7f180df2
[rails.git] / app / models / old_way.rb
1 # == Schema Information
2 #
3 # Table name: ways
4 #
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
11 #
12 # Indexes
13 #
14 #  ways_changeset_id_idx  (changeset_id)
15 #  ways_timestamp_idx     (timestamp)
16 #
17 # Foreign Keys
18 #
19 #  ways_changeset_id_fkey  (changeset_id => changesets.id)
20 #  ways_redaction_id_fkey  (redaction_id => redactions.id)
21 #
22
23 class OldWay < ApplicationRecord
24   include ConsistencyValidations
25   include ObjectMetadata
26
27   self.table_name = "ways"
28   self.primary_keys = "way_id", "version"
29
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.
32   include Redactable
33
34   belongs_to :changeset
35   belongs_to :redaction
36   belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id"
37
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]
40
41   validates :changeset, :presence => true, :associated => true
42   validates :timestamp, :presence => true
43   validates :visible, :inclusion => [true, false]
44
45   def self.from_way(way)
46     old_way = OldWay.new
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
52     old_way.nds = way.nds
53     old_way.tags = way.tags
54     old_way
55   end
56
57   def save_with_dependencies!
58     save!
59
60     tags.each do |k, v|
61       tag = OldWayTag.new
62       tag.k = k
63       tag.v = v
64       tag.way_id = way_id
65       tag.version = version
66       tag.save!
67     end
68
69     sequence = 1
70     nds.each do |n|
71       nd = OldWayNode.new
72       nd.id = [way_id, version, sequence]
73       nd.node_id = n
74       nd.save!
75       sequence += 1
76     end
77   end
78
79   def nds
80     @nds ||= old_nodes.order(:sequence_id).collect(&:node_id)
81   end
82
83   def tags
84     @tags ||= Hash[old_tags.collect { |t| [t.k, t.v] }]
85   end
86
87   attr_writer :nds, :tags
88
89   def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
90     el = XML::Node.new "way"
91     el["id"] = way_id.to_s
92
93     add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
94
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
98       el << node_el
99     end
100
101     add_tags_to_xml_node(el, old_tags)
102
103     el
104   end
105
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?)
112
113   def get_nodes_undelete
114     nds.collect do |n|
115       node = Node.find(n)
116       [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
117     end
118   end
119
120   def get_nodes_revert(timestamp)
121     points = []
122     nds.each do |n|
123       oldnode = OldNode.where("node_id = ? AND timestamp <= ?", n, timestamp).unredacted.order("timestamp DESC").first
124       curnode = Node.find(n)
125       id = 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)
129         id = -1
130         reuse = false
131       end
132       points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
133     end
134     points
135   end
136
137   # Temporary method to match interface to ways
138   def way_nodes
139     old_nodes
140   end
141
142   # Pretend we're not in any relations
143   def containing_relation_members
144     []
145   end
146
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
151   end
152 end