class Accounting::CafeteriaConfig < ApplicationRecord
  belongs_to :school
  belongs_to :subcategory

  after_initialize :set_date
  after_save :sync_transactions

  validates :start_date, presence: true

  private
    def set_date
      self.start_date = Date.today unless self.start_date
    end

    def sync_transactions
      return unless saved_changes.keys.include?('enabled')

      Accounting::SyncCafeteriaJob.perform_async(school_id)
    end
end
