refactor(mailer): for devise mailer set locale in dedicated action

This commit is contained in:
Colin Darie 2024-03-28 12:30:31 +01:00
parent 172aeec3dc
commit 36fc535386
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
3 changed files with 22 additions and 3 deletions

View file

@ -32,8 +32,6 @@ module MailerDefaultsConfigurableConcern
def configure_defaults_for_user(user) def configure_defaults_for_user(user)
return if !user.is_a?(User) # not for super-admins return if !user.is_a?(User) # not for super-admins
I18n.locale = user.locale
if user.preferred_domain_demarches_gouv_fr? if user.preferred_domain_demarches_gouv_fr?
set_currents_for_demarches_gouv_fr set_currents_for_demarches_gouv_fr
else else

View file

@ -33,8 +33,11 @@ class DeviseUserMailer < Devise::Mailer
opts[:reply_to] = Current.no_reply_email opts[:reply_to] = Current.no_reply_email
@procedure = opts[:procedure_after_confirmation] || nil @procedure = opts[:procedure_after_confirmation] || nil
@prefill_token = opts[:prefill_token] @prefill_token = opts[:prefill_token]
I18n.with_locale(record.locale) do
super super
end end
end
def self.critical_email?(action_name) def self.critical_email?(action_name)
true true

View file

@ -19,6 +19,24 @@ RSpec.describe DeviseUserMailer, type: :mailer do
expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name) expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name)
end end
end end
describe "i18n" do
context "when locale is fr" do
let(:user) { create(:user, locale: :fr) }
it "uses fr locale" do
expect(subject.body).to include("Activez votre compte")
end
end
context "when locale is en" do
let(:user) { create(:user, locale: :en) }
it "uses en locale" do
expect(subject.body).to include("Activate account")
end
end
end
end end
describe 'headers for user' do describe 'headers for user' do