diff --git a/lib/devise/models/confirmable.rb b/lib/devise/models/confirmable.rb index ffd20f94..9dea3933 100644 --- a/lib/devise/models/confirmable.rb +++ b/lib/devise/models/confirmable.rb @@ -277,6 +277,16 @@ module Devise confirmation_required? && !@skip_confirmation_notification && self.email.present? end + # With reconfirmable, notify the original email when the user first + # requests the email change, instead of when the change is confirmed. + def send_email_change_notification? + if self.class.reconfirmable + self.class.send_email_change_notification && reconfirmation_required? + else + super + end + end + # A callback initiated after successfully confirming. This can be # used to insert your own logic that is only run after the user successfully # confirms. diff --git a/test/models/confirmable_test.rb b/test/models/confirmable_test.rb index c67f0127..2e566abd 100644 --- a/test/models/confirmable_test.rb +++ b/test/models/confirmable_test.rb @@ -516,4 +516,21 @@ class ReconfirmableTest < ActiveSupport::TestCase admin.save assert admin.pending_reconfirmation? end + + test 'should notify previous email on email change when configured' do + swap Devise, send_email_change_notification: true do + admin = create_admin + original_email = admin.email + + assert_difference 'ActionMailer::Base.deliveries.size', 2 do + assert admin.update_attributes(email: 'new-email@example.com') + end + assert_equal original_email, ActionMailer::Base.deliveries[-2]['to'].to_s + assert_equal 'new-email@example.com', ActionMailer::Base.deliveries[-1]['to'].to_s + + assert_email_not_sent do + assert admin.confirm + end + end + end end