From b60bc47a4cb7479088ff3c9694ec95d98e5a5e28 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 9 Jan 2017 17:10:14 -0500 Subject: [PATCH] Plugins can register providers for global settings --- app/models/global_setting.rb | 16 ++++++++-------- config/application.rb | 9 +++++++++ lib/custom_setting_providers.rb | 7 +++++++ lib/plugin/instance.rb | 22 +--------------------- lib/plugin_gem.rb | 27 +++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 lib/custom_setting_providers.rb create mode 100644 lib/plugin_gem.rb diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb index dd1c04820ef..a7600d48206 100644 --- a/app/models/global_setting.rb +++ b/app/models/global_setting.rb @@ -145,14 +145,14 @@ class GlobalSetting attr_accessor :provider end - - if Rails.env == "test" - @provider = BlankProvider.new - else - @provider = - FileProvider.from(File.expand_path('../../../config/discourse.conf', __FILE__)) || - EnvProvider.new + def self.configure! + if Rails.env == "test" + @provider = BlankProvider.new + else + @provider = + FileProvider.from(File.expand_path('../../../config/discourse.conf', __FILE__)) || + EnvProvider.new + end end - load_defaults end diff --git a/config/application.rb b/config/application.rb index c098564340b..49b35de4743 100644 --- a/config/application.rb +++ b/config/application.rb @@ -6,8 +6,15 @@ require_relative '../lib/discourse_event' require_relative '../lib/discourse_plugin' require_relative '../lib/discourse_plugin_registry' +require_relative '../lib/plugin_gem' + # Global config require_relative '../app/models/global_setting' +GlobalSetting.configure! +unless Rails.env.test? && ENV['LOAD_PLUGINS'] != "1" + require_relative '../lib/custom_setting_providers' +end +GlobalSetting.load_defaults require 'pry-rails' if Rails.env.development? @@ -15,8 +22,10 @@ if defined?(Bundler) Bundler.require(*Rails.groups(assets: %w(development test profile))) end + module Discourse class Application < Rails::Application + def config.database_configuration if Rails.env.production? GlobalSetting.database_config diff --git a/lib/custom_setting_providers.rb b/lib/custom_setting_providers.rb new file mode 100644 index 00000000000..370097d4247 --- /dev/null +++ b/lib/custom_setting_providers.rb @@ -0,0 +1,7 @@ +# Support for plugins to register custom setting providers. They can do this +# by having a file, `register_provider.rb` in their root that will be run +# at this point. + +Dir.glob(File.join(File.dirname(__FILE__), '../plugins', '*', "register_provider.rb")) do |p| + require p +end diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index a41ab8af8c0..393e4eb0867 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -363,27 +363,7 @@ JS # # This is a very rough initial implementation def gem(name, version, opts = {}) - gems_path = File.dirname(path) + "/gems/#{RUBY_VERSION}" - spec_path = gems_path + "/specifications" - spec_file = spec_path + "/#{name}-#{version}.gemspec" - unless File.exists? spec_file - command = "gem install #{name} -v #{version} -i #{gems_path} --no-document --ignore-dependencies" - if opts[:source] - command << " --source #{opts[:source]}" - end - puts command - puts `#{command}` - end - if File.exists? spec_file - spec = Gem::Specification.load spec_file - spec.activate - unless opts[:require] == false - require opts[:require_name] ? opts[:require_name] : name - end - else - puts "You are specifying the gem #{name} in #{path}, however it does not exist!" - exit(-1) - end + PluginGem.load(path, name, version, opts) end def enabled_site_setting(setting=nil) diff --git a/lib/plugin_gem.rb b/lib/plugin_gem.rb new file mode 100644 index 00000000000..48df5e57c8b --- /dev/null +++ b/lib/plugin_gem.rb @@ -0,0 +1,27 @@ +module PluginGem + def self.load(path, name, version, opts=nil) + opts ||= {} + + gems_path = File.dirname(path) + "/gems/#{RUBY_VERSION}" + spec_path = gems_path + "/specifications" + spec_file = spec_path + "/#{name}-#{version}.gemspec" + unless File.exists? spec_file + command = "gem install #{name} -v #{version} -i #{gems_path} --no-document --ignore-dependencies" + if opts[:source] + command << " --source #{opts[:source]}" + end + puts command + puts `#{command}` + end + if File.exists? spec_file + spec = Gem::Specification.load spec_file + spec.activate + unless opts[:require] == false + require opts[:require_name] ? opts[:require_name] : name + end + else + puts "You are specifying the gem #{name} in #{path}, however it does not exist!" + exit(-1) + end + end +end