mirror of
https://github.com/redmine/redmine.git
synced 2025-11-08 06:15:59 +01:00
Adds methods for loading and adding settings.
git-svn-id: http://svn.redmine.org/redmine/trunk@13719 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
@@ -75,16 +75,12 @@ class Setting < ActiveRecord::Base
|
|||||||
TIS-620)
|
TIS-620)
|
||||||
|
|
||||||
cattr_accessor :available_settings
|
cattr_accessor :available_settings
|
||||||
@@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml"))
|
self.available_settings ||= {}
|
||||||
Redmine::Plugin.all.each do |plugin|
|
|
||||||
next unless plugin.settings
|
|
||||||
@@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
|
|
||||||
end
|
|
||||||
|
|
||||||
validates_uniqueness_of :name
|
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|
|
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
|
attr_protected :id
|
||||||
|
|
||||||
@@ -95,13 +91,13 @@ class Setting < ActiveRecord::Base
|
|||||||
def value
|
def value
|
||||||
v = read_attribute(:value)
|
v = read_attribute(:value)
|
||||||
# Unserialize serialized settings
|
# Unserialize serialized settings
|
||||||
v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
|
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 = v.to_sym if available_settings[name]['format'] == 'symbol' && !v.blank?
|
||||||
v
|
v
|
||||||
end
|
end
|
||||||
|
|
||||||
def value=(v)
|
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)
|
write_attribute(:value, v.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -119,26 +115,6 @@ class Setting < ActiveRecord::Base
|
|||||||
setting.value
|
setting.value
|
||||||
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"
|
|
||||||
@@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
|
# Sets a setting value from params
|
||||||
def self.set_from_params(name, params)
|
def self.set_from_params(name, params)
|
||||||
params = params.dup
|
params = params.dup
|
||||||
@@ -217,17 +193,61 @@ END_SRC
|
|||||||
logger.info "Settings cache cleared." if logger
|
logger.info "Settings cache cleared." if logger
|
||||||
end
|
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
|
private
|
||||||
# Returns the Setting instance for the setting named name
|
# Returns the Setting instance for the setting named name
|
||||||
# (record found in database or new record with default value)
|
# (record found in database or new record with default value)
|
||||||
def self.find_or_default(name)
|
def self.find_or_default(name)
|
||||||
name = name.to_s
|
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
|
setting = where(:name => name).first
|
||||||
unless setting
|
unless setting
|
||||||
setting = new
|
setting = new
|
||||||
setting.name = name
|
setting.name = name
|
||||||
setting.value = @@available_settings[name]['default']
|
setting.value = available_settings[name]['default']
|
||||||
end
|
end
|
||||||
setting
|
setting
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -95,6 +95,11 @@ module Redmine #:nodoc:
|
|||||||
ActiveSupport::Dependencies.autoload_paths += [dir]
|
ActiveSupport::Dependencies.autoload_paths += [dir]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Defines plugin setting if present
|
||||||
|
if p.settings
|
||||||
|
Setting.define_plugin_setting p
|
||||||
|
end
|
||||||
|
|
||||||
# Warn for potential settings[:partial] collisions
|
# Warn for potential settings[:partial] collisions
|
||||||
if p.configurable?
|
if p.configurable?
|
||||||
partial = p.settings[:partial]
|
partial = p.settings[:partial]
|
||||||
|
|||||||
@@ -137,10 +137,10 @@ class SettingsControllerTest < ActionController::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_get_plugin_settings
|
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"))
|
ActionController::Base.append_view_path(File.join(Rails.root, "test/fixtures/plugins"))
|
||||||
Redmine::Plugin.register :foo do
|
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
|
end
|
||||||
|
|
||||||
get :plugin, :id => 'foo'
|
get :plugin, :id => 'foo'
|
||||||
@@ -169,13 +169,15 @@ class SettingsControllerTest < ActionController::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_post_plugin_settings
|
def test_post_plugin_settings
|
||||||
Setting.expects(:plugin_foo=).with({'sample_setting' => 'Value'}).returns(true)
|
|
||||||
Redmine::Plugin.register(:foo) do
|
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
|
end
|
||||||
|
|
||||||
post :plugin, :id => 'foo', :settings => {'sample_setting' => 'Value'}
|
post :plugin, :id => 'foo', :settings => {'sample_setting' => 'Value'}
|
||||||
assert_redirected_to '/settings/plugin/foo'
|
assert_redirected_to '/settings/plugin/foo'
|
||||||
|
|
||||||
|
assert_equal({'sample_setting' => 'Value'}, Setting.plugin_foo)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_post_non_configurable_plugin_settings
|
def test_post_non_configurable_plugin_settings
|
||||||
|
|||||||
Reference in New Issue
Block a user