From 20fbf81505a5c0ebc5110eb1ea20447034f9a7c4 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 16 Nov 2017 08:38:24 +1100 Subject: [PATCH] FEATURE: Allow plugins to easily add defaults to GlobalSetting usage: GlobalSetting.add_default("plugin_setting_name", "value") --- app/models/global_setting.rb | 8 ++++++++ spec/models/global_setting_spec.rb | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb index 33535335d7c..fabc777690f 100644 --- a/app/models/global_setting.rb +++ b/app/models/global_setting.rb @@ -137,6 +137,14 @@ class GlobalSetting end end + def self.add_default(name, default) + unless self.respond_to? name + define_singleton_method(name) do + default + end + end + end + class BaseProvider def self.coerce(setting) return setting == "true" if setting == "true" || setting == "false" diff --git a/spec/models/global_setting_spec.rb b/spec/models/global_setting_spec.rb index c02b383a947..e3fd9a9f515 100644 --- a/spec/models/global_setting_spec.rb +++ b/spec/models/global_setting_spec.rb @@ -50,6 +50,20 @@ describe GlobalSetting do end end + describe '.add_default' do + after do + class <