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