Commit ac510e71 authored by James Lopez's avatar James Lopez

Merge branch 'kassio/ensure-user-preferred-language' into 'master'

Ensure `preferred_language` always have a value.

See merge request gitlab-org/gitlab!37464
parents 83d8d7b1 06e895b7
...@@ -355,6 +355,12 @@ class User < ApplicationRecord ...@@ -355,6 +355,12 @@ class User < ApplicationRecord
scope :order_recent_last_activity, -> { reorder(Gitlab::Database.nulls_last_order('last_activity_on', 'DESC')) } 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')) } 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? def active_for_authentication?
super && can?(:log_in) super && can?(:log_in)
end 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 ...@@ -241,6 +241,22 @@ RSpec.describe User do
it { is_expected.to validate_length_of(:last_name).is_at_most(127) } it { is_expected.to validate_length_of(:last_name).is_at_most(127) }
end 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 describe 'username' do
it 'validates presence' do it 'validates presence' do
expect(subject).to validate_presence_of(:username) 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