Module: Negroni::Models::Recoverable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/negroni/models/recoverable.rb
Overview
Recoverable takes care of resetting the user password and send reset instructions.
## Required attributes
Recoverable requires that the including class has the following attributes:
* `reset_password_sent_at`: [DateTime]
* `reset_password_token`: [String]
## Options
Recoverable adds the following class options:
* `reset_password_keys`: the keys you want to use when recovering the
password for an account
* `reset_password_within`: the time period within which the password must
be reset or the token expires.
* `sign_in_after_reset_password`: whether or not to sign in the user
automatically after a password reset.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.required_fields(_klass = nil) ⇒ Object
Required fields for Recoverable.
Instance Method Summary collapse
-
#reset_password(new_password, new_password_confirmation) ⇒ Boolean
Update password saving the record and clearing token.
-
#reset_password_period_valid? ⇒ Boolean
Checks if the reset password token sent is within the limit time.
-
#send_reset_password_instructions ⇒ String
Resets reset password token and send reset password instructions by email.
Class Method Details
.required_fields(_klass = nil) ⇒ Object
Required fields for Recoverable
40 41 42 |
# File 'lib/negroni/models/recoverable.rb', line 40 def self.required_fields(_klass = nil) [:reset_password_sent_at, :reset_password_token] end |
Instance Method Details
#reset_password(new_password, new_password_confirmation) ⇒ Boolean
Update password saving the record and clearing token. Returns true if the passwords are valid and the record was saved, false otherwise.
56 57 58 59 60 61 |
# File 'lib/negroni/models/recoverable.rb', line 56 def reset_password(new_password, new_password_confirmation) self.password = new_password self.password_confirmation = new_password_confirmation save end |
#reset_password_period_valid? ⇒ Boolean
Checks if the reset password token sent is within the limit time. We do this by calculating if the difference between today and the sending date does not exceed the confirm in time configured. Returns true if the resource is not responding to reset_password_sent_at at all.
reset_password_within is a model configuration, must always be an integer value.
100 101 102 103 |
# File 'lib/negroni/models/recoverable.rb', line 100 def reset_password_period_valid? reset_password_sent_at && reset_password_sent_at.utc >= self.class.reset_password_within.ago.utc end |
#send_reset_password_instructions ⇒ String
Resets reset password token and send reset password instructions by email. Returns the token sent in the e-mail.
67 68 69 70 71 72 |
# File 'lib/negroni/models/recoverable.rb', line 67 def send_reset_password_instructions token = set_reset_password_token send_reset_password_instructions_notification(token) token end |