Chris@909: # Redmine - project management software
Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909: #
Chris@909: # This program is free software; you can redistribute it and/or
Chris@909: # modify it under the terms of the GNU General Public License
Chris@909: # as published by the Free Software Foundation; either version 2
Chris@909: # of the License, or (at your option) any later version.
Chris@909: #
Chris@909: # This program is distributed in the hope that it will be useful,
Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909: # GNU General Public License for more details.
Chris@909: #
Chris@909: # You should have received a copy of the GNU General Public License
Chris@909: # along with this program; if not, write to the Free Software
Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909:
Chris@909: module Redmine
Chris@909: module Configuration
Chris@909:
Chris@909: # Configuration default values
Chris@909: @defaults = {
Chris@909: 'email_delivery' => nil
Chris@909: }
Chris@909:
Chris@909: @config = nil
Chris@909:
Chris@909: class << self
Chris@909: # Loads the Redmine configuration file
Chris@909: # Valid options:
Chris@909: # * :file: the configuration file to load (default: config/configuration.yml)
Chris@909: # * :env: the environment to load the configuration for (default: Rails.env)
Chris@909: def load(options={})
Chris@909: filename = options[:file] || File.join(Rails.root, 'config', 'configuration.yml')
Chris@909: env = options[:env] || Rails.env
Chris@909:
Chris@909: @config = @defaults.dup
Chris@909:
Chris@909: load_deprecated_email_configuration(env)
Chris@909: if File.file?(filename)
Chris@909: @config.merge!(load_from_yaml(filename, env))
Chris@909: end
Chris@909:
Chris@909: # Compatibility mode for those who copy email.yml over configuration.yml
Chris@909: %w(delivery_method smtp_settings sendmail_settings).each do |key|
Chris@909: if value = @config.delete(key)
Chris@909: @config['email_delivery'] ||= {}
Chris@909: @config['email_delivery'][key] = value
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: if @config['email_delivery']
Chris@909: ActionMailer::Base.perform_deliveries = true
Chris@909: @config['email_delivery'].each do |k, v|
Chris@909: v.symbolize_keys! if v.respond_to?(:symbolize_keys!)
Chris@909: ActionMailer::Base.send("#{k}=", v)
Chris@909: end
Chris@909: end
Chris@909:
Chris@909: @config
Chris@909: end
Chris@909:
Chris@909: # Returns a configuration setting
Chris@909: def [](name)
Chris@909: load unless @config
Chris@909: @config[name]
Chris@909: end
Chris@909:
Chris@909: # Yields a block with the specified hash configuration settings
Chris@909: def with(settings)
Chris@909: settings.stringify_keys!
Chris@909: load unless @config
Chris@909: was = settings.keys.inject({}) {|h,v| h[v] = @config[v]; h}
Chris@909: @config.merge! settings
Chris@909: yield if block_given?
Chris@909: @config.merge! was
Chris@909: end
Chris@909:
Chris@909: private
Chris@909:
Chris@909: def load_from_yaml(filename, env)
Chris@909: yaml = nil
Chris@909: begin
Chris@909: yaml = YAML::load_file(filename)
Chris@909: rescue ArgumentError
Chris@909: $stderr.puts "Your Redmine configuration file located at #{filename} is not a valid YAML file and could not be loaded."
Chris@909: exit 1
Chris@909: end
Chris@909: conf = {}
Chris@909: if yaml.is_a?(Hash)
Chris@909: if yaml['default']
Chris@909: conf.merge!(yaml['default'])
Chris@909: end
Chris@909: if yaml[env]
Chris@909: conf.merge!(yaml[env])
Chris@909: end
Chris@909: else
Chris@909: $stderr.puts "Your Redmine configuration file located at #{filename} is not a valid Redmine configuration file."
Chris@909: exit 1
Chris@909: end
Chris@909: conf
Chris@909: end
Chris@909:
Chris@909: def load_deprecated_email_configuration(env)
Chris@909: deprecated_email_conf = File.join(Rails.root, 'config', 'email.yml')
Chris@909: if File.file?(deprecated_email_conf)
Chris@909: 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@909: @config.merge!({'email_delivery' => load_from_yaml(deprecated_email_conf, env)})
Chris@909: end
Chris@909: end
Chris@909: end
Chris@909: end
Chris@909: end