Advanced Rails - Rails初始化20步

initializer.rb

Rails::Initialzier是用来建立Rails环境的主要类。Initializer是在config/environment.rb里面触发的,它里面包含了下面的一个代码块:


Rails::Initializer.run do |config|
# (configuration)
end


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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值