From b1b137bb0da5dd3329f90d4bdd5e5122630feceb Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Mon, 1 Apr 2019 09:22:29 +0200 Subject: [PATCH] KB Prep: Refactor & backport User#locale method --- app/models/user.rb | 5 +++ .../20190328000000_rename_locale_on_users.rb | 16 +++++++ .../db/migrate/rename_locale_on_users_spec.rb | 43 +++++++++++++++++++ spec/models/user_spec.rb | 21 +++++++++ 4 files changed, 85 insertions(+) create mode 100644 db/migrate/20190328000000_rename_locale_on_users.rb create mode 100644 spec/db/migrate/rename_locale_on_users_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb index ede3fb1ba..ea1949e1d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -930,6 +930,11 @@ try to find correct name firstname.blank? && lastname.blank? 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 def check_name diff --git a/db/migrate/20190328000000_rename_locale_on_users.rb b/db/migrate/20190328000000_rename_locale_on_users.rb new file mode 100644 index 000000000..f7f251c8a --- /dev/null +++ b/db/migrate/20190328000000_rename_locale_on_users.rb @@ -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 diff --git a/spec/db/migrate/rename_locale_on_users_spec.rb b/spec/db/migrate/rename_locale_on_users_spec.rb new file mode 100644 index 000000000..3d88aa8d4 --- /dev/null +++ b/spec/db/migrate/rename_locale_on_users_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3e395f3c2..bfa3aa3fd 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -595,6 +595,27 @@ RSpec.describe User, type: :model do 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 user’s configured locale' do + expect(user.locale).to eq('bar') + end + end + end end describe 'Attributes:' do