Class

Rails::Initializer

Inheritance
< Object

The Initializer is responsible for processing the Rails configuration, such as setting the $LOAD_PATH, requiring the right frameworks, initializing logging, and more. It can be run either as a single command that‘ll just use the default configuration, like this:

  Rails::Initializer.run

But normally it‘s more interesting to pass in a custom configuration through the block running:

  Rails::Initializer.run do |config|
    config.frameworks -= [ :action_mailer ]
  end

This will use the default configuration options from Rails::Configuration, but allow for overwriting on select areas.

Attributes

Name Visibility R/W Description
configuration public R The Configuration instance used by this Initializer instance.
gems_dependencies_loaded public R Whether or not all the gem dependencies have been met
loaded_plugins public R The set of loaded plugins.

Methods

Class

Visibility Signature
public new (configuration)
public run (command = :process, configuration = Configuration.new) {|configuration if block_given?| ...}

Instance

Visibility Signature
public add_gem_load_paths ()
public add_plugin_load_paths ()
public add_support_load_paths ()
public after_initialize ()
public check_gem_dependencies ()
public check_ruby_version ()
public disable_dependency_loading ()
public initialize_cache ()
public initialize_database ()
public initialize_database_middleware ()
public initialize_dependency_mechanism ()
public initialize_encoding ()
public initialize_framework_caches ()
public initialize_framework_logging ()
public initialize_framework_settings ()
public initialize_framework_views ()
public initialize_i18n ()
public initialize_logger ()
public initialize_metal ()
public initialize_routing ()
public initialize_time_zone ()
public initialize_whiny_nils ()
public install_gem_spec_stubs ()
public load_application_classes ()
public load_application_initializers ()
public load_environment ()
public load_gems ()
public load_observers ()
public load_plugins ()
public load_view_paths ()
public plugin_loader ()
public preload_frameworks ()
public prepare_dispatcher ()
public process ()
public require_frameworks ()
public set_autoload_paths ()
public set_load_path ()

Class Method Detail

new(configuration)

Create a new Initializer instance that references the given Configuration instance.

run(command = :process, configuration = Configuration.new) {|configuration if block_given?| ...}

Runs the initializer. By default, this will invoke the process method, which simply executes all of the initialization routines. Alternately, you can specify explicitly which initialization routine you want:

  Rails::Initializer.run(:set_load_path)

This is useful if you only want the load path initialized, without incurring the overhead of completely loading the entire environment.

Instance Method Detail

add_gem_load_paths()

add_plugin_load_paths()

Adds all load paths from plugins to the global set of load paths, so that code from plugins can be required (explicitly or automatically via ActiveSupport::Dependencies).

add_support_load_paths()

Add the load paths used by support functions such as the info controller

after_initialize()

Fires the user-supplied after_initialize block (Configuration#after_initialize)

check_gem_dependencies()

check_ruby_version()

Check for valid Ruby version This is done in an external file, so we can use it from the `rails` program as well without duplication.

disable_dependency_loading()

initialize_cache()

initialize_database()

This initialization routine does nothing unless :active_record is one of the frameworks to load (Configuration#frameworks). If it is, this sets the database configuration from Configuration#database_configuration and then establishes the connection.

initialize_database_middleware()

initialize_dependency_mechanism()

Sets the dependency loading mechanism based on the value of Configuration#cache_classes.

initialize_encoding()

For Ruby 1.8, this initialization sets $KCODE to ‘u’ to enable the multibyte safe operations. Plugin authors supporting other encodings should override this behaviour and set the relevant default_charset on ActionController::Base.

For Ruby 1.9, this does nothing. Specify the default encoding in the Ruby shebang line if you don‘t want UTF-8.

initialize_framework_caches()

initialize_framework_logging()

Sets the logger for Active Record, Action Controller, and Action Mailer (but only for those frameworks that are to be loaded). If the framework‘s logger is already set, it is not changed, otherwise it is set to use RAILS_DEFAULT_LOGGER.

initialize_framework_settings()

Initializes framework-specific settings for each of the loaded frameworks (Configuration#frameworks). The available settings map to the accessors on each of the corresponding Base classes.

initialize_framework_views()

Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+ (but only for those frameworks that are to be loaded). If the framework‘s paths have already been set, it is not changed, otherwise it is set to use Configuration#view_path.

initialize_i18n()

Set the i18n configuration from config.i18n but special-case for the load_path which should be appended to what‘s already set instead of overwritten.

initialize_logger()

If the RAILS_DEFAULT_LOGGER constant is already set, this initialization routine does nothing. If the constant is not set, and Configuration#logger is not nil, this also does nothing. Otherwise, a new logger instance is created at Configuration#log_path, with a default log level of Configuration#log_level.

If the log could not be created, the log will be set to output to STDERR, with a log level of WARN.

initialize_metal()

initialize_routing()

If Action Controller is not one of the loaded frameworks (Configuration#frameworks) this does nothing. Otherwise, it loads the routing definitions and sets up loading module used to lazily load controllers (Configuration#controller_paths).

initialize_time_zone()

Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes. If assigned value cannot be matched to a TimeZone, an exception will be raised.

initialize_whiny_nils()

Loads support for "whiny nil" (noisy warnings when methods are invoked on nil values) if Configuration#whiny_nils is true.

install_gem_spec_stubs()

If Rails is vendored and RubyGems is available, install stub GemSpecs for Rails, Active Support, Active Record, Action Pack, Action Mailer, and Active Resource. This allows Gem plugins to depend on Rails even when the Gem version of Rails shouldn‘t be loaded.

load_application_classes()

Eager load application classes

load_application_initializers()

load_environment()

Loads the environment specified by Configuration#environment_path, which is typically one of development, test, or production.

load_gems()

load_observers()

load_plugins()

Loads all plugins in config.plugin_paths. plugin_paths defaults to vendor/plugins but may also be set to a list of paths, such as

  config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]

In the default implementation, as each plugin discovered in plugin_paths is initialized:

  • its lib directory, if present, is added to the load path (immediately after the applications lib directory)
  • init.rb is evaluated, if present

After all plugins are loaded, duplicates are removed from the load path. If an array of plugin names is specified in config.plugins, only those plugins will be loaded and they plugins will be loaded in that order. Otherwise, plugins are loaded in alphabetical order.

if config.plugins ends contains :all then the named plugins will be loaded in the given order and all other plugins will be loaded in alphabetical order

load_view_paths()

plugin_loader()

preload_frameworks()

Preload all frameworks specified by the Configuration#frameworks. Used by Passenger to ensure everything‘s loaded before forking and to avoid autoload race conditions in JRuby.

prepare_dispatcher()

process()

Sequentially step through all of the available initialization routines, in order (view execution order in source).

require_frameworks()

Requires all frameworks specified by the Configuration#frameworks list. By default, all frameworks (Active Record, Active Support, Action Pack, Action Mailer, and Active Resource) are loaded.

set_autoload_paths()

Set the paths from which Rails will automatically load source files, and the load_once paths.

set_load_path()

Set the $LOAD_PATH based on the value of Configuration#load_paths. Duplicates are removed.