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