From ab5bc31f7a584d024a8232ad737d3ed17bc946b8 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 31 May 2022 15:24:47 +0100 Subject: [PATCH] refactor sending confirmation emails --- app/mailers/devise_notify_mailer.rb | 32 +++++++++++++++++------------ 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/app/mailers/devise_notify_mailer.rb b/app/mailers/devise_notify_mailer.rb index 86386d8e4..2e77c8b17 100644 --- a/app/mailers/devise_notify_mailer.rb +++ b/app/mailers/devise_notify_mailer.rb @@ -35,22 +35,12 @@ class DeviseNotifyMailer < Devise::Mailer end def confirmation_instructions(record, token, _opts = {}) - url = "#{user_confirmation_url}?confirmation_token=" - username = record.email - if record.confirmable_template == User::CONFIRMABLE_TEMPLATE_ID && (record.unconfirmed_email.present? && record.unconfirmed_email != record.email) + if email_changed(record) username = record.unconfirmed_email - send_email( - record.unconfirmed_email, - record.confirmable_template, - personalisation(record, token, url, username:), - ) + send_confirmation_email(record.unconfirmed_email, record, token, username) end - send_email( - record.email, - record.confirmable_template, - personalisation(record, token, url, username:), - ) + send_confirmation_email(record.email, record, token, username) end def intercept_send?(email) @@ -62,6 +52,22 @@ class DeviseNotifyMailer < Devise::Mailer Rails.application.credentials[:email_allowlist] end +private + + def email_changed(record) + record.confirmable_template == User::CONFIRMABLE_TEMPLATE_ID && (record.unconfirmed_email.present? && record.unconfirmed_email != record.email) + end + + def send_confirmation_email(email, record, token, username) + url = "#{user_confirmation_url}?confirmation_token=" + + send_email( + email, + record.confirmable_template, + personalisation(record, token, url, username:), + ) + end + # def unlock_instructions(record, token, opts = {}) # super # end