天天看點

Advanced Rails - Rails初始化20步

initializer.rb

Rails::Initialzier是用來建立Rails環境的主要類。Initializer是在config/environment.rb裡面觸發的,它裡面包含了下面的一個代碼塊:

Rails::Initializer.run帶進一個新的對象Rails::Configuration到這個代碼塊裡。然後,run建立一個新的Rails::Initializer對象并且調用他的process方法,這個方法依次用下面的步驟來初始化Rails:

1. check_ruby_version: 保證使用的Ruby版本大于1.8.2

2. set_load_path: 添加架構的路徑(RailTies, ActionPack, ActiveSupport, ActiveRecord, Action Mailer, and Action Web Service)和應用裝載的路徑到Ruby的裝載路徑裡。架構是在vendor/rails或者在RAILS_FRAMEWORK_ROOT指定的路徑裝載的。

3. require_frameworks: Loads each framework listed in the frameworks configuration option. If the framework path was not specified in RAILS_FRAMEWORK_ROOT and it does not exist in vendor/rails, Initializer will assume the frameworks are installed as RubyGems.

4.

set_autoload_paths: Sets the autoload paths based on the values of the load_ paths and load_once_paths configuration variables. These determine which paths will be searched to resolve unknown constants. The load_paths option is the same one that provided the application's load paths in step 2.

5.

load_environment: Loads and evaluates the environment-specific (development, production, or test) configuration file.

6.

initialize_encoding: Sets $KCODE to u for UTF-8 support throughout Rails.

7.

initialize_database: If ActiveRecord is being used, sets up its database configuration and connects to the database server.

8.

initialize_logger: Sets up the logger and sets the top-level constant RAILS_ DEFAULT_LOGGER to the instance. If logger is specified in the configuration, it is used. If not, a new logger is created and directed to the log_path specified. If that fails, a warning is displayed and logging is redirected to standard error.

9.

initialize_framework_logging: Sets the logger for ActiveRecord, ActionController, and Action Mailer (if they are being used) to the logger that was just set up.

10.

initialize_framework_views: Sets the view path for ActionController and Action Mailer to the value of the view_path configuration item.

11.

initialize_dependency_mechanism: Sets Dependencies.mechanism (which determines whether to use require or load to load files) based on the setting of the cache_classes configuration item.

12.

initialize_whiny_nils: If the whiny_nils configuration item is true, adds the WhinyNil extensions (that complain when trying to call id or other methods on nil) to NilClass.

13.

initialize_temporary_directories: Sets ActionController's temporary session and cache directories if they exist in the filesystem.

14.

initialize_framework_settings: Transforms the framework-specific configuration settings into method calls on the frameworks' Base classes. For example, consider the configuration option:

config.active_record.schema_format = :sql

The config.active_record object is an instance of Rails::OrderedOptions, which is basically an ordered hash (ordered to keep the configuration directives in order). During initialization, the initialize_framework_settings method transforms it into the following:

ActiveRecord::Base.schema_format = :sql

This offers the advantage that the Configuration object doesn't have to be updated every time a framework adds or changes a configuration option.

15.

add_support_load_paths: Adds load paths for support functions. This function is currently empty.

16.

load_plugins: Loads the plugins from paths in the plugin_paths configuration item (default vendor/plugins). If a plugins configuration item is specified, load those plugins respecting that load order. Plugins are loaded close to the end of the process so that they can override any already loaded component.

17.

load_observers: Instantiates ActiveRecord observers. This is done after plugins so that plugins have an opportunity to modify the observer classes.

18.

initialize_routing: Loads and processes the routes. Also sets the controller paths from the controller_paths configuration item.

19.

after_initialize: Calls any user-defined after_initialize callback. These call-backs are defined in the configuration block by config.after_initialize { … }.

20.

load_application_initializers: Loads all Rubyfiles in RAILS_ROOT/config/ initializers and any of its subdirectories. Old framework initialization that may previouslyhave been contained in config/environment.rb can now properly be broken out into separate initializers.

Now the framework is ready to receive requests.

繼續閱讀