class Dsn < ApplicationRecord
  validates :key, presence: true, uniqueness: true
  validates :name, presence: true

  before_validation :generate_key, on: :create

  scope :enabled, -> { where(enabled: true) }

  def self.authenticate(key)
    enabled.find_by(key: key)
  end

  private

  def generate_key
    self.key ||= SecureRandom.hex(32)
  end
end