From 2ad190b9f685c4f9dbf58d887f5f2e74234e6026 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 2 Jan 2014 10:46:09 +1100 Subject: [PATCH] FEATURE: allow for additional plugin defined global settings --- app/models/global_setting.rb | 24 ++++++++++++++++++++---- spec/models/global_setting_spec.rb | 9 +++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb index 6f9ba3340f1..a94f1eb5276 100644 --- a/app/models/global_setting.rb +++ b/app/models/global_setting.rb @@ -1,10 +1,17 @@ class GlobalSetting + def self.register(key, default) + define_singleton_method(key) do + provider.lookup(key, default) + end + end + def self.load_defaults default_provider = FileProvider.from(File.expand_path('../../../config/discourse_defaults.conf', __FILE__)) - default_provider.data.each do |name, default| - define_singleton_method(name) do - provider.lookup(name, default) + default_provider.keys.concat(@provider.keys).uniq.each do |key| + default = default_provider.lookup(key, nil) + define_singleton_method(key) do + provider.lookup(key, default) end end end @@ -56,6 +63,10 @@ class GlobalSetting resolve(var, var.nil? ? default : "") end + def keys + @data.keys + end + private def self.parse(file) @@ -70,6 +81,10 @@ class GlobalSetting var = ENV["DISCOURSE_" << key.to_s.upcase] resolve(var , var.nil? ? default : nil) end + + def keys + ENV.keys.select{|k| k =~ /^DISCOURSE_/}.map{|k| k[10..-1].downcase.to_sym} + end end @@ -78,8 +93,9 @@ class GlobalSetting end - load_defaults @provider = FileProvider.from(File.expand_path('../../../config/discourse.conf', __FILE__)) || EnvProvider.new + + load_defaults end diff --git a/spec/models/global_setting_spec.rb b/spec/models/global_setting_spec.rb index e16b5a1bab5..54d81c9a9c0 100644 --- a/spec/models/global_setting_spec.rb +++ b/spec/models/global_setting_spec.rb @@ -1,6 +1,12 @@ require 'spec_helper' require 'tempfile' +describe GlobalSetting::EnvProvider do + it "can detect keys from env" do + ENV['DISCOURSE_BLA'] = '1' + GlobalSetting::EnvProvider.new.keys.should include(:bla) + end +end describe GlobalSetting::FileProvider do it "can parse a simple file" do f = Tempfile.new('foo') @@ -20,6 +26,9 @@ describe GlobalSetting::FileProvider do provider.lookup(:d,"bob").should == nil provider.lookup(:e,"bob").should == "bob" + provider.keys.sort.should == [:a, :b, :c, :d] + f.unlink end + end