class TimeCard::Cycle < ApplicationRecord
  include Base::TimeCard::Cycle

  belongs_to :school, foreign_key: :SchoolID, inverse_of: :time_card_cycles

  has_many :submissions, class_name: '::TimeCard::Submission', inverse_of: :cycle,
    dependent: :destroy

  validates :name, :start, :stop, presence: true
  validates :name, length: { maximum: 64 }

  validate :start_before_stop

  private
    def start_before_stop
      return if start.nil? || stop.nil?
      return if start < stop

      errors.add(:stop, 'must be after start date')
    end
end
