mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 04:56:18 +08:00
DEV: Add plugin hook for transforming site setting defaults (#20941)
This commit is contained in:
parent
81566949f5
commit
df849e51b7
|
@ -31,18 +31,18 @@ class SiteSettings::DefaultsProvider
|
|||
end
|
||||
|
||||
def all(locale = nil)
|
||||
if locale
|
||||
@defaults[DEFAULT_LOCALE.to_sym].merge(@defaults[locale.to_sym] || {})
|
||||
else
|
||||
@defaults[DEFAULT_LOCALE.to_sym].dup
|
||||
end
|
||||
result =
|
||||
if locale
|
||||
@defaults[DEFAULT_LOCALE.to_sym].merge(@defaults[locale.to_sym] || {})
|
||||
else
|
||||
@defaults[DEFAULT_LOCALE.to_sym].dup
|
||||
end
|
||||
|
||||
DiscoursePluginRegistry.apply_modifier(:site_setting_defaults, result)
|
||||
end
|
||||
|
||||
def get(name, locale = DEFAULT_LOCALE)
|
||||
value = @defaults.dig(locale.to_sym, name.to_sym)
|
||||
return value unless value.nil?
|
||||
|
||||
@defaults.dig(DEFAULT_LOCALE.to_sym, name.to_sym)
|
||||
all(locale)[name.to_sym]
|
||||
end
|
||||
alias [] get
|
||||
|
||||
|
|
|
@ -128,4 +128,27 @@ RSpec.describe SiteSettings::DefaultsProvider do
|
|||
expect(settings.defaults.has_setting?("question")).to be_truthy
|
||||
end
|
||||
end
|
||||
|
||||
describe "plugin modifier" do
|
||||
before { settings.setting(:my_setting, "defaultval") }
|
||||
|
||||
class TestFilterPlugInstance < Plugin::Instance
|
||||
end
|
||||
|
||||
let(:plugin_instance) { TestFilterPlugInstance.new }
|
||||
|
||||
it "can change defaults" do
|
||||
expect(settings.defaults.get(:my_setting)).to eq "defaultval"
|
||||
expect(settings.defaults.all[:my_setting]).to eq "defaultval"
|
||||
|
||||
plugin_instance.register_modifier(:site_setting_defaults) do |defaults|
|
||||
defaults.merge({ my_setting: "overridden default" })
|
||||
end
|
||||
|
||||
expect(settings.defaults.get(:my_setting)).to eq "overridden default"
|
||||
expect(settings.defaults.all[:my_setting]).to eq "overridden default"
|
||||
ensure
|
||||
DiscoursePluginRegistry.reset!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,4 +29,37 @@ RSpec.describe "Multisite SiteSettings", type: :multisite do
|
|||
test_multisite_connection("second") { expect(SiteSetting.default_locale).to eq("zh_TW") }
|
||||
end
|
||||
end
|
||||
|
||||
describe "transforming defaults from plugin" do
|
||||
class TestFilterPlugInstance < Plugin::Instance
|
||||
end
|
||||
|
||||
let(:plugin_instance) { TestFilterPlugInstance.new }
|
||||
|
||||
it "can change defaults" do
|
||||
test_multisite_connection("default") { expect(SiteSetting.title).to eq("Discourse") }
|
||||
|
||||
plugin_instance.register_modifier(:site_setting_defaults) do |defaults|
|
||||
defaults.merge({ title: "title for #{RailsMultisite::ConnectionManagement.current_db}" })
|
||||
end
|
||||
|
||||
test_multisite_connection("default") do
|
||||
SiteSetting.refresh!
|
||||
expect(SiteSetting.title).to eq("title for default")
|
||||
SiteSetting.title = "overridden default title"
|
||||
expect(SiteSetting.title).to eq("overridden default title")
|
||||
end
|
||||
|
||||
test_multisite_connection("second") do
|
||||
SiteSetting.refresh!
|
||||
expect(SiteSetting.title).to eq("title for second")
|
||||
SiteSetting.title = "overridden second title"
|
||||
expect(SiteSetting.title).to eq("overridden second title")
|
||||
end
|
||||
ensure
|
||||
DiscoursePluginRegistry.reset!
|
||||
test_multisite_connection("default") { SiteSetting.refresh! }
|
||||
test_multisite_connection("second") { SiteSetting.refresh! }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user