FEATURE: allow for additional plugin defined global settings

This commit is contained in:
Sam 2014-01-02 10:46:09 +11:00
parent 6a0072d36e
commit 2ad190b9f6
2 changed files with 29 additions and 4 deletions

View File

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

View File

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