Delegate Account#user_locale method and allow nil (#2927)

This commit is contained in:
Matt Jankowski 2017-05-08 17:10:50 -04:00 committed by Eugen Rochko
parent cd830a2fab
commit 7a7d12d27f
4 changed files with 5 additions and 6 deletions

View file

@ -35,6 +35,6 @@ class LanguageDetector
end end
def default_locale def default_locale
account&.user&.locale || I18n.default_locale account&.user_locale || I18n.default_locale
end end
end end

View file

@ -71,7 +71,7 @@ class NotificationMailer < ApplicationMailer
private private
def locale_for_account(account) def locale_for_account(account)
I18n.with_locale(account.user.locale || I18n.default_locale) do I18n.with_locale(account.user_locale || I18n.default_locale) do
yield yield
end end
end end

View file

@ -111,6 +111,7 @@ class Account < ApplicationRecord
:current_sign_in_ip, :current_sign_in_ip,
:current_sign_in_at, :current_sign_in_at,
:confirmed?, :confirmed?,
:locale,
to: :user, to: :user,
prefix: true, prefix: true,
allow_nil: true allow_nil: true

View file

@ -43,16 +43,14 @@ describe LanguageDetector do
describe 'with an account' do describe 'with an account' do
it 'uses the account locale when present' do it 'uses the account locale when present' do
user = double(:user, locale: 'fr') account = double(user_locale: 'fr')
account = double(:account, user: user)
result = described_class.new('', account).to_iso_s result = described_class.new('', account).to_iso_s
expect(result).to eq :fr expect(result).to eq :fr
end end
it 'uses default locale when account is present but has no locale' do it 'uses default locale when account is present but has no locale' do
user = double(:user, locale: nil) account = double(user_locale: nil)
account = double(:accunt, user: user)
result = described_class.new('', account).to_iso_s result = described_class.new('', account).to_iso_s
expect(result).to eq :en expect(result).to eq :en