From 8eb6dd78a1207fdb79d3482327c8e28a2696c448 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 25 Feb 2020 15:12:09 +0100 Subject: [PATCH] mailers: fix procedure_after_confirmation in the confirmation email As mailers are run asynchronously, they don't have access to the CurrentConfirmation defined in an earlier request. For the procedure_after_confirmation to be serialized to the Mailer, we need to pass it at creation time. --- app/mailers/devise_user_mailer.rb | 2 +- app/models/user.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/mailers/devise_user_mailer.rb b/app/mailers/devise_user_mailer.rb index eab62ff29..2d4822bff 100644 --- a/app/mailers/devise_user_mailer.rb +++ b/app/mailers/devise_user_mailer.rb @@ -15,7 +15,7 @@ class DeviseUserMailer < Devise::Mailer def confirmation_instructions(record, token, opts = {}) opts[:from] = NO_REPLY_EMAIL - @procedure = CurrentConfirmation.procedure_after_confirmation || nil + @procedure = opts[:procedure_after_confirmation] || nil super end end diff --git a/app/models/user.rb b/app/models/user.rb index 1515ed760..72dc84cc4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,6 +25,20 @@ class User < ApplicationRecord before_validation -> { sanitize_email(:email) } + # Override of Devise::Models::Confirmable#send_confirmation_instructions + def send_confirmation_instructions + unless @raw_confirmation_token + generate_confirmation_token! + end + + opts = pending_reconfirmation? ? { to: unconfirmed_email } : {} + + # Make our procedure_after_confirmation available to the Mailer + opts[:procedure_after_confirmation] = CurrentConfirmation.procedure_after_confirmation + + send_devise_notification(:confirmation_instructions, @raw_confirmation_token, opts) + end + # Callback provided by Devise def after_confirmation link_invites!