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