KB Prep: Refactor & backport User#locale method

This commit is contained in:
Ryan Lue 2019-04-01 09:22:29 +02:00 committed by Thorsten Eckel
parent b3f636a5d1
commit b1b137bb0d
4 changed files with 85 additions and 0 deletions

View file

@ -930,6 +930,11 @@ try to find correct name
firstname.blank? && lastname.blank? firstname.blank? && lastname.blank?
end end
# get locale of user or system if user's own is not set
def locale
preferences.fetch(:locale) { Setting.get('locale_default') }
end
private private
def check_name def check_name

View file

@ -0,0 +1,16 @@
class RenameLocaleOnUsers < ActiveRecord::Migration[5.1]
def up
return if !Setting.find_by(name: 'system_init_done')
return if ActiveRecord::Base.connection.columns('users').map(&:name).exclude?('locale')
ActiveRecord::Migration.rename_column(:users, :locale, :_locale)
ObjectManager::Attribute.find_by(name: 'locale').update(name: '_locale')
end
def down
return if ActiveRecord::Base.connection.columns('users').map(&:name).exclude?('_locale')
ActiveRecord::Migration.rename_column(:users, :_locale, :locale)
ObjectManager::Attribute.find_by(name: '_locale').update(name: 'locale')
end
end

View file

@ -0,0 +1,43 @@
require 'rails_helper'
RSpec.describe RenameLocaleOnUsers, type: :db_migration do
context 'when custom OMA attribute #locale exists', db_strategy: :reset do
before do
ObjectManager::Attribute.add(
force: true,
object: 'User',
name: 'locale',
display: 'Locale',
data_type: 'select',
data_option: {
'default' => '',
'options' => {},
},
active: true,
position: 20,
to_migrate: true,
created_by_id: 1,
updated_by_id: 1,
)
ObjectManager::Attribute.migration_execute
end
it 'renames #locale' do
expect { migrate }
.to change { ActiveRecord::Base.connection.columns('users').map(&:name) }
.to not_include('locale')
.and include('_locale')
expect(ObjectManager::Attribute.exists?(name: 'locale')).to be(false)
expect(ObjectManager::Attribute.exists?(name: '_locale')).to be(true)
end
end
context 'when no #locale attribute exists' do
it 'makes no changes to the "users" table' do
expect { migrate }
.not_to change { ActiveRecord::Base.connection.columns('users') }
end
end
end

View file

@ -595,6 +595,27 @@ RSpec.describe User, type: :model do
end end
end end
end end
describe '#locale' do
subject(:user) { create(:user, preferences: preferences) }
context 'with no #preferences[:locale]' do
let(:preferences) { {} }
before { Setting.set('locale_default', 'foo') }
it 'returns the system-wide default locale' do
expect(user.locale).to eq('foo')
end
end
context 'with a #preferences[:locale]' do
let(:preferences) { { locale: 'bar' } }
it 'returns the users configured locale' do
expect(user.locale).to eq('bar')
end
end
end
end end
describe 'Attributes:' do describe 'Attributes:' do