class Frequency < ActiveRecord::Base
  attr_accessible :delay, :description, :helpmsg, :name, :prefix, :activate

  # Relation
  has_many :bans, :dependent => :destroy, :inverse_of => :frequency
  accepts_nested_attributes_for :bans, :allow_destroy => true

  # Validation
  validates :name,
            :uniqueness => true,
            :presence => true,
            :format => { :with => /\A-.+-\z/, :message => "Frequency name must begin and end with -" }

  validates :prefix,
            :uniqueness => true,
            :presence => true,
            :format => { :with => /\A\..+\z/, :message => "Frequency call prefix must begin with ." }

  validates :delay,
            :presence => true

  before_destroy :raise_if_last
  def raise_if_last
    if Frequency.count < 3
      raise "Can't delete last two frequencies"
    end
  end

end
