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