class Communication::LeafNote < ApplicationRecord
  include Base::LeafNote
  
  has_many :leaf_note_schools, class_name: 'Communication::LeafNoteSchool', foreign_key: :SLNID,
    inverse_of: :note, dependent: :destroy
  has_many :leaf_note_users, class_name: 'Communication::LeafNoteUser', foreign_key: :SLNID,
    inverse_of: :note, dependent: :destroy
  has_many :schools, through: :leaf_note_schools, source: :school

  after_update :clear_schools, if: :not_limited?

  scope :by_date_range, ->(range) { where(date: range) if range }

  private
    def not_limited?
      !limited?
    end

    def clear_schools
      leaf_note_schools.destroy_all
    end
end
