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