1 # frozen_string_literal: true
 
   3 # == Schema Information
 
   7 #  way_id       :bigint           not null, primary key
 
   8 #  changeset_id :bigint           not null
 
   9 #  timestamp    :datetime         not null
 
  10 #  version      :bigint           not null, primary key
 
  11 #  visible      :boolean          default(TRUE), not null
 
  12 #  redaction_id :integer
 
  16 #  ways_changeset_id_idx  (changeset_id)
 
  17 #  ways_timestamp_idx     (timestamp)
 
  21 #  ways_changeset_id_fkey  (changeset_id => changesets.id)
 
  22 #  ways_redaction_id_fkey  (redaction_id => redactions.id)
 
  25 class OldWay < ApplicationRecord
 
  26   self.table_name = "ways"
 
  28   # NOTE: this needs to be included after the table name changes, or
 
  29   # the queries generated by Redactable will use the wrong table name.
 
  33   belongs_to :redaction, :optional => true
 
  34   belongs_to :current_way, :class_name => "Way", :foreign_key => "way_id", :inverse_of => :old_ways
 
  36   has_many :old_nodes, :class_name => "OldWayNode", :foreign_key => [:way_id, :version], :inverse_of => :old_way
 
  37   has_many :old_tags, :class_name => "OldWayTag", :foreign_key => [:way_id, :version], :inverse_of => :old_way
 
  39   validates :changeset, :associated => true
 
  40   validates :timestamp, :presence => true
 
  41   validates :visible, :inclusion => [true, false]
 
  43   def self.from_way(way)
 
  45     old_way.visible = way.visible
 
  46     old_way.changeset_id = way.changeset_id
 
  47     old_way.timestamp = way.timestamp
 
  48     old_way.way_id = way.id
 
  49     old_way.version = way.version
 
  51     old_way.tags = way.tags
 
  55   def save_with_dependencies!
 
  70       nd.id = [way_id, version, sequence]
 
  78     @nds ||= old_nodes.order(:sequence_id).collect(&:node_id)
 
  82     @tags ||= old_tags.to_h { |t| [t.k, t.v] }
 
  85   attr_writer :nds, :tags
 
  87   # Temporary method to match interface to ways
 
  92   # Pretend we're not in any relations
 
  93   def containing_relation_members
 
  97   # check whether this element is the latest version - that is,
 
  98   # has the same version as its "current" counterpart.
 
 100     current_way.version == version