FIX: Use a distrbuted cache for site locale.

This commit is contained in:
Guo Xiang Tan 2017-09-12 19:36:17 +08:00
parent 51e1c0b6ce
commit 07bfe3d053
2 changed files with 17 additions and 11 deletions
lib/site_settings
spec/components/site_settings

View File

@ -9,13 +9,14 @@ class SiteSettings::DefaultsProvider
DEFAULT_LOCALE = 'en'.freeze
DEFAULT_CATEGORY = 'required'.freeze
@@site_locales ||= DistributedCache.new('site_locales')
def initialize(site_setting)
@site_setting = site_setting
@site_setting.refresh_settings << DEFAULT_LOCALE_KEY
@defaults = {}
@defaults[DEFAULT_LOCALE.to_sym] = {}
@site_locales = {}
refresh_site_locale!
end
@ -61,21 +62,21 @@ class SiteSettings::DefaultsProvider
end
def site_locale
@site_locales[current_db]
@@site_locales[current_db]
end
def site_locale=(val)
val = val.to_s
raise Discourse::InvalidParameters.new(:value) unless LocaleSiteSetting.valid_value?(val)
if val != @site_locales[current_db]
if val != @@site_locales[current_db]
@site_setting.provider.save(DEFAULT_LOCALE_KEY, val, SiteSetting.types[:string])
refresh_site_locale!
@site_setting.refresh!
Discourse.request_refresh!
end
@site_locales[current_db]
@@site_locales[current_db]
end
def each(&block)
@ -92,7 +93,7 @@ class SiteSettings::DefaultsProvider
description: @site_setting.description(DEFAULT_LOCALE_KEY),
type: SiteSetting.types[SiteSetting.types[:enum]],
preview: nil,
value: @site_locales[current_db],
value: @@site_locales[current_db],
valid_values: LocaleSiteSetting.values,
translate_names: LocaleSiteSetting.translate_names?
}
@ -100,7 +101,7 @@ class SiteSettings::DefaultsProvider
def refresh_site_locale!
RailsMultisite::ConnectionManagement.each_connection do |db|
@site_locales[db] =
@@site_locales[db] =
if GlobalSetting.respond_to?(DEFAULT_LOCALE_KEY) &&
(global_val = GlobalSetting.send(DEFAULT_LOCALE_KEY)) &&
!global_val.blank?
@ -111,7 +112,7 @@ class SiteSettings::DefaultsProvider
DEFAULT_LOCALE
end
@site_locales[db]
@@site_locales[db]
end
end

View File

@ -2,8 +2,7 @@ require 'rails_helper'
require_dependency 'site_settings/defaults_provider'
describe SiteSettings::DefaultsProvider do
let :provider_local do
let(:provider_local) do
SiteSettings::LocalProcessProvider.new
end
@ -14,7 +13,7 @@ describe SiteSettings::DefaultsProvider do
end
end
let :settings do
let(:settings) do
new_settings(provider_local)
end
@ -149,9 +148,15 @@ describe SiteSettings::DefaultsProvider do
end
describe '.site_locale=' do
it 'should store site locale in a distributed cache' do
expect(settings.defaults.class.class_variable_get(:@@site_locales))
.to be_a(DistributedCache)
end
it 'changes and store the current site locale' do
settings.defaults.site_locale = 'zh_CN'
expect(settings.defaults.site_locale).to eq 'zh_CN'
expect(settings.defaults.site_locale).to eq('zh_CN')
end
it 'changes and store the current site locale' do