diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index 703e7e9fd3b..7701e89cfc3 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -65,6 +65,9 @@ module SiteSettingExtension def setting(name_arg, default = nil, opts = {}) name = name_arg.to_sym + + shadowed_val = nil + mutex.synchronize do defaults.load_setting( name, @@ -82,6 +85,7 @@ module SiteSettingExtension val = GlobalSetting.send(name) unless val.nil? || (val == ''.freeze) + shadowed_val = val hidden_settings << name shadowed_settings << name end @@ -104,7 +108,11 @@ module SiteSettingExtension opts.extract!(*SiteSettings::TypeSupervisor::CONSUMED_OPTS) ) - setup_methods(name) + if !shadowed_val.nil? + setup_shadowed_methods(name, shadowed_val) + else + setup_methods(name) + end end end @@ -291,6 +299,24 @@ module SiteSettingExtension [changes, deletions] end + def setup_shadowed_methods(name, value) + clean_name = name.to_s.sub("?", "").to_sym + + define_singleton_method clean_name do + value + end + + define_singleton_method "#{clean_name}?" do + value + end + + define_singleton_method "#{clean_name}=" do |val| + Rails.logger.warn("An attempt was to change #{clean_name} SiteSetting to #{val} however it is shadowed so this will be ignored!") + nil + end + + end + def setup_methods(name) clean_name = name.to_s.sub("?", "").to_sym diff --git a/spec/components/site_setting_extension_spec.rb b/spec/components/site_setting_extension_spec.rb index 1c2d8cb7f9f..8adf3c15567 100644 --- a/spec/components/site_setting_extension_spec.rb +++ b/spec/components/site_setting_extension_spec.rb @@ -472,6 +472,7 @@ describe SiteSettingExtension do it "should return default cause nothing is set" do expect(settings.nada).to eq('nothing') end + end context "with a false override" do @@ -484,6 +485,14 @@ describe SiteSettingExtension do it "should return default cause nothing is set" do expect(settings.bool).to eq(false) end + + it "should not trigger any message bus work if you try to set it" do + m = MessageBus.track_publish('/site_settings') do + settings.bool = true + expect(settings.bool).to eq(false) + end + expect(m.length).to eq(0) + end end context "with global setting" do