Module: Devise::Models::Confirmable::ClassMethods

Defined in:
lib/devise/models/confirmable.rb

Instance Method Summary collapse

Instance Method Details

#confirm_by_token(confirmation_token) ⇒ Object

Find a user by its confirmation token and try to confirm it. If no user is found, returns a new user with an error. If the user is already confirmed, create an error for the user Options must have the confirmation_token



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/devise/models/confirmable.rb', line 329

def confirm_by_token(confirmation_token)
  # When the `confirmation_token` parameter is blank, if there are any users with a blank
  # `confirmation_token` in the database, the first one would be confirmed here.
  # The error is being manually added here to ensure no users are confirmed by mistake.
  # This was done in the model for convenience, since validation errors are automatically
  # displayed in the view.
  if confirmation_token.blank?
    confirmable = new
    confirmable.errors.add(:confirmation_token, :blank)
    return confirmable
  end

  confirmable = find_first_by_auth_conditions(confirmation_token: confirmation_token)

  unless confirmable
    confirmation_digest = Devise.token_generator.digest(self, :confirmation_token, confirmation_token)
    confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_digest)
  end

  # TODO: replace above lines with
  # confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
  # after enough time has passed that Devise clients do not use digested tokens

  confirmable.confirm if confirmable.persisted?
  confirmable
end

#find_by_unconfirmed_email_with_errors(attributes = {}) ⇒ Object

Find a record for confirmation by unconfirmed email field



357
358
359
360
361
362
363
# File 'lib/devise/models/confirmable.rb', line 357

def find_by_unconfirmed_email_with_errors(attributes = {})
  attributes = attributes.slice(*confirmation_keys).permit!.to_h if attributes.respond_to? :permit
  unconfirmed_required_attributes = confirmation_keys.map { |k| k == :email ? :unconfirmed_email : k }
  unconfirmed_attributes = attributes.symbolize_keys
  unconfirmed_attributes[:unconfirmed_email] = unconfirmed_attributes.delete(:email)
  find_or_initialize_with_errors(unconfirmed_required_attributes, unconfirmed_attributes, :not_found)
end

#send_confirmation_instructions(attributes = {}) ⇒ Object

Attempt to find a user by its email. If a record is found, send new confirmation instructions to it. If not, try searching for a user by unconfirmed_email field. If no user is found, returns a new user with an email not found error. Options must contain the user email



316
317
318
319
320
321
322
323
# File 'lib/devise/models/confirmable.rb', line 316

def send_confirmation_instructions(attributes = {})
  confirmable = find_by_unconfirmed_email_with_errors(attributes) if reconfirmable
  unless confirmable.try(:persisted?)
    confirmable = find_or_initialize_with_errors(confirmation_keys, attributes, :not_found)
  end
  confirmable.resend_confirmation_instructions if confirmable.persisted?
  confirmable
end