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:
parent
f8ffe3dc48
commit
97b71915aa
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user