~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/railties/lib/initializer.rb

  • Committer: Michael Forrest
  • Date: 2010-10-15 16:28:50 UTC
  • Revision ID: michael.forrest@canonical.com-20101015162850-tj2vchanv0kr0dun
refrozeĀ gems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'logger'
 
2
require 'set'
 
3
require 'pathname'
 
4
 
 
5
$LOAD_PATH.unshift File.dirname(__FILE__)
 
6
require 'railties_path'
 
7
require 'rails/version'
 
8
require 'rails/plugin/locator'
 
9
require 'rails/plugin/loader'
 
10
require 'rails/gem_dependency'
 
11
require 'rails/rack'
 
12
 
 
13
 
 
14
RAILS_ENV = (ENV['RAILS_ENV'] || 'development').dup unless defined?(RAILS_ENV)
 
15
 
 
16
module Rails
 
17
  class << self
 
18
    # The Configuration instance used to configure the Rails environment
 
19
    def configuration
 
20
      @@configuration
 
21
    end
 
22
 
 
23
    def configuration=(configuration)
 
24
      @@configuration = configuration
 
25
    end
 
26
 
 
27
    def initialized?
 
28
      @initialized || false
 
29
    end
 
30
 
 
31
    def initialized=(initialized)
 
32
      @initialized ||= initialized
 
33
    end
 
34
 
 
35
    def logger
 
36
      if defined?(RAILS_DEFAULT_LOGGER)
 
37
        RAILS_DEFAULT_LOGGER
 
38
      else
 
39
        nil
 
40
      end
 
41
    end
 
42
 
 
43
    def backtrace_cleaner
 
44
      @@backtrace_cleaner ||= begin
 
45
        # Relies on ActiveSupport, so we have to lazy load to postpone definition until AS has been loaded
 
46
        require 'rails/backtrace_cleaner'
 
47
        Rails::BacktraceCleaner.new
 
48
      end
 
49
    end
 
50
 
 
51
    def root
 
52
      Pathname.new(RAILS_ROOT) if defined?(RAILS_ROOT)
 
53
    end
 
54
 
 
55
    def env
 
56
      @_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV)
 
57
    end
 
58
 
 
59
    def cache
 
60
      RAILS_CACHE
 
61
    end
 
62
 
 
63
    def version
 
64
      VERSION::STRING
 
65
    end
 
66
 
 
67
    def public_path
 
68
      @@public_path ||= self.root ? File.join(self.root, "public") : "public"
 
69
    end
 
70
 
 
71
    def public_path=(path)
 
72
      @@public_path = path
 
73
    end
 
74
  end
 
75
 
 
76
  # The Initializer is responsible for processing the Rails configuration, such
 
77
  # as setting the $LOAD_PATH, requiring the right frameworks, initializing
 
78
  # logging, and more. It can be run either as a single command that'll just
 
79
  # use the default configuration, like this:
 
80
  #
 
81
  #   Rails::Initializer.run
 
82
  #
 
83
  # But normally it's more interesting to pass in a custom configuration
 
84
  # through the block running:
 
85
  #
 
86
  #   Rails::Initializer.run do |config|
 
87
  #     config.frameworks -= [ :action_mailer ]
 
88
  #   end
 
89
  #
 
90
  # This will use the default configuration options from Rails::Configuration,
 
91
  # but allow for overwriting on select areas.
 
92
  class Initializer
 
93
    # The Configuration instance used by this Initializer instance.
 
94
    attr_reader :configuration
 
95
 
 
96
    # The set of loaded plugins.
 
97
    attr_reader :loaded_plugins
 
98
 
 
99
    # Whether or not all the gem dependencies have been met
 
100
    attr_reader :gems_dependencies_loaded
 
101
 
 
102
    # Runs the initializer. By default, this will invoke the #process method,
 
103
    # which simply executes all of the initialization routines. Alternately,
 
104
    # you can specify explicitly which initialization routine you want:
 
105
    #
 
106
    #   Rails::Initializer.run(:set_load_path)
 
107
    #
 
108
    # This is useful if you only want the load path initialized, without
 
109
    # incurring the overhead of completely loading the entire environment.
 
110
    def self.run(command = :process, configuration = Configuration.new)
 
111
      yield configuration if block_given?
 
112
      initializer = new configuration
 
113
      initializer.send(command)
 
114
      initializer
 
115
    end
 
116
 
 
117
    # Create a new Initializer instance that references the given Configuration
 
118
    # instance.
 
119
    def initialize(configuration)
 
120
      @configuration = configuration
 
121
      @loaded_plugins = []
 
122
    end
 
123
 
 
124
    # Sequentially step through all of the available initialization routines,
 
125
    # in order (view execution order in source).
 
126
    def process
 
127
      Rails.configuration = configuration
 
128
 
 
129
      check_ruby_version
 
130
      install_gem_spec_stubs
 
131
      set_load_path
 
132
      add_gem_load_paths
 
133
 
 
134
      require_frameworks
 
135
      set_autoload_paths
 
136
      add_plugin_load_paths
 
137
      load_environment
 
138
      preload_frameworks
 
139
 
 
140
      initialize_encoding
 
141
      initialize_database
 
142
 
 
143
      initialize_cache
 
144
      initialize_framework_caches
 
145
 
 
146
      initialize_logger
 
147
      initialize_framework_logging
 
148
 
 
149
      initialize_dependency_mechanism
 
150
      initialize_whiny_nils
 
151
 
 
152
      initialize_time_zone
 
153
      initialize_i18n
 
154
 
 
155
      initialize_framework_settings
 
156
      initialize_framework_views
 
157
 
 
158
      initialize_metal
 
159
 
 
160
      add_support_load_paths
 
161
 
 
162
      check_for_unbuilt_gems
 
163
 
 
164
      load_gems
 
165
      load_plugins
 
166
 
 
167
      # pick up any gems that plugins depend on
 
168
      add_gem_load_paths
 
169
      load_gems
 
170
      check_gem_dependencies
 
171
 
 
172
      # bail out if gems are missing - note that check_gem_dependencies will have
 
173
      # already called abort() unless $gems_rake_task is set
 
174
      return unless gems_dependencies_loaded
 
175
 
 
176
      load_application_initializers
 
177
 
 
178
      # the framework is now fully initialized
 
179
      after_initialize
 
180
 
 
181
      # Setup database middleware after initializers have run
 
182
      initialize_database_middleware
 
183
 
 
184
      # Prepare dispatcher callbacks and run 'prepare' callbacks
 
185
      prepare_dispatcher
 
186
 
 
187
      # Routing must be initialized after plugins to allow the former to extend the routes
 
188
      initialize_routing
 
189
 
 
190
      # Observers are loaded after plugins in case Observers or observed models are modified by plugins.
 
191
      load_observers
 
192
 
 
193
      # Load view path cache
 
194
      load_view_paths
 
195
 
 
196
      # Load application classes
 
197
      load_application_classes
 
198
 
 
199
      # Disable dependency loading during request cycle
 
200
      disable_dependency_loading
 
201
 
 
202
      # Flag initialized
 
203
      Rails.initialized = true
 
204
    end
 
205
 
 
206
    # Check for valid Ruby version
 
207
    # This is done in an external file, so we can use it
 
208
    # from the `rails` program as well without duplication.
 
209
    def check_ruby_version
 
210
      require 'ruby_version_check'
 
211
    end
 
212
 
 
213
    # If Rails is vendored and RubyGems is available, install stub GemSpecs
 
214
    # for Rails, Active Support, Active Record, Action Pack, Action Mailer, and
 
215
    # Active Resource. This allows Gem plugins to depend on Rails even when
 
216
    # the Gem version of Rails shouldn't be loaded.
 
217
    def install_gem_spec_stubs
 
218
      unless Rails.respond_to?(:vendor_rails?)
 
219
        abort %{Your config/boot.rb is outdated: Run "rake rails:update".}
 
220
      end
 
221
 
 
222
      if Rails.vendor_rails?
 
223
        begin; require "rubygems"; rescue LoadError; return; end
 
224
 
 
225
        stubs = %w(rails activesupport activerecord actionpack actionmailer activeresource)
 
226
        stubs.reject! { |s| Gem.loaded_specs.key?(s) }
 
227
 
 
228
        stubs.each do |stub|
 
229
          Gem.loaded_specs[stub] = Gem::Specification.new do |s|
 
230
            s.name = stub
 
231
            s.version = Rails::VERSION::STRING
 
232
            s.loaded_from = ""
 
233
          end
 
234
        end
 
235
      end
 
236
    end
 
237
 
 
238
    # Set the <tt>$LOAD_PATH</tt> based on the value of
 
239
    # Configuration#load_paths. Duplicates are removed.
 
240
    def set_load_path
 
241
      load_paths = configuration.load_paths + configuration.framework_paths
 
242
      load_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) }
 
243
      $LOAD_PATH.uniq!
 
244
    end
 
245
 
 
246
    # Set the paths from which Rails will automatically load source files, and
 
247
    # the load_once paths.
 
248
    def set_autoload_paths
 
249
      ActiveSupport::Dependencies.load_paths = configuration.load_paths.uniq
 
250
      ActiveSupport::Dependencies.load_once_paths = configuration.load_once_paths.uniq
 
251
 
 
252
      extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths
 
253
      unless extra.empty?
 
254
        abort <<-end_error
 
255
          load_once_paths must be a subset of the load_paths.
 
256
          Extra items in load_once_paths: #{extra * ','}
 
257
        end_error
 
258
      end
 
259
 
 
260
      # Freeze the arrays so future modifications will fail rather than do nothing mysteriously
 
261
      configuration.load_once_paths.freeze
 
262
    end
 
263
 
 
264
    # Requires all frameworks specified by the Configuration#frameworks
 
265
    # list. By default, all frameworks (Active Record, Active Support,
 
266
    # Action Pack, Action Mailer, and Active Resource) are loaded.
 
267
    def require_frameworks
 
268
      configuration.frameworks.each { |framework| require(framework.to_s) }
 
269
    rescue LoadError => e
 
270
      # Re-raise as RuntimeError because Mongrel would swallow LoadError.
 
271
      raise e.to_s
 
272
    end
 
273
 
 
274
    # Preload all frameworks specified by the Configuration#frameworks.
 
275
    # Used by Passenger to ensure everything's loaded before forking and
 
276
    # to avoid autoload race conditions in JRuby.
 
277
    def preload_frameworks
 
278
      if configuration.preload_frameworks
 
279
        configuration.frameworks.each do |framework|
 
280
          # String#classify and #constantize aren't available yet.
 
281
          toplevel = Object.const_get(framework.to_s.gsub(/(?:^|_)(.)/) { $1.upcase })
 
282
          toplevel.load_all! if toplevel.respond_to?(:load_all!)
 
283
        end
 
284
      end
 
285
    end
 
286
 
 
287
    # Add the load paths used by support functions such as the info controller
 
288
    def add_support_load_paths
 
289
    end
 
290
 
 
291
    # Adds all load paths from plugins to the global set of load paths, so that
 
292
    # code from plugins can be required (explicitly or automatically via ActiveSupport::Dependencies).
 
293
    def add_plugin_load_paths
 
294
      plugin_loader.add_plugin_load_paths
 
295
    end
 
296
 
 
297
    def add_gem_load_paths
 
298
      Rails::GemDependency.add_frozen_gem_path
 
299
      unless @configuration.gems.empty?
 
300
        require "rubygems"
 
301
        @configuration.gems.each { |gem| gem.add_load_paths }
 
302
      end
 
303
    end
 
304
 
 
305
    def load_gems
 
306
      unless $gems_rake_task
 
307
        @configuration.gems.each { |gem| gem.load }
 
308
      end
 
309
    end
 
310
 
 
311
    def check_for_unbuilt_gems
 
312
      unbuilt_gems = @configuration.gems.select(&:frozen?).reject(&:built?)
 
313
      if unbuilt_gems.size > 0
 
314
        # don't print if the gems:build rake tasks are being run
 
315
        unless $gems_build_rake_task
 
316
          abort <<-end_error
 
317
The following gems have native components that need to be built
 
318
  #{unbuilt_gems.map { |gem| "#{gem.name}  #{gem.requirement}" } * "\n  "}
 
319
 
 
320
You're running:
 
321
  ruby #{Gem.ruby_version} at #{Gem.ruby}
 
322
  rubygems #{Gem::RubyGemsVersion} at #{Gem.path * ', '}
 
323
 
 
324
Run `rake gems:build` to build the unbuilt gems.
 
325
          end_error
 
326
        end
 
327
      end
 
328
    end
 
329
 
 
330
    def check_gem_dependencies
 
331
      unloaded_gems = @configuration.gems.reject { |g| g.loaded? }
 
332
      if unloaded_gems.size > 0
 
333
        @gems_dependencies_loaded = false
 
334
        # don't print if the gems rake tasks are being run
 
335
        unless $gems_rake_task
 
336
          abort <<-end_error
 
337
Missing these required gems:
 
338
  #{unloaded_gems.map { |gem| "#{gem.name}  #{gem.requirement}" } * "\n  "}
 
339
 
 
340
You're running:
 
341
  ruby #{Gem.ruby_version} at #{Gem.ruby}
 
342
  rubygems #{Gem::RubyGemsVersion} at #{Gem.path * ', '}
 
343
 
 
344
Run `rake gems:install` to install the missing gems.
 
345
          end_error
 
346
        end
 
347
      else
 
348
        @gems_dependencies_loaded = true
 
349
      end
 
350
    end
 
351
 
 
352
    # Loads all plugins in <tt>config.plugin_paths</tt>.  <tt>plugin_paths</tt>
 
353
    # defaults to <tt>vendor/plugins</tt> but may also be set to a list of
 
354
    # paths, such as
 
355
    #   config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]
 
356
    #
 
357
    # In the default implementation, as each plugin discovered in <tt>plugin_paths</tt> is initialized:
 
358
    # * its +lib+ directory, if present, is added to the load path (immediately after the applications lib directory)
 
359
    # * <tt>init.rb</tt> is evaluated, if present
 
360
    #
 
361
    # After all plugins are loaded, duplicates are removed from the load path.
 
362
    # If an array of plugin names is specified in config.plugins, only those plugins will be loaded
 
363
    # and they plugins will be loaded in that order. Otherwise, plugins are loaded in alphabetical
 
364
    # order.
 
365
    #
 
366
    # if config.plugins ends contains :all then the named plugins will be loaded in the given order and all other
 
367
    # plugins will be loaded in alphabetical order
 
368
    def load_plugins
 
369
      plugin_loader.load_plugins
 
370
    end
 
371
 
 
372
    def plugin_loader
 
373
      @plugin_loader ||= configuration.plugin_loader.new(self)
 
374
    end
 
375
 
 
376
    # Loads the environment specified by Configuration#environment_path, which
 
377
    # is typically one of development, test, or production.
 
378
    def load_environment
 
379
      silence_warnings do
 
380
        return if @environment_loaded
 
381
        @environment_loaded = true
 
382
 
 
383
        config = configuration
 
384
        constants = self.class.constants
 
385
 
 
386
        eval(IO.read(configuration.environment_path), binding, configuration.environment_path)
 
387
 
 
388
        (self.class.constants - constants).each do |const|
 
389
          Object.const_set(const, self.class.const_get(const))
 
390
        end
 
391
      end
 
392
    end
 
393
 
 
394
    def load_observers
 
395
      if gems_dependencies_loaded && configuration.frameworks.include?(:active_record)
 
396
        ActiveRecord::Base.instantiate_observers
 
397
      end
 
398
    end
 
399
 
 
400
    def load_view_paths
 
401
      if configuration.frameworks.include?(:action_view)
 
402
        ActionController::Base.view_paths.load! if configuration.frameworks.include?(:action_controller)
 
403
        ActionMailer::Base.view_paths.load! if configuration.frameworks.include?(:action_mailer)
 
404
      end
 
405
    end
 
406
 
 
407
    # Eager load application classes
 
408
    def load_application_classes
 
409
      return if $rails_rake_task
 
410
      if configuration.cache_classes
 
411
        configuration.eager_load_paths.each do |load_path|
 
412
          matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
 
413
          Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
 
414
            require_dependency file.sub(matcher, '\1')
 
415
          end
 
416
        end
 
417
      end
 
418
    end
 
419
 
 
420
    # For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
 
421
    # multibyte safe operations. Plugin authors supporting other encodings
 
422
    # should override this behaviour and set the relevant +default_charset+
 
423
    # on ActionController::Base.
 
424
    #
 
425
    # For Ruby 1.9, this does nothing. Specify the default encoding in the Ruby
 
426
    # shebang line if you don't want UTF-8.
 
427
    def initialize_encoding
 
428
      $KCODE='u' if RUBY_VERSION < '1.9'
 
429
    end
 
430
 
 
431
    # This initialization routine does nothing unless <tt>:active_record</tt>
 
432
    # is one of the frameworks to load (Configuration#frameworks). If it is,
 
433
    # this sets the database configuration from Configuration#database_configuration
 
434
    # and then establishes the connection.
 
435
    def initialize_database
 
436
      if configuration.frameworks.include?(:active_record)
 
437
        ActiveRecord::Base.configurations = configuration.database_configuration
 
438
        ActiveRecord::Base.establish_connection
 
439
      end
 
440
    end
 
441
 
 
442
    def initialize_database_middleware
 
443
      if configuration.frameworks.include?(:active_record)
 
444
        if configuration.frameworks.include?(:action_controller) &&
 
445
            ActionController::Base.session_store.name == 'ActiveRecord::SessionStore'
 
446
          configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::ConnectionAdapters::ConnectionManagement
 
447
          configuration.middleware.insert_before :"ActiveRecord::SessionStore", ActiveRecord::QueryCache
 
448
        else
 
449
          configuration.middleware.use ActiveRecord::ConnectionAdapters::ConnectionManagement
 
450
          configuration.middleware.use ActiveRecord::QueryCache
 
451
        end
 
452
      end
 
453
    end
 
454
 
 
455
    def initialize_cache
 
456
      unless defined?(RAILS_CACHE)
 
457
        silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(configuration.cache_store) }
 
458
 
 
459
        if RAILS_CACHE.respond_to?(:middleware)
 
460
          # Insert middleware to setup and teardown local cache for each request
 
461
          configuration.middleware.insert_after(:"ActionController::Failsafe", RAILS_CACHE.middleware)
 
462
        end
 
463
      end
 
464
    end
 
465
 
 
466
    def initialize_framework_caches
 
467
      if configuration.frameworks.include?(:action_controller)
 
468
        ActionController::Base.cache_store ||= RAILS_CACHE
 
469
      end
 
470
    end
 
471
 
 
472
    # If the RAILS_DEFAULT_LOGGER constant is already set, this initialization
 
473
    # routine does nothing. If the constant is not set, and Configuration#logger
 
474
    # is not +nil+, this also does nothing. Otherwise, a new logger instance
 
475
    # is created at Configuration#log_path, with a default log level of
 
476
    # Configuration#log_level.
 
477
    #
 
478
    # If the log could not be created, the log will be set to output to
 
479
    # +STDERR+, with a log level of +WARN+.
 
480
    def initialize_logger
 
481
      # if the environment has explicitly defined a logger, use it
 
482
      return if Rails.logger
 
483
 
 
484
      unless logger = configuration.logger
 
485
        begin
 
486
          logger = ActiveSupport::BufferedLogger.new(configuration.log_path)
 
487
          logger.level = ActiveSupport::BufferedLogger.const_get(configuration.log_level.to_s.upcase)
 
488
          if configuration.environment == "production"
 
489
            logger.auto_flushing = false
 
490
          end
 
491
        rescue StandardError => e
 
492
          logger = ActiveSupport::BufferedLogger.new(STDERR)
 
493
          logger.level = ActiveSupport::BufferedLogger::WARN
 
494
          logger.warn(
 
495
            "Rails Error: Unable to access log file. Please ensure that #{configuration.log_path} exists and is chmod 0666. " +
 
496
            "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
 
497
          )
 
498
        end
 
499
      end
 
500
 
 
501
      silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
 
502
    end
 
503
 
 
504
    # Sets the logger for Active Record, Action Controller, and Action Mailer
 
505
    # (but only for those frameworks that are to be loaded). If the framework's
 
506
    # logger is already set, it is not changed, otherwise it is set to use
 
507
    # RAILS_DEFAULT_LOGGER.
 
508
    def initialize_framework_logging
 
509
      for framework in ([ :active_record, :action_controller, :action_mailer ] & configuration.frameworks)
 
510
        framework.to_s.camelize.constantize.const_get("Base").logger ||= Rails.logger
 
511
      end
 
512
 
 
513
      ActiveSupport::Dependencies.logger ||= Rails.logger
 
514
      Rails.cache.logger ||= Rails.logger
 
515
    end
 
516
 
 
517
    # Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+
 
518
    # (but only for those frameworks that are to be loaded). If the framework's
 
519
    # paths have already been set, it is not changed, otherwise it is
 
520
    # set to use Configuration#view_path.
 
521
    def initialize_framework_views
 
522
      if configuration.frameworks.include?(:action_view)
 
523
        view_path = ActionView::PathSet.type_cast(configuration.view_path)
 
524
        ActionMailer::Base.template_root  = view_path if configuration.frameworks.include?(:action_mailer) && ActionMailer::Base.view_paths.blank?
 
525
        ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller) && ActionController::Base.view_paths.blank?
 
526
      end
 
527
    end
 
528
 
 
529
    # If Action Controller is not one of the loaded frameworks (Configuration#frameworks)
 
530
    # this does nothing. Otherwise, it loads the routing definitions and sets up
 
531
    # loading module used to lazily load controllers (Configuration#controller_paths).
 
532
    def initialize_routing
 
533
      return unless configuration.frameworks.include?(:action_controller)
 
534
 
 
535
      ActionController::Routing.controller_paths += configuration.controller_paths
 
536
      ActionController::Routing::Routes.add_configuration_file(configuration.routes_configuration_file)
 
537
      ActionController::Routing::Routes.reload!
 
538
    end
 
539
 
 
540
    # Sets the dependency loading mechanism based on the value of
 
541
    # Configuration#cache_classes.
 
542
    def initialize_dependency_mechanism
 
543
      ActiveSupport::Dependencies.mechanism = configuration.cache_classes ? :require : :load
 
544
    end
 
545
 
 
546
    # Loads support for "whiny nil" (noisy warnings when methods are invoked
 
547
    # on +nil+ values) if Configuration#whiny_nils is true.
 
548
    def initialize_whiny_nils
 
549
      require('active_support/whiny_nil') if configuration.whiny_nils
 
550
    end
 
551
 
 
552
    # Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes.
 
553
    # If assigned value cannot be matched to a TimeZone, an exception will be raised.
 
554
    def initialize_time_zone
 
555
      if configuration.time_zone
 
556
        zone_default = Time.__send__(:get_zone, configuration.time_zone)
 
557
 
 
558
        unless zone_default
 
559
          raise \
 
560
            'Value assigned to config.time_zone not recognized.' +
 
561
            'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
 
562
        end
 
563
 
 
564
        Time.zone_default = zone_default
 
565
 
 
566
        if configuration.frameworks.include?(:active_record)
 
567
          ActiveRecord::Base.time_zone_aware_attributes = true
 
568
          ActiveRecord::Base.default_timezone = :utc
 
569
        end
 
570
      end
 
571
    end
 
572
 
 
573
    # Set the i18n configuration from config.i18n but special-case for the load_path which should be
 
574
    # appended to what's already set instead of overwritten.
 
575
    def initialize_i18n
 
576
      configuration.i18n.each do |setting, value|
 
577
        if setting == :load_path
 
578
          I18n.load_path += value
 
579
        else
 
580
          I18n.send("#{setting}=", value)
 
581
        end
 
582
      end
 
583
    end
 
584
 
 
585
    def initialize_metal
 
586
      Rails::Rack::Metal.requested_metals = configuration.metals
 
587
      Rails::Rack::Metal.metal_paths += plugin_loader.engine_metal_paths
 
588
 
 
589
      configuration.middleware.insert_before(
 
590
        :"ActionController::ParamsParser",
 
591
        Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
 
592
    end
 
593
 
 
594
    # Initializes framework-specific settings for each of the loaded frameworks
 
595
    # (Configuration#frameworks). The available settings map to the accessors
 
596
    # on each of the corresponding Base classes.
 
597
    def initialize_framework_settings
 
598
      configuration.frameworks.each do |framework|
 
599
        base_class = framework.to_s.camelize.constantize.const_get("Base")
 
600
 
 
601
        configuration.send(framework).each do |setting, value|
 
602
          base_class.send("#{setting}=", value)
 
603
        end
 
604
      end
 
605
      configuration.active_support.each do |setting, value|
 
606
        ActiveSupport.send("#{setting}=", value)
 
607
      end
 
608
    end
 
609
 
 
610
    # Fires the user-supplied after_initialize block (Configuration#after_initialize)
 
611
    def after_initialize
 
612
      if gems_dependencies_loaded
 
613
        configuration.after_initialize_blocks.each do |block|
 
614
          block.call
 
615
        end
 
616
      end
 
617
    end
 
618
 
 
619
    def load_application_initializers
 
620
      if gems_dependencies_loaded
 
621
        Dir["#{configuration.root_path}/config/initializers/**/*.rb"].sort.each do |initializer|
 
622
          load(initializer)
 
623
        end
 
624
      end
 
625
    end
 
626
 
 
627
    def prepare_dispatcher
 
628
      return unless configuration.frameworks.include?(:action_controller)
 
629
      require 'dispatcher' unless defined?(::Dispatcher)
 
630
      Dispatcher.define_dispatcher_callbacks(configuration.cache_classes)
 
631
      Dispatcher.run_prepare_callbacks
 
632
    end
 
633
 
 
634
    def disable_dependency_loading
 
635
      if configuration.cache_classes && !configuration.dependency_loading
 
636
        ActiveSupport::Dependencies.unhook!
 
637
      end
 
638
    end
 
639
  end
 
640
 
 
641
  # The Configuration class holds all the parameters for the Initializer and
 
642
  # ships with defaults that suites most Rails applications. But it's possible
 
643
  # to overwrite everything. Usually, you'll create an Configuration file
 
644
  # implicitly through the block running on the Initializer, but it's also
 
645
  # possible to create the Configuration instance in advance and pass it in
 
646
  # like this:
 
647
  #
 
648
  #   config = Rails::Configuration.new
 
649
  #   Rails::Initializer.run(:process, config)
 
650
  class Configuration
 
651
    # The application's base directory.
 
652
    attr_reader :root_path
 
653
 
 
654
    # A stub for setting options on ActionController::Base.
 
655
    attr_accessor :action_controller
 
656
 
 
657
    # A stub for setting options on ActionMailer::Base.
 
658
    attr_accessor :action_mailer
 
659
 
 
660
    # A stub for setting options on ActionView::Base.
 
661
    attr_accessor :action_view
 
662
 
 
663
    # A stub for setting options on ActiveRecord::Base.
 
664
    attr_accessor :active_record
 
665
 
 
666
    # A stub for setting options on ActiveResource::Base.
 
667
    attr_accessor :active_resource
 
668
 
 
669
    # A stub for setting options on ActiveSupport.
 
670
    attr_accessor :active_support
 
671
 
 
672
    # Whether to preload all frameworks at startup.
 
673
    attr_accessor :preload_frameworks
 
674
 
 
675
    # Whether or not classes should be cached (set to false if you want
 
676
    # application classes to be reloaded on each request)
 
677
    attr_accessor :cache_classes
 
678
 
 
679
    # The list of paths that should be searched for controllers. (Defaults
 
680
    # to <tt>app/controllers</tt>.)
 
681
    attr_accessor :controller_paths
 
682
 
 
683
    # The path to the database configuration file to use. (Defaults to
 
684
    # <tt>config/database.yml</tt>.)
 
685
    attr_accessor :database_configuration_file
 
686
 
 
687
    # The path to the routes configuration file to use. (Defaults to
 
688
    # <tt>config/routes.rb</tt>.)
 
689
    attr_accessor :routes_configuration_file
 
690
 
 
691
    # The list of rails framework components that should be loaded. (Defaults
 
692
    # to <tt>:active_record</tt>, <tt>:action_controller</tt>,
 
693
    # <tt>:action_view</tt>, <tt>:action_mailer</tt>, and
 
694
    # <tt>:active_resource</tt>).
 
695
    attr_accessor :frameworks
 
696
 
 
697
    # An array of additional paths to prepend to the load path. By default,
 
698
    # all +app+, +lib+, +vendor+ and mock paths are included in this list.
 
699
    attr_accessor :load_paths
 
700
 
 
701
    # An array of paths from which Rails will automatically load from only once.
 
702
    # All elements of this array must also be in +load_paths+.
 
703
    attr_accessor :load_once_paths
 
704
 
 
705
    # An array of paths from which Rails will eager load on boot if cache
 
706
    # classes is enabled. All elements of this array must also be in
 
707
    # +load_paths+.
 
708
    attr_accessor :eager_load_paths
 
709
 
 
710
    # The log level to use for the default Rails logger. In production mode,
 
711
    # this defaults to <tt>:info</tt>. In development mode, it defaults to
 
712
    # <tt>:debug</tt>.
 
713
    attr_accessor :log_level
 
714
 
 
715
    # The path to the log file to use. Defaults to log/#{environment}.log
 
716
    # (e.g. log/development.log or log/production.log).
 
717
    attr_accessor :log_path
 
718
 
 
719
    # The specific logger to use. By default, a logger will be created and
 
720
    # initialized using #log_path and #log_level, but a programmer may
 
721
    # specifically set the logger to use via this accessor and it will be
 
722
    # used directly.
 
723
    attr_accessor :logger
 
724
 
 
725
    # The specific cache store to use. By default, the ActiveSupport::Cache::Store will be used.
 
726
    attr_accessor :cache_store
 
727
 
 
728
    # The root of the application's views. (Defaults to <tt>app/views</tt>.)
 
729
    attr_accessor :view_path
 
730
 
 
731
    # Set to +true+ if you want to be warned (noisily) when you try to invoke
 
732
    # any method of +nil+. Set to +false+ for the standard Ruby behavior.
 
733
    attr_accessor :whiny_nils
 
734
 
 
735
    # The list of plugins to load. If this is set to <tt>nil</tt>, all plugins will
 
736
    # be loaded. If this is set to <tt>[]</tt>, no plugins will be loaded. Otherwise,
 
737
    # plugins will be loaded in the order specified.
 
738
    attr_reader :plugins
 
739
    def plugins=(plugins)
 
740
      @plugins = plugins.nil? ? nil : plugins.map { |p| p.to_sym }
 
741
    end
 
742
 
 
743
    # The list of metals to load. If this is set to <tt>nil</tt>, all metals will
 
744
    # be loaded in alphabetical order. If this is set to <tt>[]</tt>, no metals will
 
745
    # be loaded. Otherwise metals will be loaded in the order specified
 
746
    attr_accessor :metals
 
747
 
 
748
    # The path to the root of the plugins directory. By default, it is in
 
749
    # <tt>vendor/plugins</tt>.
 
750
    attr_accessor :plugin_paths
 
751
 
 
752
    # The classes that handle finding the desired plugins that you'd like to load for
 
753
    # your application. By default it is the Rails::Plugin::FileSystemLocator which finds
 
754
    # plugins to load in <tt>vendor/plugins</tt>. You can hook into gem location by subclassing
 
755
    # Rails::Plugin::Locator and adding it onto the list of <tt>plugin_locators</tt>.
 
756
    attr_accessor :plugin_locators
 
757
 
 
758
    # The class that handles loading each plugin. Defaults to Rails::Plugin::Loader, but
 
759
    # a sub class would have access to fine grained modification of the loading behavior. See
 
760
    # the implementation of Rails::Plugin::Loader for more details.
 
761
    attr_accessor :plugin_loader
 
762
 
 
763
    # Enables or disables plugin reloading.  You can get around this setting per plugin.
 
764
    # If <tt>reload_plugins?</tt> is false, add this to your plugin's <tt>init.rb</tt>
 
765
    # to make it reloadable:
 
766
    #
 
767
    #   ActiveSupport::Dependencies.load_once_paths.delete lib_path
 
768
    #
 
769
    # If <tt>reload_plugins?</tt> is true, add this to your plugin's <tt>init.rb</tt>
 
770
    # to only load it once:
 
771
    #
 
772
    #   ActiveSupport::Dependencies.load_once_paths << lib_path
 
773
    #
 
774
    attr_accessor :reload_plugins
 
775
 
 
776
    # Returns true if plugin reloading is enabled.
 
777
    def reload_plugins?
 
778
      !!@reload_plugins
 
779
    end
 
780
 
 
781
    # Enables or disables dependency loading during the request cycle. Setting
 
782
    # <tt>dependency_loading</tt> to true will allow new classes to be loaded
 
783
    # during a request. Setting it to false will disable this behavior.
 
784
    #
 
785
    # Those who want to run in a threaded environment should disable this
 
786
    # option and eager load or require all there classes on initialization.
 
787
    #
 
788
    # If <tt>cache_classes</tt> is disabled, dependency loaded will always be
 
789
    # on.
 
790
    attr_accessor :dependency_loading
 
791
 
 
792
    # An array of gems that this rails application depends on.  Rails will automatically load
 
793
    # these gems during installation, and allow you to install any missing gems with:
 
794
    #
 
795
    #   rake gems:install
 
796
    #
 
797
    # You can add gems with the #gem method.
 
798
    attr_accessor :gems
 
799
 
 
800
    # Adds a single Gem dependency to the rails application. By default, it will require
 
801
    # the library with the same name as the gem. Use :lib to specify a different name.
 
802
    #
 
803
    #   # gem 'aws-s3', '>= 0.4.0'
 
804
    #   # require 'aws/s3'
 
805
    #   config.gem 'aws-s3', :lib => 'aws/s3', :version => '>= 0.4.0', \
 
806
    #     :source => "http://code.whytheluckystiff.net"
 
807
    #
 
808
    # To require a library be installed, but not attempt to load it, pass :lib => false
 
809
    #
 
810
    #   config.gem 'qrp', :version => '0.4.1', :lib => false
 
811
    def gem(name, options = {})
 
812
      @gems << Rails::GemDependency.new(name, options)
 
813
    end
 
814
 
 
815
    # Deprecated options:
 
816
    def breakpoint_server(_ = nil)
 
817
      $stderr.puts %(
 
818
      *******************************************************************
 
819
      * config.breakpoint_server has been deprecated and has no effect. *
 
820
      *******************************************************************
 
821
      )
 
822
    end
 
823
    alias_method :breakpoint_server=, :breakpoint_server
 
824
 
 
825
    # Sets the default +time_zone+.  Setting this will enable +time_zone+
 
826
    # awareness for Active Record models and set the Active Record default
 
827
    # timezone to <tt>:utc</tt>.
 
828
    attr_accessor :time_zone
 
829
 
 
830
    # Accessor for i18n settings.
 
831
    attr_accessor :i18n
 
832
 
 
833
    # Create a new Configuration instance, initialized with the default
 
834
    # values.
 
835
    def initialize
 
836
      set_root_path!
 
837
 
 
838
      self.frameworks                   = default_frameworks
 
839
      self.load_paths                   = default_load_paths
 
840
      self.load_once_paths              = default_load_once_paths
 
841
      self.eager_load_paths             = default_eager_load_paths
 
842
      self.log_path                     = default_log_path
 
843
      self.log_level                    = default_log_level
 
844
      self.view_path                    = default_view_path
 
845
      self.controller_paths             = default_controller_paths
 
846
      self.preload_frameworks           = default_preload_frameworks
 
847
      self.cache_classes                = default_cache_classes
 
848
      self.dependency_loading           = default_dependency_loading
 
849
      self.whiny_nils                   = default_whiny_nils
 
850
      self.plugins                      = default_plugins
 
851
      self.plugin_paths                 = default_plugin_paths
 
852
      self.plugin_locators              = default_plugin_locators
 
853
      self.plugin_loader                = default_plugin_loader
 
854
      self.database_configuration_file  = default_database_configuration_file
 
855
      self.routes_configuration_file    = default_routes_configuration_file
 
856
      self.gems                         = default_gems
 
857
      self.i18n                         = default_i18n
 
858
 
 
859
      for framework in default_frameworks
 
860
        self.send("#{framework}=", Rails::OrderedOptions.new)
 
861
      end
 
862
      self.active_support = Rails::OrderedOptions.new
 
863
    end
 
864
 
 
865
    # Set the root_path to RAILS_ROOT and canonicalize it.
 
866
    def set_root_path!
 
867
      raise 'RAILS_ROOT is not set' unless defined?(::RAILS_ROOT)
 
868
      raise 'RAILS_ROOT is not a directory' unless File.directory?(::RAILS_ROOT)
 
869
 
 
870
      @root_path =
 
871
        # Pathname is incompatible with Windows, but Windows doesn't have
 
872
        # real symlinks so File.expand_path is safe.
 
873
        if RUBY_PLATFORM =~ /(:?mswin|mingw)/
 
874
          File.expand_path(::RAILS_ROOT)
 
875
 
 
876
        # Otherwise use Pathname#realpath which respects symlinks.
 
877
        else
 
878
          Pathname.new(::RAILS_ROOT).realpath.to_s
 
879
        end
 
880
 
 
881
      Object.const_set(:RELATIVE_RAILS_ROOT, ::RAILS_ROOT.dup) unless defined?(::RELATIVE_RAILS_ROOT)
 
882
      ::RAILS_ROOT.replace @root_path
 
883
    end
 
884
 
 
885
    # Enable threaded mode. Allows concurrent requests to controller actions and
 
886
    # multiple database connections. Also disables automatic dependency loading
 
887
    # after boot, and disables reloading code on every request, as these are
 
888
    # fundamentally incompatible with thread safety.
 
889
    def threadsafe!
 
890
      self.preload_frameworks = true
 
891
      self.cache_classes = true
 
892
      self.dependency_loading = false
 
893
      self.action_controller.allow_concurrency = true
 
894
      self
 
895
    end
 
896
 
 
897
    # Loads and returns the contents of the #database_configuration_file. The
 
898
    # contents of the file are processed via ERB before being sent through
 
899
    # YAML::load.
 
900
    def database_configuration
 
901
      require 'erb'
 
902
      YAML::load(ERB.new(IO.read(database_configuration_file)).result)
 
903
    end
 
904
 
 
905
    # The path to the current environment's file (<tt>development.rb</tt>, etc.). By
 
906
    # default the file is at <tt>config/environments/#{environment}.rb</tt>.
 
907
    def environment_path
 
908
      "#{root_path}/config/environments/#{environment}.rb"
 
909
    end
 
910
 
 
911
    # Return the currently selected environment. By default, it returns the
 
912
    # value of the RAILS_ENV constant.
 
913
    def environment
 
914
      ::RAILS_ENV
 
915
    end
 
916
 
 
917
    # Adds a block which will be executed after rails has been fully initialized.
 
918
    # Useful for per-environment configuration which depends on the framework being
 
919
    # fully initialized.
 
920
    def after_initialize(&after_initialize_block)
 
921
      after_initialize_blocks << after_initialize_block if after_initialize_block
 
922
    end
 
923
 
 
924
    # Returns the blocks added with Configuration#after_initialize
 
925
    def after_initialize_blocks
 
926
      @after_initialize_blocks ||= []
 
927
    end
 
928
 
 
929
    # Add a preparation callback that will run before every request in development
 
930
    # mode, or before the first request in production.
 
931
    #
 
932
    # See Dispatcher#to_prepare.
 
933
    def to_prepare(&callback)
 
934
      after_initialize do
 
935
        require 'dispatcher' unless defined?(::Dispatcher)
 
936
        Dispatcher.to_prepare(&callback)
 
937
      end
 
938
    end
 
939
 
 
940
    def middleware
 
941
      require 'action_controller'
 
942
      ActionController::Dispatcher.middleware
 
943
    end
 
944
 
 
945
    def builtin_directories
 
946
      # Include builtins only in the development environment.
 
947
      (environment == 'development') ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
 
948
    end
 
949
 
 
950
    def framework_paths
 
951
      paths = %w(railties railties/lib activesupport/lib)
 
952
      paths << 'actionpack/lib' if frameworks.include?(:action_controller) || frameworks.include?(:action_view)
 
953
 
 
954
      [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
 
955
        paths << "#{framework.to_s.gsub('_', '')}/lib" if frameworks.include?(framework)
 
956
      end
 
957
 
 
958
      paths.map { |dir| "#{framework_root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
 
959
    end
 
960
 
 
961
    private
 
962
      def framework_root_path
 
963
        defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root_path}/vendor/rails"
 
964
      end
 
965
 
 
966
      def default_frameworks
 
967
        [ :active_record, :action_controller, :action_view, :action_mailer, :active_resource ]
 
968
      end
 
969
 
 
970
      def default_load_paths
 
971
        paths = []
 
972
 
 
973
        # Add the old mock paths only if the directories exists
 
974
        paths.concat(Dir["#{root_path}/test/mocks/#{environment}"]) if File.exists?("#{root_path}/test/mocks/#{environment}")
 
975
 
 
976
        # Add the app's controller directory
 
977
        paths.concat(Dir["#{root_path}/app/controllers/"])
 
978
 
 
979
        # Followed by the standard includes.
 
980
        paths.concat %w(
 
981
          app
 
982
          app/metal
 
983
          app/models
 
984
          app/controllers
 
985
          app/helpers
 
986
          app/services
 
987
          lib
 
988
          vendor
 
989
        ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
 
990
 
 
991
        paths.concat builtin_directories
 
992
      end
 
993
 
 
994
      # Doesn't matter since plugins aren't in load_paths yet.
 
995
      def default_load_once_paths
 
996
        []
 
997
      end
 
998
 
 
999
      def default_eager_load_paths
 
1000
        %w(
 
1001
          app/metal
 
1002
          app/models
 
1003
          app/controllers
 
1004
          app/helpers
 
1005
        ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
 
1006
      end
 
1007
 
 
1008
      def default_log_path
 
1009
        File.join(root_path, 'log', "#{environment}.log")
 
1010
      end
 
1011
 
 
1012
      def default_log_level
 
1013
        environment == 'production' ? :info : :debug
 
1014
      end
 
1015
 
 
1016
      def default_database_configuration_file
 
1017
        File.join(root_path, 'config', 'database.yml')
 
1018
      end
 
1019
 
 
1020
      def default_routes_configuration_file
 
1021
        File.join(root_path, 'config', 'routes.rb')
 
1022
      end
 
1023
 
 
1024
      def default_view_path
 
1025
        File.join(root_path, 'app', 'views')
 
1026
      end
 
1027
 
 
1028
      def default_controller_paths
 
1029
        paths = [File.join(root_path, 'app', 'controllers')]
 
1030
        paths.concat builtin_directories
 
1031
        paths
 
1032
      end
 
1033
 
 
1034
      def default_dependency_loading
 
1035
        true
 
1036
      end
 
1037
 
 
1038
      def default_preload_frameworks
 
1039
        false
 
1040
      end
 
1041
 
 
1042
      def default_cache_classes
 
1043
        true
 
1044
      end
 
1045
 
 
1046
      def default_whiny_nils
 
1047
        false
 
1048
      end
 
1049
 
 
1050
      def default_plugins
 
1051
        nil
 
1052
      end
 
1053
 
 
1054
      def default_plugin_paths
 
1055
        ["#{root_path}/vendor/plugins"]
 
1056
      end
 
1057
 
 
1058
      def default_plugin_locators
 
1059
        locators = []
 
1060
        locators << Plugin::GemLocator if defined? Gem
 
1061
        locators << Plugin::FileSystemLocator
 
1062
      end
 
1063
 
 
1064
      def default_plugin_loader
 
1065
        Plugin::Loader
 
1066
      end
 
1067
 
 
1068
      def default_cache_store
 
1069
        if File.exist?("#{root_path}/tmp/cache/")
 
1070
          [ :file_store, "#{root_path}/tmp/cache/" ]
 
1071
        else
 
1072
          :memory_store
 
1073
        end
 
1074
      end
 
1075
 
 
1076
      def default_gems
 
1077
        []
 
1078
      end
 
1079
 
 
1080
      def default_i18n
 
1081
        i18n = Rails::OrderedOptions.new
 
1082
        i18n.load_path = []
 
1083
 
 
1084
        if File.exist?(File.join(RAILS_ROOT, 'config', 'locales'))
 
1085
          i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
 
1086
          i18n.load_path.flatten!
 
1087
        end
 
1088
 
 
1089
        i18n
 
1090
      end
 
1091
  end
 
1092
end
 
1093
 
 
1094
# Needs to be duplicated from Active Support since its needed before Active
 
1095
# Support is available. Here both Options and Hash are namespaced to prevent
 
1096
# conflicts with other implementations AND with the classes residing in Active Support.
 
1097
class Rails::OrderedOptions < Array #:nodoc:
 
1098
  def []=(key, value)
 
1099
    key = key.to_sym
 
1100
 
 
1101
    if pair = find_pair(key)
 
1102
      pair.pop
 
1103
      pair << value
 
1104
    else
 
1105
      self << [key, value]
 
1106
    end
 
1107
  end
 
1108
 
 
1109
  def [](key)
 
1110
    pair = find_pair(key.to_sym)
 
1111
    pair ? pair.last : nil
 
1112
  end
 
1113
 
 
1114
  def method_missing(name, *args)
 
1115
    if name.to_s =~ /(.*)=$/
 
1116
      self[$1.to_sym] = args.first
 
1117
    else
 
1118
      self[name]
 
1119
    end
 
1120
  end
 
1121
 
 
1122
  private
 
1123
    def find_pair(key)
 
1124
      self.each { |i| return i if i.first == key }
 
1125
      return false
 
1126
    end
 
1127
end
 
1128