]> git.openstreetmap.org Git - rails.git/blob - app/models/old_node.rb
Merge pull request #6394 from openstreetmap/dependabot/github_actions/ruby/setup...
[rails.git] / app / models / old_node.rb
1 # frozen_string_literal: true
2
3 # == Schema Information
4 #
5 # Table name: nodes
6 #
7 #  node_id      :bigint           not null, primary key
8 #  latitude     :integer          not null
9 #  longitude    :integer          not null
10 #  changeset_id :bigint           not null
11 #  visible      :boolean          not null
12 #  timestamp    :datetime         not null
13 #  tile         :bigint           not null
14 #  version      :bigint           not null, primary key
15 #  redaction_id :integer
16 #
17 # Indexes
18 #
19 #  nodes_changeset_id_idx  (changeset_id)
20 #  nodes_tile_idx          (tile)
21 #  nodes_timestamp_idx     (timestamp)
22 #
23 # Foreign Keys
24 #
25 #  nodes_changeset_id_fkey  (changeset_id => changesets.id)
26 #  nodes_redaction_id_fkey  (redaction_id => redactions.id)
27 #
28
29 class OldNode < ApplicationRecord
30   include GeoRecord
31
32   self.table_name = "nodes"
33
34   # NOTE: this needs to be included after the table name changes, or
35   # the queries generated by Redactable will use the wrong table name.
36   include Redactable
37
38   validates :changeset, :associated => true
39   validates :latitude, :presence => true,
40                        :numericality => { :only_integer => true }
41   validates :longitude, :presence => true,
42                         :numericality => { :only_integer => true }
43   validates :timestamp, :presence => true
44   validates :visible, :inclusion => [true, false]
45
46   validate :validate_position
47
48   belongs_to :changeset
49   belongs_to :redaction, :optional => true
50   belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id", :inverse_of => :old_nodes
51
52   has_many :old_tags, :class_name => "OldNodeTag", :foreign_key => [:node_id, :version], :inverse_of => :old_node
53
54   def validate_position
55     errors.add(:base, "Node is not in the world") unless in_world?
56   end
57
58   def self.from_node(node)
59     old_node = OldNode.new
60     old_node.latitude = node.latitude
61     old_node.longitude = node.longitude
62     old_node.visible = node.visible
63     old_node.tags = node.tags
64     old_node.timestamp = node.timestamp
65     old_node.changeset_id = node.changeset_id
66     old_node.node_id = node.id
67     old_node.version = node.version
68     old_node
69   end
70
71   def save_with_dependencies!
72     save!
73
74     tags.each do |k, v|
75       tag = OldNodeTag.new
76       tag.k = k
77       tag.v = v
78       tag.node_id = node_id
79       tag.version = version
80       tag.save!
81     end
82   end
83
84   def tags
85     @tags ||= old_tags.to_h { |t| [t.k, t.v] }
86   end
87
88   attr_writer :tags
89
90   # Pretend we're not in any ways
91   def ways
92     []
93   end
94
95   # Pretend we're not in any relations
96   def containing_relation_members
97     []
98   end
99
100   # check whether this element is the latest version - that is,
101   # has the same version as its "current" counterpart.
102   def latest_version?
103     current_node.version == version
104   end
105 end