diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2014-12-05 15:00:27 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2014-12-05 15:00:27 +0000 |
commit | ca71cf380046d74d71e344da0dd85eed9de18f31 (patch) | |
tree | ee780c5c7a15ed41b136cfd7b80537442cbaa12f | |
parent | c084ef509f114d0d32891fc58d45817e6e11fa4e (diff) | |
download | redmine-ca71cf380046d74d71e344da0dd85eed9de18f31.tar.gz redmine-ca71cf380046d74d71e344da0dd85eed9de18f31.zip |
Adds methods for loading and adding settings.
git-svn-id: http://svn.redmine.org/redmine/trunk@13719 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r-- | app/models/setting.rb | 84 | ||||
-rw-r--r-- | lib/redmine/plugin.rb | 5 | ||||
-rw-r--r-- | test/functional/settings_controller_test.rb | 10 |
3 files changed, 63 insertions, 36 deletions
diff --git a/app/models/setting.rb b/app/models/setting.rb index c207de22b..bc02d29ce 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -75,16 +75,12 @@ class Setting < ActiveRecord::Base TIS-620) cattr_accessor :available_settings - @@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml")) - Redmine::Plugin.all.each do |plugin| - next unless plugin.settings - @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true} - end + self.available_settings ||= {} validates_uniqueness_of :name - validates_inclusion_of :name, :in => @@available_settings.keys + validates_inclusion_of :name, :in => Proc.new {available_settings.keys} validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| - (s = @@available_settings[setting.name]) && s['format'] == 'int' + (s = available_settings[setting.name]) && s['format'] == 'int' } attr_protected :id @@ -95,13 +91,13 @@ class Setting < ActiveRecord::Base def value v = read_attribute(:value) # Unserialize serialized settings - v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String) - v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank? + v = YAML::load(v) if available_settings[name]['serialized'] && v.is_a?(String) + v = v.to_sym if available_settings[name]['format'] == 'symbol' && !v.blank? v end def value=(v) - v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized'] + v = v.to_yaml if v && available_settings[name] && available_settings[name]['serialized'] write_attribute(:value, v.to_s) end @@ -119,26 +115,6 @@ class Setting < ActiveRecord::Base setting.value end - # Defines getter and setter for each setting - # Then setting values can be read using: Setting.some_setting_name - # or set using Setting.some_setting_name = "some value" - @@available_settings.each do |name, params| - src = <<-END_SRC - def self.#{name} - self[:#{name}] - end - - def self.#{name}? - self[:#{name}].to_i > 0 - end - - def self.#{name}=(value) - self[:#{name}] = value - end -END_SRC - class_eval src, __FILE__, __LINE__ - end - # Sets a setting value from params def self.set_from_params(name, params) params = params.dup @@ -217,17 +193,61 @@ END_SRC logger.info "Settings cache cleared." if logger end + def self.define_plugin_setting(plugin) + if plugin.settings + name = "plugin_#{plugin.id}" + define_setting name, {'default' => plugin.settings[:default], 'serialized' => true} + end + end + + # Defines getter and setter for each setting + # Then setting values can be read using: Setting.some_setting_name + # or set using Setting.some_setting_name = "some value" + def self.define_setting(name, options={}) + available_settings[name.to_s] = options + + src = <<-END_SRC + def self.#{name} + self[:#{name}] + end + + def self.#{name}? + self[:#{name}].to_i > 0 + end + + def self.#{name}=(value) + self[:#{name}] = value + end +END_SRC + class_eval src, __FILE__, __LINE__ + end + + def self.load_available_settings + YAML::load(File.open("#{Rails.root}/config/settings.yml")).each do |name, options| + define_setting name, options + end + end + + def self.load_plugin_settings + Redmine::Plugin.all.each do |plugin| + define_plugin_setting(plugin) + end + end + + load_available_settings + load_plugin_settings + private # Returns the Setting instance for the setting named name # (record found in database or new record with default value) def self.find_or_default(name) name = name.to_s - raise "There's no setting named #{name}" unless @@available_settings.has_key?(name) + raise "There's no setting named #{name}" unless available_settings.has_key?(name) setting = where(:name => name).first unless setting setting = new setting.name = name - setting.value = @@available_settings[name]['default'] + setting.value = available_settings[name]['default'] end setting end diff --git a/lib/redmine/plugin.rb b/lib/redmine/plugin.rb index 9c67399d9..0b0712072 100644 --- a/lib/redmine/plugin.rb +++ b/lib/redmine/plugin.rb @@ -95,6 +95,11 @@ module Redmine #:nodoc: ActiveSupport::Dependencies.autoload_paths += [dir] end + # Defines plugin setting if present + if p.settings + Setting.define_plugin_setting p + end + # Warn for potential settings[:partial] collisions if p.configurable? partial = p.settings[:partial] diff --git a/test/functional/settings_controller_test.rb b/test/functional/settings_controller_test.rb index 615eae47d..f5f6d46ee 100644 --- a/test/functional/settings_controller_test.rb +++ b/test/functional/settings_controller_test.rb @@ -137,10 +137,10 @@ class SettingsControllerTest < ActionController::TestCase end def test_get_plugin_settings - Setting.stubs(:plugin_foo).returns({'sample_setting' => 'Plugin setting value'}) ActionController::Base.append_view_path(File.join(Rails.root, "test/fixtures/plugins")) Redmine::Plugin.register :foo do - settings :partial => "foo_plugin/foo_plugin_settings" + settings :partial => "foo_plugin/foo_plugin_settings", + :default => {'sample_setting' => 'Plugin setting value'} end get :plugin, :id => 'foo' @@ -169,13 +169,15 @@ class SettingsControllerTest < ActionController::TestCase end def test_post_plugin_settings - Setting.expects(:plugin_foo=).with({'sample_setting' => 'Value'}).returns(true) Redmine::Plugin.register(:foo) do - settings :partial => 'not blank' # so that configurable? is true + settings :partial => 'not blank', # so that configurable? is true + :default => {'sample_setting' => 'Plugin setting value'} end post :plugin, :id => 'foo', :settings => {'sample_setting' => 'Value'} assert_redirected_to '/settings/plugin/foo' + + assert_equal({'sample_setting' => 'Value'}, Setting.plugin_foo) end def test_post_non_configurable_plugin_settings |