]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
Merge remote-tracking branch 'upstream/pull/4251'
[rails.git] / app / models / old_way.rb
1 # == Schema Information
2 #
3 # Table name: ways
4 #
5 #  way_id       :bigint(8)        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
26   self.table_name = "ways"
27
28   # NOTE: this needs to be included after the table name changes, or
29   # the queries generated by Redactable will use the wrong table name.
30   include Redactable
31
32   belongs_to :changeset
33   belongs_to :redaction, :optional => true
34   belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id", :inverse_of => :old_ways
35
36   has_many :old_nodes, :class_name => "OldWayNode", :query_constraints => [:way_id, :version], :inverse_of => :old_way
37   has_many :old_tags, :class_name => "OldWayTag", :query_constraints => [:way_id, :version], :inverse_of => :old_way
38
39   validates :changeset, :associated => true
40   validates :timestamp, :presence => true
41   validates :visible, :inclusion => [true, false]
42
43   def self.from_way(way)
44     old_way = OldWay.new
45     old_way.visible = way.visible
46     old_way.changeset_id = way.changeset_id
47     old_way.timestamp = way.timestamp
48     old_way.way_id = way.id
49     old_way.version = way.version
50     old_way.nds = way.nds
51     old_way.tags = way.tags
52     old_way
53   end
54
55   def save_with_dependencies!
56     save!
57
58     tags.each do |k, v|
59       tag = OldWayTag.new
60       tag.k = k
61       tag.v = v
62       tag.way_id = way_id
63       tag.version = version
64       tag.save!
65     end
66
67     sequence = 1
68     nds.each do |n|
69       nd = OldWayNode.new
70       nd.id = [way_id, version, sequence]
71       nd.node_id = n
72       nd.save!
73       sequence += 1
74     end
75   end
76
77   def nds
78     @nds ||= old_nodes.order(:sequence_id).collect(&:node_id)
79   end
80
81   def tags
82     @tags ||= old_tags.to_h { |t| [t.k, t.v] }
83   end
84
85   attr_writer :nds, :tags
86
87   # Temporary method to match interface to ways
88   def way_nodes
89     old_nodes
90   end
91
92   # Pretend we're not in any relations
93   def containing_relation_members
94     []
95   end
96
97   # check whether this element is the latest version - that is,
98   # has the same version as its "current" counterpart.
99   def latest_version?
100     current_way.version == version
101   end
102 end