From 72cfaad6185332857cb5860a1d812f573efadaf7 Mon Sep 17 00:00:00 2001 From: Greg Gates Date: Fri, 22 Feb 2013 12:43:01 -0500 Subject: [PATCH] Add #skip_confirmation_notification to Confirmable --- lib/devise/models/confirmable.rb | 12 +++++++++++- test/models/confirmable_test.rb | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index 18146fd7..51ca45d4 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -34,7 +34,7 @@ module Devise included do before_create :generate_confirmation_token, :if => :confirmation_required? - after_create :send_on_create_confirmation_instructions, :if => :confirmation_required? + after_create :send_on_create_confirmation_instructions, :if => :send_confirmation_notification? before_update :postpone_email_change_until_confirmation, :if => :postpone_email_change? after_update :send_confirmation_instructions, :if => :reconfirmation_required? end @@ -119,6 +119,12 @@ module Devise self.confirmed_at = Time.now.utc end + # Skips sending the confirmation notification email after_create. Unlike + # #skip_confirmation!, record still requires confirmation. + def skip_confirmation_notification! + @skip_confirmation_notification = true + end + # If you don't want reconfirmation to be sent, neither a code # to be generated, call skip_reconfirmation! def skip_reconfirmation! @@ -223,6 +229,10 @@ module Devise self.class.reconfirmable && @reconfirmation_required end + def send_confirmation_notification? + confirmation_required? && !@skip_confirmation_notification + end + module ClassMethods # 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 diff --git a/test/models/confirmable_test.rb b/test/models/confirmable_test.rb index d262b870..51e8fee2 100644 --- a/test/models/confirmable_test.rb +++ b/test/models/confirmable_test.rb @@ -104,6 +104,16 @@ class ConfirmableTest < ActiveSupport::TestCase end end + test 'should skip confirmation e-mail without confirming if skip_confirmation_notification! is invoked' do + user = new_user + user.skip_confirmation_notification! + + assert_email_not_sent do + user.save! + assert !user.confirmed? + end + end + test 'should find a user to send confirmation instructions' do user = create_user confirmation_user = User.send_confirmation_instructions(:email => user.email)