module VersionRecord
  extend ActiveSupport::Concern

  def readonly? = persisted?

  included do
    belongs_to :created_by_user, class_name: 'User', optional: true

    enum :event_type, { create: 'create',
                        update: 'update',
                        discard: 'discard',
                        restore: 'restore' }, prefix: true, validate: true

    validates :version_no, presence: true, numericality: { only_integer: true, greater_than: 0 }
    validates :event_type, presence: true

    scope :chronological, -> { order(:version_no, :id) }
  end
end
