class Admission::Document < ApplicationRecord
  audited
  belongs_to :school

  has_many :application_documents, dependent: :destroy
  has_many :applicant_documents, dependent: :restrict_with_error

  has_many :applications, through: :application_documents
  has_many :applicants, through: :applicant_documents

  validates :name, :display_name, :description, presence: true
  validates :name, length: { maximum: 255 }
  validates :display_name, length: { maximum: 128 }

  scope :by_archived, ->(flag) { where(archived: flag) unless flag.nil? }

  def self.sorted
    order(:name)
  end

  def self.positioned
    order('admission_application_documents.position')
  end
end
