Revert "FEATURE: Site settings defaults per locale"

This reverts commit 468a8fcd206d14ff4421758e840d63a27c246254.
This commit is contained in:
Guo Xiang Tan
2017-08-07 10:31:50 +09:00
parent 4b53fe3cc7
commit 439fe8ba24
28 changed files with 364 additions and 1373 deletions

View File

@ -64,7 +64,7 @@ describe SiteSettingExtension do
expect(settings.hello).to eq(99)
end
it "publishes changes cross sites" do
it "Publishes changes cross sites" do
settings.setting(:hello, 1)
settings2.setting(:hello, 1)
@ -143,7 +143,7 @@ describe SiteSettingExtension do
it "should publish changes to clients" do
settings.setting("test_setting", 100)
settings.setting("test_setting", nil, client: true)
settings.client_setting("test_setting")
messages = MessageBus.track_publish do
settings.test_setting = 88
@ -155,11 +155,8 @@ describe SiteSettingExtension do
end
describe "remove_override" do
before do
settings.setting(:test_override, "test")
settings.refresh!
end
it "correctly nukes overrides" do
settings.setting(:test_override, "test")
settings.test_override = "bla"
settings.remove_override!(:test_override)
expect(settings.test_override).to eq("test")
@ -266,7 +263,6 @@ describe SiteSettingExtension do
settings.setting(:test_int_enum, 1, enum: TestIntEnumClass)
settings.test_int_enum = "2"
settings.refresh!
expect(settings.defaults[:test_int_enum]).to eq(1)
expect(settings.test_int_enum).to eq(2)
end
@ -276,7 +272,7 @@ describe SiteSettingExtension do
class TestEnumClass
def self.valid_value?(v)
self.values.include?(v)
true
end
def self.values
['en']
@ -303,10 +299,6 @@ describe SiteSettingExtension do
expect(settings.all_settings.detect { |s| s[:setting] == :test_enum }).to be_present
end
it 'should report error when being set other values' do
expect { settings.test_enum = 'not_in_enum' }.to raise_error(Discourse::InvalidParameters)
end
context 'when overridden' do
after :each do
settings.remove_override!(:validated_setting)
@ -392,14 +384,6 @@ describe SiteSettingExtension do
end
end
describe ".set_and_log" do
it "raises an error when set for an invalid setting name" do
expect {
settings.set_and_log("provider", "haxxed")
}.to raise_error(ArgumentError)
end
end
describe "filter domain name" do
before do
settings.setting(:white_listed_spam_host_domains, "www.example.com")
@ -519,62 +503,4 @@ describe SiteSettingExtension do
end
end
describe 'locale default overrides are respected' do
before do
settings.setting(:test_override, 'default', locale_default: { zh_CN: 'cn' })
settings.refresh!
end
after do
settings.remove_override!(:test_override)
end
it 'ensures the default cache expired after overriding the default_locale' do
expect(settings.test_override).to eq('default')
settings.default_locale = 'zh_CN'
expect(settings.test_override).to eq('cn')
end
it 'returns the saved setting even locale default exists' do
expect(settings.test_override).to eq('default')
settings.default_locale = 'zh_CN'
settings.test_override = 'saved'
expect(settings.test_override).to eq('saved')
end
end
describe '.requires_refresh?' do
it 'always refresh default_locale always require refresh' do
expect(settings.requires_refresh?(:default_locale)).to be_truthy
end
end
describe '.default_locale' do
it 'is always loaded' do
expect(settings.default_locale).to eq 'en'
end
end
describe '.default_locale=' do
it 'can be changed' do
settings.default_locale = 'zh_CN'
expect(settings.default_locale).to eq 'zh_CN'
end
it 'refresh!' do
settings.expects(:refresh!)
settings.default_locale = 'zh_CN'
end
it 'expires the cache' do
settings.default_locale = 'zh_CN'
expect(Rails.cache.exist?(SiteSettingExtension.client_settings_cache_key)).to be_falsey
end
it 'refreshes the client' do
Discourse.expects(:request_refresh!)
settings.default_locale = 'zh_CN'
end
end
end

View File

@ -1,268 +0,0 @@
require 'rails_helper'
require_dependency 'site_settings/defaults_provider'
describe SiteSettings::DefaultsProvider do
let :provider_local do
SiteSettings::LocalProcessProvider.new
end
def new_settings(provider)
Class.new do
extend SiteSettingExtension
self.provider = provider
end
end
let :settings do
new_settings(provider_local)
end
describe 'inserts default_locale into refresh' do
it 'when initialize' do
expect(settings.refresh_settings.include?(SiteSettings::DefaultsProvider::DEFAULT_LOCALE_KEY)).to be_truthy
end
end
describe '.db_all' do
it 'collects values from db except default locale' do
settings.provider.save(SiteSettings::DefaultsProvider::DEFAULT_LOCALE_KEY,
'en',
SiteSetting.types[:string])
expect(settings.defaults.db_all).to eq([])
end
it 'can collect values from db' do
settings.provider.save('try_a', 1, SiteSetting.types[:integer])
settings.provider.save('try_b', 2, SiteSetting.types[:integer])
expect(settings.defaults.db_all.count).to eq 2
end
end
describe 'expose default cache according to locale' do
before(:each) do
settings.setting(:test_override, 'default', locale_default: { zh_CN: 'cn' })
settings.setting(:test_default, 'test', regex: '^\S+$')
settings.refresh!
end
describe '.all' do
it 'returns all values according to the current locale' do
expect(settings.defaults.all).to eq(test_override: 'default', test_default: 'test')
settings.defaults.site_locale = 'zh_CN'
settings.defaults.refresh_site_locale!
expect(settings.defaults.all).to eq(test_override: 'cn', test_default: 'test')
end
end
describe '.get' do
it 'returns the default value to a site setting' do
expect(settings.defaults.get(:test_override)).to eq 'default'
end
it 'accepts a string as the parameters' do
expect(settings.defaults.get('test_override')).to eq 'default'
end
it 'returns the default value according to current locale' do
expect(settings.defaults.get(:test_override)).to eq 'default'
settings.defaults.site_locale = 'zh_CN'
expect(settings.defaults.get(:test_override)).to eq 'cn'
end
end
describe '.set_regardless_of_locale' do
let(:val) { 'env_overriden' }
it 'sets the default value to a site setting regardless the locale' do
settings.defaults.set_regardless_of_locale(:test_override, val)
expect(settings.defaults.get(:test_override)).to eq val
settings.defaults.site_locale = 'zh_CN'
expect(settings.defaults.get(:test_override)).to eq val
end
it 'handles the string' do
settings.defaults.set_regardless_of_locale('test_override', val)
expect(settings.defaults.get(:test_override)).to eq val
end
it 'converts the data type' do
settings.defaults.set_regardless_of_locale(:test_override, 1)
expect(settings.defaults.get(:test_override)).to eq '1'
end
it 'raises when the setting does not exists' do
expect {
settings.defaults.set_regardless_of_locale(:not_exist, 1)
}.to raise_error(ArgumentError)
end
it 'raises when the value is not valid' do
expect {
settings.defaults.set_regardless_of_locale(:test_default, 'regex will fail')
}.to raise_error(Discourse::InvalidParameters)
end
end
describe '.each' do
it 'yields the pair of site settings' do
expect { |b| settings.defaults.each(&b) }.to yield_successive_args([:test_override, 'default'], [:test_default, 'test'])
settings.defaults.site_locale = 'zh_CN'
expect { |b| settings.defaults.each(&b) }.to yield_successive_args([:test_override, 'cn'], [:test_default, 'test'])
end
end
end
describe '.site_locale' do
it 'returns the current site locale' do
expect(settings.defaults.site_locale).to eq 'en'
end
context 'when locale is set in the db' do
let(:db_val) { 'zr' }
let(:global_val) { 'gr' }
before do
settings.provider.save(SiteSettings::DefaultsProvider::DEFAULT_LOCALE_KEY,
db_val,
SiteSetting.types[:string])
settings.defaults.refresh_site_locale!
end
it 'should load from database' do
expect(settings.defaults.site_locale).to eq db_val
end
it 'prioritizes GlobalSetting than value from db' do
GlobalSetting.stubs(:default_locale).returns(global_val)
settings.defaults.refresh_site_locale!
expect(settings.defaults.site_locale).to eq global_val
end
it 'ignores blank GlobalSetting' do
GlobalSetting.stubs(:default_locale).returns('')
settings.defaults.refresh_site_locale!
expect(settings.defaults.site_locale).to eq db_val
end
end
end
describe '.site_locale=' do
it 'changes and store the current site locale' do
settings.defaults.site_locale = 'zh_CN'
expect(settings.defaults.site_locale).to eq 'zh_CN'
end
it 'changes and store the current site locale' do
expect { settings.defaults.site_locale = 'random' }.to raise_error(Discourse::InvalidParameters)
expect(settings.defaults.site_locale).to eq 'en'
end
it "don't change when it's shadowed" do
GlobalSetting.stubs(:default_locale).returns('shadowed')
settings.defaults.site_locale = 'zh_CN'
expect(settings.defaults.site_locale).to eq 'shadowed'
end
it 'refresh_site_locale! when called' do
settings.defaults.expects(:refresh_site_locale!)
settings.defaults.site_locale = 'zh_CN'
end
it 'refreshes the client when changed' do
Discourse.expects(:request_refresh!).once
settings.defaults.site_locale = 'zh_CN'
end
it "doesn't refresh the client when changed" do
Discourse.expects(:request_refresh!).never
settings.defaults.site_locale = 'en'
end
end
describe '.locale_setting_hash' do
it 'returns the hash for client display' do
result = settings.defaults.locale_setting_hash
expect(result[:setting]).to eq(SiteSettings::DefaultsProvider::DEFAULT_LOCALE_KEY)
expect(result[:default]).to eq(SiteSettings::DefaultsProvider::DEFAULT_LOCALE)
expect(result[:type]).to eq(SiteSetting.types[SiteSetting.types[:enum]])
expect(result[:preview]).to be_nil
expect(result[:value]).to eq(SiteSettings::DefaultsProvider::DEFAULT_LOCALE)
expect(result[:category]).to eq(SiteSettings::DefaultsProvider::DEFAULT_CATEGORY)
expect(result[:valid_values]).to eq(LocaleSiteSetting.values)
expect(result[:translate_names]).to eq(LocaleSiteSetting.translate_names?)
expect(result[:description]).not_to be_nil
end
end
describe '.load_setting' do
it 'adds a setting to the cache' do
settings.defaults.load_setting('new_a', 1)
expect(settings.defaults[:new_a]).to eq 1
end
it 'takes care of locale default' do
settings.defaults.load_setting(:new_b, 1, locale_default: { zh_CN: 2, zh_TW: 2 })
expect(settings.defaults[:new_b]).to eq 1
end
end
describe '.refresh_site_locale!' do
it 'loads the change to locale' do
expect(settings.defaults.site_locale).to eq 'en'
settings.provider.save(SiteSettings::DefaultsProvider::DEFAULT_LOCALE_KEY,
'zh_CN',
SiteSetting.types[:string])
settings.defaults.refresh_site_locale!
expect(settings.defaults.site_locale).to eq 'zh_CN'
end
it 'loads from GlobalSettings' do
expect(settings.defaults.site_locale).to eq 'en'
GlobalSetting.stubs(:default_locale).returns('fr')
settings.defaults.refresh_site_locale!
expect(settings.defaults.site_locale).to eq 'fr'
end
it 'prioritized GlobalSettings than db' do
expect(settings.defaults.site_locale).to eq 'en'
settings.provider.save(SiteSettings::DefaultsProvider::DEFAULT_LOCALE_KEY,
'zh_CN',
SiteSetting.types[:string])
GlobalSetting.stubs(:default_locale).returns('fr')
settings.defaults.refresh_site_locale!
expect(settings.defaults.site_locale).to eq 'fr'
end
end
describe '.has_setting?' do
before do
settings.setting(:r, 1)
settings.setting(:question?, 1)
end
it "returns true when it's present in the cache" do
expect(settings.defaults.has_setting?(:r)).to be_truthy
end
it '"responds when the arg is string' do
expect(settings.defaults.has_setting?('r')).to be_truthy
end
it 'default_locale always exists' do
expect(settings.defaults.has_setting?(SiteSettings::DefaultsProvider::DEFAULT_LOCALE_KEY)).to be_truthy
end
it 'returns false when the key is not exist' do
expect(settings.defaults.has_setting?('no_key')).to be_falsey
end
it 'checks name with question mark' do
expect(settings.defaults.has_setting?(:question)).to be_truthy
expect(settings.defaults.has_setting?('question')).to be_truthy
end
end
end

View File

@ -1,341 +0,0 @@
require 'rails_helper'
require_dependency 'site_settings/type_supervisor'
describe SiteSettings::TypeSupervisor do
let :provider_local do
SiteSettings::LocalProcessProvider.new
end
def new_settings(provider)
Class.new do
extend SiteSettingExtension
self.provider = provider
end
end
let :settings do
new_settings(provider_local)
end
subject { SiteSettings::TypeSupervisor }
describe 'constants' do
it 'validator opts are the subset of consumed opts' do
expect(Set.new(SiteSettings::TypeSupervisor::CONSUMED_OPTS).superset?(
Set.new(SiteSettings::TypeSupervisor::VALIDATOR_OPTS))).to be_truthy
end
end
describe '#types' do
context "verify enum sequence" do
it "'string' should be at 1st position" do
expect(SiteSettings::TypeSupervisor.types[:string]).to eq(1)
end
it "'time' should be at 2nd position" do
expect(SiteSettings::TypeSupervisor.types[:time]).to eq(2)
end
it "'integer' should be at 3rd position" do
expect(SiteSettings::TypeSupervisor.types[:integer]).to eq(3)
end
it "'float' should be at 4th position" do
expect(SiteSettings::TypeSupervisor.types[:float]).to eq(4)
end
it "'bool' should be at 5th position" do
expect(SiteSettings::TypeSupervisor.types[:bool]).to eq(5)
end
it "'null' should be at 6th position" do
expect(SiteSettings::TypeSupervisor.types[:null]).to eq(6)
end
it "'enum' should be at 7th position" do
expect(SiteSettings::TypeSupervisor.types[:enum]).to eq(7)
end
it "'list' should be at 8th position" do
expect(SiteSettings::TypeSupervisor.types[:list]).to eq(8)
end
it "'url_list' should be at 9th position" do
expect(SiteSettings::TypeSupervisor.types[:url_list]).to eq(9)
end
it "'host_list' should be at 10th position" do
expect(SiteSettings::TypeSupervisor.types[:host_list]).to eq(10)
end
it "'category_list' should be at 11th position" do
expect(SiteSettings::TypeSupervisor.types[:category_list]).to eq(11)
end
it "'value_list' should be at 12th position" do
expect(SiteSettings::TypeSupervisor.types[:value_list]).to eq(12)
end
it "'regex' should be at 13th position" do
expect(SiteSettings::TypeSupervisor.types[:regex]).to eq(13)
end
it "'email' should be at 14th position" do
expect(SiteSettings::TypeSupervisor.types[:email]).to eq(14)
end
it "'username' should be at 15th position" do
expect(SiteSettings::TypeSupervisor.types[:username]).to eq(15)
end
end
end
describe '#parse_value_type' do
it 'returns :null type when the value is nil' do
expect(subject.parse_value_type(nil)).to eq(SiteSetting.types[:null])
end
it 'returns :integer type when the value is int' do
expect(subject.parse_value_type(2)).to eq(SiteSetting.types[:integer])
end
it 'returns :integer type when the value is large int' do
expect(subject.parse_value_type(99999999999999999999999999999999999)).to eq(SiteSetting.types[:integer])
end
it 'returns :float type when the value is float' do
expect(subject.parse_value_type(1.23)).to eq(SiteSetting.types[:float])
end
it 'returns :bool type when the value is true' do
expect(subject.parse_value_type(true)).to eq(SiteSetting.types[:bool])
end
it 'returns :bool type when the value is false' do
expect(subject.parse_value_type(false)).to eq(SiteSetting.types[:bool])
end
it 'raises when the value is not listed' do
expect {
subject.parse_value_type(Object.new)
}.to raise_error ArgumentError
end
end
context 'with different data types' do
class TestEnumClass
def self.valid_value?(v)
self.values.include?(v)
end
def self.values
['en']
end
def self.translate_names?
false
end
end
class TestSmallThanTenValidator
def initialize(opts)
end
def valid_value?(v)
v < 10
end
def error_message
''
end
end
before do
settings.setting(:type_null, nil)
settings.setting(:type_int, 1)
settings.setting(:type_true, true)
settings.setting(:type_false, false)
settings.setting(:type_float, 2.3232)
settings.setting(:type_string, 'string')
settings.setting(:type_enum_default_string, '2', type: 'enum', choices: ['2'])
settings.setting(:type_enum_class, 'en', enum: 'TestEnumClass')
settings.setting(:type_validator, 5, validator: 'TestSmallThanTenValidator')
settings.setting(:type_mock_validate_method, 'no_value')
settings.setting(:type_custom, 'custom', type: 'list')
settings.refresh!
end
describe '.to_db_value' do
let(:true_val) { 't' }
let(:false_val) { 'f' }
it 'returns nil value' do
expect(settings.type_supervisor.to_db_value(:type_null, nil)).to eq [nil, SiteSetting.types[:null]]
end
it 'gives a second chance to guess even told :null type' do
expect(settings.type_supervisor.to_db_value(:type_null, 1)).to eq [1, SiteSetting.types[:integer]]
end
it 'writes `t` or `f` given the possible bool value' do
expect(settings.type_supervisor.to_db_value(:type_true, true)).to eq [true_val, SiteSetting.types[:bool]]
expect(settings.type_supervisor.to_db_value(:type_true, 't')).to eq [true_val, SiteSetting.types[:bool]]
expect(settings.type_supervisor.to_db_value(:type_true, 'true')).to eq [true_val, SiteSetting.types[:bool]]
expect(settings.type_supervisor.to_db_value(:type_true, false)).to eq [false_val, SiteSetting.types[:bool]]
end
it 'writes `f` if given not `true` value' do
expect(settings.type_supervisor.to_db_value(:type_true, '')).to eq [false_val, SiteSetting.types[:bool]]
expect(settings.type_supervisor.to_db_value(:type_true, nil)).to eq [false_val, SiteSetting.types[:bool]]
end
it 'returns floats value' do
expect(settings.type_supervisor.to_db_value(:type_float, 1.2)).to eq [1.2, SiteSetting.types[:float]]
expect(settings.type_supervisor.to_db_value(:type_float, 1)).to eq [1.0, SiteSetting.types[:float]]
end
it 'returns string value' do
expect(settings.type_supervisor.to_db_value(:type_string, 'a')).to eq ['a', SiteSetting.types[:string]]
end
it 'returns enum value with string default' do
expect(settings.type_supervisor.to_db_value(:type_enum_default_string, 2)).to eq ['2', SiteSetting.types[:enum]]
expect(settings.type_supervisor.to_db_value(:type_enum_default_string, '2')).to eq ['2', SiteSetting.types[:enum]]
end
it 'raises when it does not in the enum choices' do
expect {
settings.type_supervisor.to_db_value(:type_enum_default_string, 'random')
}.to raise_error Discourse::InvalidParameters
end
it 'returns enum value for the given enum class' do
expect(settings.type_supervisor.to_db_value(:type_enum_class, 'en')).to eq ['en', SiteSetting.types[:enum]]
end
it 'raises when it does not in the enum class' do
expect {
settings.type_supervisor.to_db_value(:type_enum_class, 'random')
}.to raise_error Discourse::InvalidParameters
end
it 'validates value by validator' do
expect(settings.type_supervisor.to_db_value(:type_validator, 1)).to eq [1, SiteSetting.types[:integer]]
end
it 'raises when the validator says so' do
expect {
settings.type_supervisor.to_db_value(:type_validator, 11)
}.to raise_error Discourse::InvalidParameters
end
it 'tries invoke validate methods' do
settings.type_supervisor.expects(:validate_type_mock_validate_method).with('no')
settings.type_supervisor.to_db_value(:type_mock_validate_method, 'no')
end
end
describe '.to_rb_value' do
let(:true_val) { 't' }
let(:false_val) { 'f' }
it 'the type can be overriden by a parameter' do
expect(settings.type_supervisor.to_rb_value(:type_null, '1', SiteSetting.types[:integer])).to eq(1)
end
it 'returns nil value' do
expect(settings.type_supervisor.to_rb_value(:type_null, '1')).to eq nil
expect(settings.type_supervisor.to_rb_value(:type_null, 1)).to eq nil
expect(settings.type_supervisor.to_rb_value(:type_null, 'null')).to eq nil
expect(settings.type_supervisor.to_rb_value(:type_null, 'nil')).to eq nil
end
it 'returns true when it is true or `t` or `true`' do
expect(settings.type_supervisor.to_rb_value(:type_true, true)).to eq true
expect(settings.type_supervisor.to_rb_value(:type_true, 't')).to eq true
expect(settings.type_supervisor.to_rb_value(:type_true, 'true')).to eq true
end
it 'returns false if not one of `true` value' do
expect(settings.type_supervisor.to_rb_value(:type_true, 'tr')).to eq false
expect(settings.type_supervisor.to_rb_value(:type_true, '')).to eq false
expect(settings.type_supervisor.to_rb_value(:type_true, nil)).to eq false
expect(settings.type_supervisor.to_rb_value(:type_true, false)).to eq false
expect(settings.type_supervisor.to_rb_value(:type_true, 'f')).to eq false
expect(settings.type_supervisor.to_rb_value(:type_true, 'false')).to eq false
end
it 'returns float value' do
expect(settings.type_supervisor.to_rb_value(:type_float, 1.2)).to eq 1.2
expect(settings.type_supervisor.to_rb_value(:type_float, 1)).to eq 1.0
expect(settings.type_supervisor.to_rb_value(:type_float, '2.2')).to eq 2.2
expect(settings.type_supervisor.to_rb_value(:type_float, '2')).to eq 2
end
it 'returns string value' do
expect(settings.type_supervisor.to_rb_value(:type_string, 'a')).to eq 'a'
expect(settings.type_supervisor.to_rb_value(:type_string, 2)).to eq '2'
end
it 'returns value with string default' do
expect(settings.type_supervisor.to_rb_value(:type_enum_default_string, 2)).to eq '2'
expect(settings.type_supervisor.to_rb_value(:type_enum_default_string, '2')).to eq '2'
end
it 'returns value with a custom type' do
expect(settings.type_supervisor.to_rb_value(:type_custom, 2)).to eq 2
expect(settings.type_supervisor.to_rb_value(:type_custom, '2|3')).to eq '2|3'
end
end
end
describe '.type_hash' do
class TestEnumClass2
def self.valid_value?(v)
self.values.include?(v)
end
def self.values
['a', 'b']
end
def self.translate_names?
false
end
end
before do
settings.setting(:type_null, nil)
settings.setting(:type_int, 1)
settings.setting(:type_true, true)
settings.setting(:type_float, 2.3232)
settings.setting(:type_string, 'string')
settings.setting(:type_url_list, 'string', type: 'url_list')
settings.setting(:type_enum_choices, '2', type: 'enum', choices: ['1', '2'])
settings.setting(:type_enum_class, 'a', enum: 'TestEnumClass2')
settings.setting(:type_list, 'a', type: 'list', choices: ['a', 'b'])
settings.refresh!
end
it 'returns null type' do
expect(settings.type_supervisor.type_hash(:type_null)[:type]).to eq 'null'
end
it 'returns int type' do
expect(settings.type_supervisor.type_hash(:type_int)[:type]).to eq 'integer'
end
it 'returns bool type' do
expect(settings.type_supervisor.type_hash(:type_true)[:type]).to eq 'bool'
end
it 'returns float type' do
expect(settings.type_supervisor.type_hash(:type_float)[:type]).to eq 'float'
end
it 'returns string type' do
expect(settings.type_supervisor.type_hash(:type_string)[:type]).to eq 'string'
end
it 'returns url_list type' do
expect(settings.type_supervisor.type_hash(:type_url_list)[:type]).to eq 'url_list'
end
it 'returns enum type' do
expect(settings.type_supervisor.type_hash(:type_enum_choices)[:type]).to eq 'enum'
end
it 'returns list choices' do
expect(settings.type_supervisor.type_hash(:type_list)[:choices]).to eq ['a', 'b']
end
it 'returns enum choices' do
hash = settings.type_supervisor.type_hash(:type_enum_choices)
expect(hash[:valid_values]).to eq [{ name: '1', value: '1' }, { name: '2', value: '2' }]
expect(hash[:translate_names]).to eq false
end
it 'returns enum class' do
hash = settings.type_supervisor.type_hash(:type_enum_class)
expect(hash[:valid_values]).to eq ['a', 'b']
expect(hash[:translate_names]).to eq false
end
end
end

View File

@ -8,18 +8,26 @@ describe SiteSettings::YamlLoader do
def load_yaml(file_arg)
SiteSettings::YamlLoader.new(file_arg).load do |category, name, default, opts|
setting(category, name, default, opts)
if opts.delete(:client)
client_setting(category, name, default, opts)
else
setting(category, name, default, opts)
end
end
end
def setting(category, name, default = nil, opts = {})
@settings ||= []
@client_settings ||= []
@settings << name
@categories ||= []
@categories << category
@categories.uniq!
@client_settings << name if opts.has_key?(:client)
end
def client_setting(category, name, default = nil)
@client_settings ||= []
@client_settings << name
setting(category, name, default)
end
end
@ -28,9 +36,7 @@ describe SiteSettings::YamlLoader do
let(:client) { "#{Rails.root}/spec/fixtures/site_settings/client.yml" }
let(:enum) { "#{Rails.root}/spec/fixtures/site_settings/enum.yml" }
let(:enum_client) { "#{Rails.root}/spec/fixtures/site_settings/enum_client.yml" }
let(:deprecated_env) { "#{Rails.root}/spec/fixtures/site_settings/deprecated_env.yml" }
let(:deprecated_hidden) { "#{Rails.root}/spec/fixtures/site_settings/deprecated_hidden.yml" }
let(:locale_default) { "#{Rails.root}/spec/fixtures/site_settings/locale_default.yml" }
let(:env) { "#{Rails.root}/spec/fixtures/site_settings/env.yml" }
it "loads simple settings" do
receiver.expects(:setting).with('category1', 'title', 'My Site', {}).once
@ -51,9 +57,9 @@ describe SiteSettings::YamlLoader do
end
it "can load client settings" do
receiver.expects(:setting).with('category1', 'title', 'Discourse', client: true)
receiver.expects(:setting).with('category2', 'tos_url', '', client: true)
receiver.expects(:setting).with('category2', 'must_approve_users', false, client: true)
receiver.expects(:client_setting).with('category1', 'title', 'Discourse', {})
receiver.expects(:client_setting).with('category2', 'tos_url', '', {})
receiver.expects(:client_setting).with('category2', 'must_approve_users', false, {})
receiver.load_yaml(client)
end
@ -63,22 +69,15 @@ describe SiteSettings::YamlLoader do
end
it "can load enum client settings" do
receiver.expects(:setting).with do |category, name, default, opts|
category == ('basics') && name == ('default_locale') && default == ('en') && opts[:enum] == ('LocaleSiteSetting') && opts[:client] == true
receiver.expects(:client_setting).with do |category, name, default, opts|
category == ('basics') && name == ('default_locale') && default == ('en') && opts[:enum] == ('LocaleSiteSetting')
end
receiver.load_yaml(enum_client)
end
it "raises deprecation when load settings based on environment" do
expect { receiver.load_yaml(deprecated_env) }.to raise_error(Discourse::Deprecation)
end
it "raises deprecation when hidden property is based on environment" do
expect { receiver.load_yaml(deprecated_hidden) }.to raise_error(Discourse::Deprecation)
end
it "can load settings with locale default" do
receiver.expects(:setting).with('search', 'min_search_term_length', 3, min: 2, client: true, locale_default: { zh_CN: 2, zh_TW: 2 })
receiver.load_yaml(locale_default)
it "can load settings based on environment" do
receiver.expects(:setting).with('misc', 'port', '', {})
receiver.expects(:client_setting).with('misc', 'crawl_images', false, {})
receiver.load_yaml(env)
end
end