module Devise::Models::Recoverable

def self.required_fields(klass)

def self.required_fields(klass)
  [:reset_password_sent_at, :reset_password_token]
end

def clear_reset_password_token

Removes reset_password token
def clear_reset_password_token
  self.reset_password_token = nil
  self.reset_password_sent_at = nil
end

def clear_reset_password_token?

def clear_reset_password_token?
  encrypted_password_changed = devise_respond_to_and_will_save_change_to_attribute?(:encrypted_password)
  authentication_keys_changed = self.class.authentication_keys.any? do |attribute|
    devise_respond_to_and_will_save_change_to_attribute?(attribute)
  end
  authentication_keys_changed || encrypted_password_changed
end

def reset_password(new_password, new_password_confirmation)

the passwords are valid and the record was saved, false otherwise.
Update password saving the record and clearing token. Returns true if
def reset_password(new_password, new_password_confirmation)
  if new_password.present?
    self.password = new_password
    self.password_confirmation = new_password_confirmation
    save
  else
    errors.add(:password, :blank)
    false
  end
end

def reset_password_period_valid?


reset_password_period_valid? # will always return false
# reset_password_within = 0.days

reset_password_period_valid? # returns false
# reset_password_within = 5.days and reset_password_sent_at = 5.days.ago

reset_password_period_valid? # returns true
# reset_password_within = 5.days and reset_password_sent_at = 4.days.ago

reset_password_period_valid? # returns true
# reset_password_within = 1.day and reset_password_sent_at = today

Example:

reset_password_within is a model configuration, must always be an integer value.
Returns true if the resource is not responding to reset_password_sent_at at all.
sending date does not exceed the confirm in time configured.
We do this by calculating if the difference between today and the
Checks if the reset password token sent is within the limit time.
def reset_password_period_valid?
  reset_password_sent_at && reset_password_sent_at.utc >= self.class.reset_password_within.ago.utc
end

def send_reset_password_instructions

Returns the token sent in the e-mail.
Resets reset password token and send reset password instructions by email.
def send_reset_password_instructions
  token = set_reset_password_token
  send_reset_password_instructions_notification(token)
  token
end

def send_reset_password_instructions_notification(token)

def send_reset_password_instructions_notification(token)
  send_devise_notification(:reset_password_instructions, token, {})
end

def set_reset_password_token

def set_reset_password_token
  raw, enc = Devise.token_generator.generate(self.class, :reset_password_token)
  self.reset_password_token   = enc
  self.reset_password_sent_at = Time.now.utc
  save(validate: false)
  raw
end