Commit 06e895b7 authored by Kassio Borges's avatar Kassio Borges

Ensure `preferred_language` always have a value

parent 00b75ff4
......@@ -355,6 +355,12 @@ class User < ApplicationRecord
scope :order_recent_last_activity, -> { reorder(Gitlab::Database.nulls_last_order('last_activity_on', 'DESC')) }
scope :order_oldest_last_activity, -> { reorder(Gitlab::Database.nulls_first_order('last_activity_on', 'ASC')) }
def preferred_language
read_attribute('preferred_language') ||
I18n.default_locale.to_s.presence_in(Gitlab::I18n::AVAILABLE_LANGUAGES.keys) ||
'en'
end
def active_for_authentication?
super && can?(:log_in)
end
......
---
title: Ensure User's preferred_language always has a value.
merge_request: 37464
author:
type: fixed
......@@ -241,6 +241,22 @@ RSpec.describe User do
it { is_expected.to validate_length_of(:last_name).is_at_most(127) }
end
describe 'preferred_language' do
context 'when its value is nil in the database' do
let(:user) { build(:user, preferred_language: nil) }
it 'falls back to I18n.default_locale when empty in the database' do
expect(user.preferred_language).to eq I18n.default_locale.to_s
end
it 'falls back to english when I18n.default_locale is not an available language' do
I18n.default_locale = :kl
expect(user.preferred_language).to eq 'en'
end
end
end
describe 'username' do
it 'validates presence' do
expect(subject).to validate_presence_of(:username)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment