class ApiKey < ActiveRecord::Base
  attr_accessible :team, :access_token

  # validations
  validates :team, presence: true
  validates :team, length: { maximum: 240 }

  # callbacks
  before_create :generate_access_token
  after_create :send_api_notification
  
  private
    
    def generate_access_token
      begin
        self.access_token = SecureRandom.hex
      end while self.class.exists?(access_token: access_token)
    end

    def send_api_notification
      UserMailer.api_notification(self).deliver
    end
end
