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