aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/configuration.rb
blob: 322590f10863892774f1b57a033899bb8b3cf9ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
require 'rails/plugin/loader'
require 'rails/plugin/locator'

module Rails
  class Configuration
    attr_accessor :cache_classes, :load_paths,
                  :load_once_paths, :gems_dependencies_loaded, :after_initialize_blocks,
                  :frameworks, :framework_root_path, :root, :plugin_paths, :plugins,
                  :plugin_loader, :plugin_locators, :gems, :loaded_plugins, :reload_plugins,
                  :i18n, :gems, :whiny_nils, :consider_all_requests_local,
                  :action_controller, :active_record, :action_view, :active_support,
                  :action_mailer, :active_resource,
                  :log_path, :log_level, :logger, :preload_frameworks,
                  :database_configuration_file, :cache_store, :time_zone,
                  :view_path, :metals, :controller_paths, :routes_configuration_file,
                  :eager_load_paths, :dependency_loading, :paths, :serve_static_assets

    def initialize
      @load_once_paths              = []
      @after_initialize_blocks      = []
      @loaded_plugins               = []
      @dependency_loading           = true
      @serve_static_assets          = true

      for framework in frameworks
        self.send("#{framework}=", Rails::OrderedOptions.new)
      end
      self.active_support = Rails::OrderedOptions.new
    end

    def after_initialize(&blk)
      @after_initialize_blocks << blk if blk
    end

    def root
      @root ||= begin
        if defined?(RAILS_ROOT)
          root = RAILS_ROOT
        else
          call_stack = caller.map { |p| p.split(':').first }
          root_path  = call_stack.detect { |p| p !~ %r[railties/lib/rails] }
          root_path  = File.dirname(root_path)

          while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/config.ru")
            parent = File.dirname(root_path)
            root_path = parent != root_path && parent
          end

          Object.class_eval("RAILS_ROOT = ''")

          root = File.exist?("#{root_path}/config.ru") ? root_path : Dir.pwd
        end

        root = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
          Pathname.new(root).expand_path.to_s :
          Pathname.new(root).realpath.to_s

        # TODO: Remove RAILS_ROOT
        RAILS_ROOT.replace(root)
        root
      end
    end

    def root=(root)
      Object.class_eval("RAILS_ROOT = ''") unless defined?(RAILS_ROOT)
      RAILS_ROOT.replace(root)
      @root = root
    end

    def paths
      @paths ||= begin
        paths = Rails::Application::Root.new(root)
        paths.app                 "app",             :load_path => true
        paths.app.metals          "app/metal",       :eager_load => true
        paths.app.models          "app/models",      :eager_load => true
        paths.app.controllers     "app/controllers", builtin_directories, :eager_load => true
        paths.app.helpers         "app/helpers",     :eager_load => true
        paths.app.services        "app/services",    :load_path => true
        paths.lib                 "lib",             :load_path => true
        paths.vendor              "vendor",          :load_path => true
        paths.vendor.plugins      "vendor/plugins"
        paths.tmp                 "tmp"
        paths.tmp.cache           "tmp/cache"
        paths.config              "config"
        paths.config.locales      "config/locales"
        paths.config.environments "config/environments", :glob => "#{RAILS_ENV}.rb"
        paths
      end
    end

    # Enable threaded mode. Allows concurrent requests to controller actions and
    # multiple database connections. Also disables automatic dependency loading
    # after boot, and disables reloading code on every request, as these are
    # fundamentally incompatible with thread safety.
    def threadsafe!
      self.preload_frameworks = true
      self.cache_classes = true
      self.dependency_loading = false
      self.action_controller.allow_concurrency = true
      self
    end

    def framework_paths
      paths = %w(railties railties/lib activesupport/lib)
      paths << 'actionpack/lib' if frameworks.include?(:action_controller) || frameworks.include?(:action_view)

      [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
        paths << "#{framework.to_s.gsub('_', '')}/lib" if frameworks.include?(framework)
      end

      paths.map { |dir| "#{framework_root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
    end

    def framework_root_path
      defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root}/vendor/rails"
    end

    def middleware
      require 'action_dispatch'
      @middleware ||= ActionDispatch::MiddlewareStack.new
    end

    # Loads and returns the contents of the #database_configuration_file. The
    # contents of the file are processed via ERB before being sent through
    # YAML::load.
    def database_configuration
      require 'erb'
      YAML::load(ERB.new(IO.read(database_configuration_file)).result)
    end

    def routes_configuration_file
      @routes_configuration_file ||= File.join(root, 'config', 'routes.rb')
    end

    def controller_paths
      @controller_paths ||= begin
        paths = [File.join(root, 'app', 'controllers')]
        paths.concat builtin_directories
        paths
      end
    end

    def cache_store
      @cache_store ||= begin
        if File.exist?("#{root}/tmp/cache/")
          [ :file_store, "#{root}/tmp/cache/" ]
        else
          :memory_store
        end
      end
    end

    def database_configuration_file
      @database_configuration_file ||= File.join(root, 'config', 'database.yml')
    end

    def view_path
      @view_path ||= File.join(root, 'app', 'views')
    end

    def eager_load_paths
      @eager_load_paths ||= %w(
        app/metal
        app/models
        app/controllers
        app/helpers
      ).map { |dir| "#{root}/#{dir}" }.select { |dir| File.directory?(dir) }
    end

    def load_paths
      @load_paths ||= begin
        paths = []

        # Add the old mock paths only if the directories exists
        paths.concat(Dir["#{root}/test/mocks/#{RAILS_ENV}"]) if File.exists?("#{root}/test/mocks/#{RAILS_ENV}")

        # Add the app's controller directory
        paths.concat(Dir["#{root}/app/controllers/"])

        # Followed by the standard includes.
        paths.concat %w(
          app
          app/metal
          app/models
          app/controllers
          app/helpers
          app/services
          lib
          vendor
        ).map { |dir| "#{root}/#{dir}" }.select { |dir| File.directory?(dir) }

        paths.concat builtin_directories
      end
    end

    def builtin_directories
      # Include builtins only in the development environment.
      (RAILS_ENV == 'development') ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
    end

    def log_path
      @log_path ||= File.join(root, 'log', "#{RAILS_ENV}.log")
    end

    def log_level
      @log_level ||= RAILS_ENV == 'production' ? :info : :debug
    end

    def frameworks
      @frameworks ||= [ :active_record, :action_controller, :action_view, :action_mailer, :active_resource ]
    end

    def plugin_paths
      @plugin_paths ||= ["#{root}/vendor/plugins"]
    end

    def plugin_loader
      @plugin_loader ||= begin
        Plugin::Loader
      end
    end

    def plugin_locators
      @plugin_locators ||= begin
        locators = []
        locators << Plugin::GemLocator if defined? Gem
        locators << Plugin::FileSystemLocator
      end
    end

    def i18n
      @i18n ||= begin
        i18n = Rails::OrderedOptions.new
        i18n.load_path = []

        if File.exist?(File.join(root, 'config', 'locales'))
          i18n.load_path << Dir[File.join(root, 'config', 'locales', '*.{rb,yml}')]
          i18n.load_path.flatten!
        end

        i18n
      end
    end

    # Adds a single Gem dependency to the rails application. By default, it will require
    # the library with the same name as the gem. Use :lib to specify a different name.
    #
    #   # gem 'aws-s3', '>= 0.4.0'
    #   # require 'aws/s3'
    #   config.gem 'aws-s3', :lib => 'aws/s3', :version => '>= 0.4.0', \
    #     :source => "http://code.whytheluckystiff.net"
    #
    # To require a library be installed, but not attempt to load it, pass :lib => false
    #
    #   config.gem 'qrp', :version => '0.4.1', :lib => false
    def gem(name, options = {})
      gems << Rails::GemDependency.new(name, options)
    end

    def gems
      @gems ||= []
    end

    def environment_path
      "#{root}/config/environments/#{RAILS_ENV}.rb"
    end

    def reload_plugins?
      @reload_plugins
    end

    # Holds generators configuration:
    #
    #   config.generators do |g|
    #     g.orm             :datamapper, :migration => true
    #     g.template_engine :haml
    #     g.test_framework  :rspec
    #   end
    #
    # If you want to disable color in console, do:
    #
    #   config.generators.colorize_logging = false
    #
    def generators
      @generators ||= Generators.new
      if block_given?
        yield @generators
      else
        @generators
      end
    end

    class Generators #:nodoc:
      attr_accessor :aliases, :options, :colorize_logging

      def initialize
        @aliases = Hash.new { |h,k| h[k] = {} }
        @options = Hash.new { |h,k| h[k] = {} }
        @colorize_logging = true
      end

      def method_missing(method, *args)
        method        = method.to_s.sub(/=$/, '').to_sym
        namespace     = args.first.is_a?(Symbol) ? args.shift : nil
        configuration = args.first.is_a?(Hash)   ? args.shift : nil

        @options[:rails][method] = namespace if namespace
        namespace ||= method

        if configuration
          aliases = configuration.delete(:aliases)
          @aliases[namespace].merge!(aliases) if aliases
          @options[namespace].merge!(configuration)
        end
      end
    end
  end
end