module Devise::Models::Confirmable::ClassMethods

def confirm_by_token(confirmation_token)

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