From 9a658d5e7437db29ca189548daa0cee4514d0957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 15 Dec 2009 00:30:28 +0100 Subject: [PATCH] Added skip_confirmation! It skips confirmation token generation, e-mail sending and automatically sets confirmed_at. --- lib/devise/models/confirmable.rb | 16 ++++++++++++++-- lib/devise/models/recoverable.rb | 9 ++------- test/models/confirmable_test.rb | 20 ++++++++++++-------- test/models/recoverable_test.rb | 2 +- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index 25a9de85..47a1ecec 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -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. diff --git a/lib/devise/models/recoverable.rb b/lib/devise/models/recoverable.rb index 4bfcbba5..9fa05a8b 100644 --- a/lib/devise/models/recoverable.rb +++ b/lib/devise/models/recoverable.rb @@ -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 diff --git a/test/models/confirmable_test.rb b/test/models/confirmable_test.rb index 3256520e..6d269fea 100644 --- a/test/models/confirmable_test.rb +++ b/test/models/confirmable_test.rb @@ -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 diff --git a/test/models/recoverable_test.rb b/test/models/recoverable_test.rb index f7804ddd..fdd5402e 100644 --- a/test/models/recoverable_test.rb +++ b/test/models/recoverable_test.rb @@ -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