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 1/2] 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! From 24d237de9e44beb2966d856968240c30ac57c4e6 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 25 Feb 2020 13:47:44 +0100 Subject: [PATCH 2/2] A cloned procedure should not have canonical relationship --- app/models/procedure.rb | 1 + spec/models/procedure_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 6171d5791..a27e57b47 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -295,6 +295,7 @@ class Procedure < ApplicationRecord procedure.cloned_from_library = from_library procedure.parent_procedure = self + procedure.canonical_procedure = nil if from_library procedure.service = nil diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 3b7a9b26c..080d753a5 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -538,6 +538,15 @@ describe Procedure do expect(subject.deliberation.attached?).to be true end end + + context 'with canonical procedure' do + let(:canonical_procedure) { create(:procedure) } + let(:procedure) { create(:procedure, canonical_procedure: canonical_procedure, received_mail: received_mail, service: service) } + + it 'do not clone canonical procedure' do + expect(subject.canonical_procedure).to be_nil + end + end end describe '#publish!' do