DEV: Add plugin hook for transforming site setting defaults (#20941)

This commit is contained in:
David Taylor 2023-04-05 12:28:16 +01:00 committed by GitHub
parent 81566949f5
commit df849e51b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 9 deletions

View File

@ -31,18 +31,18 @@ class SiteSettings::DefaultsProvider
end
def all(locale = nil)
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

View File

@ -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

View File

@ -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