Skip to content

Commit

Permalink
When using reconfirmable, notify the original email about the change …
Browse files Browse the repository at this point in the history
…right away

Do not wait for the email change to be confirmed by the "unconfirmed
email" with reconfirmable: notify the original email right away.
  • Loading branch information
carlosantoniodasilva committed Mar 6, 2017
1 parent 70eb18d commit 8387cc9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/devise/models/confirmable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ def send_confirmation_notification?
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.
Expand Down
17 changes: 17 additions & 0 deletions test/models/confirmable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,21 @@ class Admin::WithSaveInCallback < Admin
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

0 comments on commit 8387cc9

Please sign in to comment.