From 97b71915aa8bebc5e1fb6ba955ee102507dbb48b Mon Sep 17 00:00:00 2001 From: Gabriel Jablonski Date: Fri, 17 Apr 2026 21:58:08 -0300 Subject: [PATCH] fix(mailer): fall back to user account for devise email locale (#267) Devise emails sent from unauthenticated flows (password reset, unlock, confirmation re-send) were always rendered in the default locale because Current.account is nil in that context. Use the user's first account as a fallback so the email respects the account locale configured by the user. --- app/models/user.rb | 3 ++- spec/models/user_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 965bc0c22..e0ca51a9d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -132,7 +132,8 @@ class User < ApplicationRecord end def send_devise_notification(notification, *) - devise_mailer.with(account: Current.account).send(notification, self, *).deliver_later + account = Current.account || accounts.first + devise_mailer.with(account: account).send(notification, self, *).deliver_later end def set_password_and_uid diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index f9eef4ef1..1afa21367 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -283,6 +283,33 @@ RSpec.describe User do end end + describe '#send_devise_notification' do + let(:account) { create(:account, locale: 'pt_BR') } + let(:recipient) { create(:user, account: account) } + let(:mailer_double) { double(reset_password_instructions: double(deliver_later: nil)) } # rubocop:disable RSpec/VerifiedDoubles + + before do + Current.reset + allow(Devise::Mailer).to receive(:with).and_return(mailer_double) + end + + it 'falls back to the user account when Current.account is nil' do + recipient.send_reset_password_instructions + + expect(Devise::Mailer).to have_received(:with).with(account: account) + end + + it 'prefers Current.account when it is set' do + other_account = create(:account) + create(:account_user, user: recipient, account: other_account) + Current.account = other_account + + recipient.send_reset_password_instructions + + expect(Devise::Mailer).to have_received(:with).with(account: other_account) + end + end + describe 'destroy' do it 'nullifies scheduled messages author when user has sent scheduled messages' do account = create(:account)