annotate lib/redmine/configuration.rb @ 1478:5ca1f4a47171 bibplugin_db_migrations

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