To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / redmine / configuration.rb @ 1568:bc47b68a9487

History | View | Annotate | Download (3.77 KB)

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