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.
This commit is contained in:
Gabriel Jablonski 2026-04-17 21:58:08 -03:00 committed by GitHub
parent f8ffe3dc48
commit 97b71915aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View File

@ -132,7 +132,8 @@ class User < ApplicationRecord
end end
def send_devise_notification(notification, *) 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 end
def set_password_and_uid def set_password_and_uid

View File

@ -283,6 +283,33 @@ RSpec.describe User do
end end
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 describe 'destroy' do
it 'nullifies scheduled messages author when user has sent scheduled messages' do it 'nullifies scheduled messages author when user has sent scheduled messages' do
account = create(:account) account = create(:account)