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