##// END OF EJS Templates
Adds a method to temporarily override configuration settings....
Jean-Philippe Lang -
r4829:e1ad561cf652
parent child
Show More
@@ -1,97 +1,107
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module Configuration
20 20
21 21 # Configuration default values
22 22 @defaults = {
23 23 'email_delivery' => nil
24 24 }
25 25
26 26 @config = nil
27 27
28 28 class << self
29 29 # Loads the Redmine configuration file
30 30 # Valid options:
31 31 # * <tt>:file</tt>: the configuration file to load (default: config/configuration.yml)
32 32 # * <tt>:env</tt>: the environment to load the configuration for (default: Rails.env)
33 33 def load(options={})
34 34 filename = options[:file] || File.join(Rails.root, 'config', 'configuration.yml')
35 35 env = options[:env] || Rails.env
36 36
37 37 @config = @defaults.dup
38 38
39 39 load_deprecated_email_configuration(env)
40 40 if File.file?(filename)
41 41 @config.merge!(load_from_yaml(filename, env))
42 42 end
43 43
44 44 # Compatibility mode for those who copy email.yml over configuration.yml
45 45 %w(delivery_method smtp_settings sendmail_settings).each do |key|
46 46 if value = @config.delete(key)
47 47 @config['email_delivery'] ||= {}
48 48 @config['email_delivery'][key] = value
49 49 end
50 50 end
51 51
52 52 if @config['email_delivery']
53 53 ActionMailer::Base.perform_deliveries = true
54 54 @config['email_delivery'].each do |k, v|
55 55 v.symbolize_keys! if v.respond_to?(:symbolize_keys!)
56 56 ActionMailer::Base.send("#{k}=", v)
57 57 end
58 58 end
59 59
60 60 @config
61 61 end
62 62
63 63 # Returns a configuration setting
64 64 def [](name)
65 65 load unless @config
66 66 @config[name]
67 67 end
68 68
69 # Yields a block with the specified hash configuration settings
70 def with(settings)
71 settings.stringify_keys!
72 load unless @config
73 was = settings.keys.inject({}) {|h,v| h[v] = @config[v]; h}
74 @config.merge! settings
75 yield if block_given?
76 @config.merge! was
77 end
78
69 79 private
70 80
71 81 def load_from_yaml(filename, env)
72 82 yaml = YAML::load_file(filename)
73 83 conf = {}
74 84 if yaml.is_a?(Hash)
75 85 if yaml['default']
76 86 conf.merge!(yaml['default'])
77 87 end
78 88 if yaml[env]
79 89 conf.merge!(yaml[env])
80 90 end
81 91 else
82 92 $stderr.puts "#{filename} is not a valid Redmine configuration file"
83 93 exit 1
84 94 end
85 95 conf
86 96 end
87 97
88 98 def load_deprecated_email_configuration(env)
89 99 deprecated_email_conf = File.join(Rails.root, 'config', 'email.yml')
90 100 if File.file?(deprecated_email_conf)
91 101 warn "Storing outgoing emails configuration in config/email.yml is deprecated. You should now store it in config/configuration.yml using the email_delivery setting."
92 102 @config.merge!({'email_delivery' => load_from_yaml(deprecated_email_conf, env)})
93 103 end
94 104 end
95 105 end
96 106 end
97 107 end
@@ -1,52 +1,61
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../../test_helper', __FILE__)
19 19
20 20 class Redmine::ConfigurationTest < ActiveSupport::TestCase
21 21 def setup
22 22 @conf = Redmine::Configuration
23 23 end
24 24
25 25 def test_empty
26 26 assert_kind_of Hash, load_conf('empty.yml', 'test')
27 27 end
28 28
29 29 def test_default
30 30 assert_kind_of Hash, load_conf('default.yml', 'test')
31 31 assert_equal 'foo', @conf['somesetting']
32 32 end
33 33
34 34 def test_no_default
35 35 assert_kind_of Hash, load_conf('no_default.yml', 'test')
36 36 assert_equal 'foo', @conf['somesetting']
37 37 end
38 38
39 39 def test_overrides
40 40 assert_kind_of Hash, load_conf('overrides.yml', 'test')
41 41 assert_equal 'bar', @conf['somesetting']
42 42 end
43 43
44 def test_with
45 load_conf('default.yml', 'test')
46 assert_equal 'foo', @conf['somesetting']
47 @conf.with 'somesetting' => 'bar' do
48 assert_equal 'bar', @conf['somesetting']
49 end
50 assert_equal 'foo', @conf['somesetting']
51 end
52
44 53 private
45 54
46 55 def load_conf(file, env)
47 56 @conf.load(
48 57 :file => File.join(Rails.root, 'test', 'fixtures', 'configuration', file),
49 58 :env => env
50 59 )
51 60 end
52 61 end
General Comments 0
You need to be logged in to leave comments. Login now