Sha256: 76bd2debf672b1fa6e795a9ae118e8f28d89b039a216413efcc46098f2bce558

Contents?: true

Size: 644 Bytes

Versions: 17

Compression:

Stored size: 644 Bytes

Contents

# frozen_string_literal: true

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

  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

17 entries across 17 versions & 1 rubygems

Version Path
pages_core-3.13.0 app/models/password_reset_token.rb
pages_core-3.12.7 app/models/password_reset_token.rb
pages_core-3.12.6 app/models/password_reset_token.rb
pages_core-3.12.5 app/models/password_reset_token.rb
pages_core-3.12.4 app/models/password_reset_token.rb
pages_core-3.12.3 app/models/password_reset_token.rb
pages_core-3.12.2 app/models/password_reset_token.rb
pages_core-3.12.1 app/models/password_reset_token.rb
pages_core-3.12.0 app/models/password_reset_token.rb
pages_core-3.11.3 app/models/password_reset_token.rb
pages_core-3.11.2 app/models/password_reset_token.rb
pages_core-3.11.1 app/models/password_reset_token.rb
pages_core-3.11.0 app/models/password_reset_token.rb
pages_core-3.10.2 app/models/password_reset_token.rb
pages_core-3.10.1 app/models/password_reset_token.rb
pages_core-3.9.2 app/models/password_reset_token.rb
pages_core-3.9.1 app/models/password_reset_token.rb