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