Sha256: 504f046d36607326d985a793629d270c5a549864bf26f4e01e68ee25755f5151

Contents?: true

Size: 682 Bytes

Versions: 6

Compression:

Stored size: 682 Bytes

Contents

# frozen_string_literal: true

class PasswordResetToken < ApplicationRecord
  belongs_to :user
  before_create :ensure_token
  before_create :ensure_expiration

  validates :user_id, presence: true

  scope :active,  -> { where("expires_at >= ?", Time.now.utc) }
  scope :expired, -> { where("expires_at < ?", Time.now.utc) }

  class << self
    def default_expiration
      24.hours
    end

    def expire!
      expired.delete_all
    end
  end

  def expired?
    expires_at < Time.now.utc
  end

  private

  def ensure_expiration
    self.expires_at ||= Time.now.utc + self.class.default_expiration
  end

  def ensure_token
    self.token ||= SecureRandom.hex(32)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pages_core-3.9.0 app/models/password_reset_token.rb
pages_core-3.8.3 app/models/password_reset_token.rb
pages_core-3.8.2 app/models/password_reset_token.rb
pages_core-3.8.1 app/models/password_reset_token.rb
pages_core-3.8.0 app/models/password_reset_token.rb
pages_core-3.7.0 app/models/password_reset_token.rb