Module: Negroni::Models::Recoverable::ClassMethods

Defined in:
lib/negroni/models/recoverable.rb

Overview

Class Methods for ‘Recoverable`

Instance Method Summary collapse

Instance Method Details

#reset_password_by_token(attributes = {}) ⇒ Object

Attempt to find a user by its reset_password_token to reset its password. If a user is found and token is still valid, reset its password and automatically try saving the record. If not user is found, returns a new user containing an error in reset_password_token attribute.

Attributes must contain reset_password_token, password and confirmation



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/negroni/models/recoverable.rb', line 171

def reset_password_by_token(attributes = {})
  original = attributes[:reset_password_token]
  token = Negroni.token_generator.digest(
    self, :reset_password_token, original
  )

  recoverable = find_or_initialize_with_error_by(:reset_password_token,
                                                 token)

  if recoverable.persisted?
    if recoverable.reset_password_period_valid?
      recoverable.reset_password(attributes[:password],
                                 attributes[:password_confirmation])
    else
      recoverable.errors.add(:reset_password_token, :expired)
    end
  end

  if recoverable.reset_password_token.present?
    recoverable.reset_password_token = original
  end

  recoverable
end

#send_reset_password_instructions(attributes = {}) ⇒ Object

Attempt to find a user by its email. If a record is found, send new password instructions to it. If user is not found, returns a new user with an email not found error.

Attributes must contain the user’s email



153
154
155
156
157
158
159
160
# File 'lib/negroni/models/recoverable.rb', line 153

def send_reset_password_instructions(attributes = {})
  recoverable = find_or_initialize_with_errors(reset_password_keys,
                                               attributes,
                                               :not_found)

  recoverable.send_reset_password_instructions if recoverable.persisted?
  recoverable
end

#with_reset_password_token(token) ⇒ Object

Attempt to find a user by password reset token. If a user is found, return it. If not, return ‘nil`.



141
142
143
144
145
146
# File 'lib/negroni/models/recoverable.rb', line 141

def with_reset_password_token(token)
  reset_password_token = Negroni.token_generator.digest(
    self, :reset_password_token, token
  )
  to_adapter.find_first(reset_password_token: reset_password_token)
end