]> git.openstreetmap.org Git - rails.git/blob - app/models/old_node.rb
Merge remote-tracking branch 'upstream/pull/4747'
[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
30   self.table_name = "nodes"
31
32   # NOTE: this needs to be included after the table name changes, or
33   # the queries generated by Redactable will use the wrong table name.
34   include Redactable
35
36   validates :changeset, :associated => true
37   validates :latitude, :presence => true,
38                        :numericality => { :only_integer => true }
39   validates :longitude, :presence => true,
40                         :numericality => { :only_integer => true }
41   validates :timestamp, :presence => true
42   validates :visible, :inclusion => [true, false]
43
44   validate :validate_position
45
46   belongs_to :changeset
47   belongs_to :redaction, :optional => true
48   belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id", :inverse_of => :old_nodes
49
50   has_many :old_tags, :class_name => "OldNodeTag", :query_constraints => [:node_id, :version], :inverse_of => :old_node
51
52   def validate_position
53     errors.add(:base, "Node is not in the world") unless in_world?
54   end
55
56   def self.from_node(node)
57     old_node = OldNode.new
58     old_node.latitude = node.latitude
59     old_node.longitude = node.longitude
60     old_node.visible = node.visible
61     old_node.tags = node.tags
62     old_node.timestamp = node.timestamp
63     old_node.changeset_id = node.changeset_id
64     old_node.node_id = node.id
65     old_node.version = node.version
66     old_node
67   end
68
69   def save_with_dependencies!
70     save!
71
72     tags.each do |k, v|
73       tag = OldNodeTag.new
74       tag.k = k
75       tag.v = v
76       tag.node_id = node_id
77       tag.version = version
78       tag.save!
79     end
80   end
81
82   def tags
83     @tags ||= old_tags.to_h { |t| [t.k, t.v] }
84   end
85
86   attr_writer :tags
87
88   def tags_as_hash
89     tags
90   end
91
92   # Pretend we're not in any ways
93   def ways
94     []
95   end
96
97   # Pretend we're not in any relations
98   def containing_relation_members
99     []
100   end
101
102   # check whether this element is the latest version - that is,
103   # has the same version as its "current" counterpart.
104   def latest_version?
105     current_node.version == version
106   end
107 end