]> git.openstreetmap.org Git - rails.git/blob - app/models/old_node.rb
Fix new rubocop warnings
[rails.git] / app / models / old_node.rb
1 # == Schema Information
2 #
3 # Table name: nodes
4 #
5 #  node_id      :bigint(8)        not null, primary key
6 #  latitude     :integer          not null
7 #  longitude    :integer          not null
8 #  changeset_id :bigint(8)        not null
9 #  visible      :boolean          not null
10 #  timestamp    :datetime         not null
11 #  tile         :bigint(8)        not null
12 #  version      :bigint(8)        not null, primary key
13 #  redaction_id :integer
14 #
15 # Indexes
16 #
17 #  nodes_changeset_id_idx  (changeset_id)
18 #  nodes_tile_idx          (tile)
19 #  nodes_timestamp_idx     (timestamp)
20 #
21 # Foreign Keys
22 #
23 #  nodes_changeset_id_fkey  (changeset_id => changesets.id)
24 #  nodes_redaction_id_fkey  (redaction_id => redactions.id)
25 #
26
27 class OldNode < ApplicationRecord
28   include GeoRecord
29   include ConsistencyValidations
30
31   self.table_name = "nodes"
32   self.primary_keys = "node_id", "version"
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, :presence => true, :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
50   belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id"
51
52   has_many :old_tags, :class_name => "OldNodeTag", :foreign_key => [:node_id, :version]
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   def tags_as_hash
91     tags
92   end
93
94   # Pretend we're not in any ways
95   def ways
96     []
97   end
98
99   # Pretend we're not in any relations
100   def containing_relation_members
101     []
102   end
103
104   # check whether this element is the latest version - that is,
105   # has the same version as its "current" counterpart.
106   def is_latest_version?
107     current_node.version == version
108   end
109 end