class VerificationCode < ApplicationRecord

  VALID_FOR = 15.minutes

  before_validation(on: :create) do
    self.token = rand(100000..999999).to_s
  end

  validates_presence_of :token

  belongs_to :user

  scope :valid, -> { where("verification_codes.created_at > ?", VALID_FOR.ago) }
  scope :expired, -> { where("verification_codes.created_at <= ?", VALID_FOR.ago) }


  def expires_at
    self.created_at + VALID_FOR
  end

end
