Added skip_confirmation! It skips confirmation token generation, e-mail sending and automatically sets confirmed_at.

This commit is contained in:
José Valim 2009-12-15 00:30:28 +01:00
parent 358a2389ce
commit 9a658d5e74
4 changed files with 29 additions and 18 deletions

View File

@ -34,8 +34,8 @@ module Devise
base.class_eval do
extend ClassMethods
before_create :generate_confirmation_token
after_create :send_confirmation_instructions
before_create :generate_confirmation_token, :if => :confirmation_required?
after_create :send_confirmation_instructions, :if => :confirmation_required?
end
end
@ -78,8 +78,20 @@ module Devise
confirmed? || confirmation_period_valid?
end
# If you don't want confirmation to be sent on create, neither a code
# to be generated, call skip_confirmation!
def skip_confirmation!
self.confirmed_at = Time.now
@skip_confirmation = true
end
protected
# Callback to overwrite if confirmation is required or not.
def confirmation_required?
!@skip_confirmation
end
# Checks if the confirmation for the user is within the limit time.
# We do this by calculating if the difference between today and the
# confirmation sent date does not exceed the confirm in time configured.

View File

@ -20,16 +20,11 @@ module Devise
end
end
# Update password
def reset_password(new_password, new_password_confirmation)
self.password = new_password
self.password_confirmation = new_password_confirmation
end
# Update password saving the record and clearing token. Returns true if
# the passwords are valid and the record was saved, false otherwise.
def reset_password!(new_password, new_password_confirmation)
reset_password(new_password, new_password_confirmation)
self.password = new_password
self.password_confirmation = new_password_confirmation
clear_reset_password_token if valid?
save
end

View File

@ -108,6 +108,17 @@ class ConfirmableTest < ActiveSupport::TestCase
end
end
test 'should not generate a new token neither send e-mail if skip_confirmation! is invoked' do
user = new_user
user.skip_confirmation!
assert_email_not_sent do
user.save!
assert_nil user.confirmation_token
assert_not_nil user.confirmed_at
end
end
test 'should find a user to send confirmation instructions' do
user = create_user
confirmation_user = User.send_confirmation_instructions(:email => user.email)
@ -125,20 +136,13 @@ class ConfirmableTest < ActiveSupport::TestCase
assert_equal 'not found', confirmation_user.errors[:email]
end
test 'should reset confirmation token before send the confirmation instructions email' do
test 'should generate a confirmation token before send the confirmation instructions email' do
user = create_user
token = user.confirmation_token
confirmation_user = User.send_confirmation_instructions(:email => user.email)
assert_not_equal token, user.reload.confirmation_token
end
test 'should reset confirmation status when sending the confirmation instructions' do
user = create_user
assert_not user.confirmed?
confirmation_user = User.send_confirmation_instructions(:email => user.email)
assert_not user.reload.confirmed?
end
test 'should send email instructions for the user confirm it\'s email' do
user = create_user
assert_email_sent do

View File

@ -32,7 +32,7 @@ class RecoverableTest < ActiveSupport::TestCase
test 'should reset password and password confirmation from params' do
user = create_user
user.reset_password('123456789', '987654321')
user.reset_password!('123456789', '987654321')
assert_equal '123456789', user.password
assert_equal '987654321', user.password_confirmation
end