1 # frozen_string_literal: true
 
   3 # == Schema Information
 
   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
 
  19 #  nodes_changeset_id_idx  (changeset_id)
 
  20 #  nodes_tile_idx          (tile)
 
  21 #  nodes_timestamp_idx     (timestamp)
 
  25 #  nodes_changeset_id_fkey  (changeset_id => changesets.id)
 
  26 #  nodes_redaction_id_fkey  (redaction_id => redactions.id)
 
  29 class OldNode < ApplicationRecord
 
  32   self.table_name = "nodes"
 
  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.
 
  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]
 
  46   validate :validate_position
 
  49   belongs_to :redaction, :optional => true
 
  50   belongs_to :current_node, :class_name => "Node", :foreign_key => "node_id", :inverse_of => :old_nodes
 
  52   has_many :old_tags, :class_name => "OldNodeTag", :foreign_key => [:node_id, :version], :inverse_of => :old_node
 
  55     errors.add(:base, "Node is not in the world") unless in_world?
 
  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
 
  71   def save_with_dependencies!
 
  85     @tags ||= old_tags.to_h { |t| [t.k, t.v] }
 
  90   # Pretend we're not in any ways
 
  95   # Pretend we're not in any relations
 
  96   def containing_relation_members
 
 100   # check whether this element is the latest version - that is,
 
 101   # has the same version as its "current" counterpart.
 
 103     current_node.version == version