mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Config guide: Use bold titles for initializers instead of headings.
This commit is contained in:
parent
e4456b6f03
commit
ae7825d57c
1 changed files with 58 additions and 153 deletions
|
@ -419,219 +419,124 @@ The block's argument of the +initialize+ is the instance of the application itse
|
|||
|
||||
Because +Rails::Application+ inherits from +Rails::Railtie+ (indirectly), you can use the +initializer+ method in +config/application.rb+ to define initializers for the application.
|
||||
|
||||
h4. Initializers
|
||||
|
||||
Below is a comprehensive list of all the initializers found in Rails in the order that they are defined (and therefore run in, unless otherwise stated).
|
||||
|
||||
h4. +load_environment_hook+
|
||||
|
||||
*+load_environment_hook+*
|
||||
Serves as a placeholder so that +:load_environment_config+ can be defined to run before it.
|
||||
|
||||
h4. +load_active_support+
|
||||
*+load_active_support+* Requires +active_support/dependencies+ which sets up the basis for Active Support. Optionally requires +active_support/all+ if +config.active_support.bare+ is un-truthful, which is the default.
|
||||
|
||||
Requires +active_support/dependencies+ which sets up the basis for Active Support. Optionally requires +active_support/all+ if +config.active_support.bare+ is un-truthful, which is the default.
|
||||
*+preload_frameworks+* Will load all autoload dependencies of Rails automatically if +config.preload_frameworks+ is +true+ or "truthful". By default this configuration option is disabled. In Rails, when internal classes are referenced for the first time they are autoloaded. +:preload_frameworks+ loads all of this at once on initialization.
|
||||
|
||||
h4. +preload_frameworks+
|
||||
*+initialize_logger+* Initializes the logger (an +ActiveSupport::BufferedLogger+ object) for the application and makes it accessible at +Rails.logger+, providing that there's no initializer inserted before this point that has defined +Rails.logger+.
|
||||
|
||||
Will load all autoload dependencies of Rails automatically if +config.preload_frameworks+ is +true+ or "truthful". By default this configuration option is disabled. In Rails, when internal classes are referenced for the first time they are autoloaded. +:preload_frameworks+ loads all of this at once on initialization.
|
||||
*+initialize_cache+* If +RAILS_CACHE+ isn't yet set, initializes the cache by referencing the value in +config.cache_store+ and stores the outcome as +RAILS_CACHE+. If this object responds to the +middleware+ method, its middleware is inserted before +Rack::Runtime+ in the middleware stack.
|
||||
|
||||
h4. +initialize_logger+
|
||||
*+set_clear_dependencies_hook+* Provides a hook for +active_record.set_dispatch_hooks+ to use, which will run before this initializer. This initializer -- which runs only if +cache_classes+ is set to +false+ -- uses +ActionDispatch::Callbacks.after+ to remove the constants which have been referenced during the request from the object space so that they will be reloaded during the following request.
|
||||
|
||||
Initializes the logger (an +ActiveSupport::BufferedLogger+ object) for the application and makes it accessible at +Rails.logger+, providing that there's no initializer inserted before this point that has defined +Rails.logger+.
|
||||
*+initialize_dependency_mechanism+* If +config.cache_classes+ is set to +true+, configures +ActiveSupport::Dependencies.mechanism+ to +require+ dependencies rather than +load+ them.
|
||||
|
||||
h4. +initialize_cache+
|
||||
*+bootstrap_hook+* Runs all configured +before_initialize+ blocks.
|
||||
|
||||
If +RAILS_CACHE+ isn't yet set, initializes the cache by referencing the value in +config.cache_store+ and stores the outcome as +RAILS_CACHE+. If this object responds to the +middleware+ method, its middleware is inserted before +Rack::Runtime+ in the middleware stack.
|
||||
*+i18n.callbacks+* In the development environment, sets up a +to_prepare+ callback which will call +I18n.reload!+ if any of the locales have changed since the last request. In production mode this callback will only run on the first request.
|
||||
|
||||
h4. +set_clear_dependencies_hook+
|
||||
*+active_support.initialize_whiny_nils+* Will require +active_support/whiny_nil+ if +config.whiny_nil+ is set to +true+. This file will output errors such as:
|
||||
|
||||
Provides a hook for +active_record.set_dispatch_hooks+ to use, which will run before this initializer.
|
||||
|
||||
This initializer -- which runs only if +cache_classes+ is set to +false+ -- uses +ActionDispatch::Callbacks.after+ to remove the constants which have been referenced during the request from the object space so that they will be reloaded during the following request.
|
||||
|
||||
h4. +initialize_dependency_mechanism+
|
||||
|
||||
If +config.cache_classes+ is set to +true+, configures +ActiveSupport::Dependencies.mechanism+ to +require+ dependencies rather than +load+ them.
|
||||
|
||||
h4. +bootstrap_hook+
|
||||
|
||||
Runs all configured +before_initialize+ blocks.
|
||||
|
||||
h4. +i18n.callbacks+
|
||||
|
||||
In the development environment, sets up a +to_prepare+ callback which will call +I18n.reload!+ if any of the locales have changed since the last request. In production mode this callback will only run on the first request.
|
||||
|
||||
h4. +active_support.initialize_whiny_nils+
|
||||
|
||||
Will require +active_support/whiny_nil+ if +config.whiny_nil+ is set to +true+. This file will output errors such as:
|
||||
|
||||
<text>
|
||||
<plain>
|
||||
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
|
||||
</text>
|
||||
</plain>
|
||||
|
||||
And:
|
||||
|
||||
<text>
|
||||
You have a nil object when you didn't expect it!
|
||||
You might have expected an instance of Array.
|
||||
The error occurred while evaluating nil.each
|
||||
</text>
|
||||
<plain>
|
||||
You have a nil object when you didn't expect it!
|
||||
You might have expected an instance of Array.
|
||||
The error occurred while evaluating nil.each
|
||||
</plain>
|
||||
|
||||
h4. +active_support.deprecation_behavior+
|
||||
*+active_support.deprecation_behavior+* Sets up deprecation reporting for environments, defaulting to +log+ for development, +notify+ for production and +stderr+ for test. If a value isn't set for +config.active_support.deprecation+ then this initializer will prompt the user to configure this line in the current environment's +config/environments+ file.
|
||||
|
||||
Sets up deprecation reporting for environments, defaulting to +log+ for development, +notify+ for production and +stderr+ for test. If a value isn't set for +config.active_support.deprecation+ then this initializer will prompt the user to configure this line in the current environment's +config/environments+ file.
|
||||
*+active_support.initialize_time_zone+* Sets the default time zone for the application based off the +config.time_zone+ setting, which defaults to "UTC".
|
||||
|
||||
h4. +active_support.initialize_time_zone+
|
||||
*+action_dispatch.configure+* Configures the +ActionDispatch::Http::URL.tld_length+ to be set to the value of +config.action_dispatch.tld_length+.
|
||||
|
||||
Sets the default time zone for the application based off the +config.time_zone+ setting, which defaults to "UTC".
|
||||
*+action_view.cache_asset_ids+* Will set +ActionView::Helpers::AssetTagHelper::AssetPaths.cache_asset_ids+ to +false+ when Active Support loads, but only if +config.cache_classes+ is too.
|
||||
|
||||
h4. +action_dispatch.configure+
|
||||
*+action_view.javascript_expansions+* Registers the expansions set up by +config.action_view.javascript_expansions+ and +config.action_view.stylesheet_expansions+ to be recognised by Action View and therefore usable in the views.
|
||||
|
||||
Configures the +ActionDispatch::Http::URL.tld_length+ to be set to the value of +config.action_dispatch.tld_length+.
|
||||
*+action_view.set_configs+* Sets up Action View by using the settings in +config.action_view+ by +send+'ing the method names as setters to +ActionView::Base+ and passing the values through.
|
||||
|
||||
h4. +action_view.cache_asset_ids+
|
||||
*+action_controller.logger+* Sets +ActionController::Base.logger+ -- if it's not already set -- to +Rails.logger+.
|
||||
|
||||
Will set +ActionView::Helpers::AssetTagHelper::AssetPaths.cache_asset_ids+ to +false+ when Active Support loads, but only if +config.cache_classes+ is too.
|
||||
*+action_controller.initialize_framework_caches+* Sets +ActionController::Base.cache_store+ -- if it's not already set -- to +RAILS_CACHE+.
|
||||
|
||||
h4. +action_view.javascript_expansions+
|
||||
*+action_controller.set_configs+* Sets up Action Controller by using the settings in +config.action_controller+ by +send+'ing the method names as setters to +ActionController::Base+ and passing the values through.
|
||||
|
||||
Registers the expansions set up by +config.action_view.javascript_expansions+ and +config.action_view.stylesheet_expansions+ to be recognised by Action View and therefore usable in the views.
|
||||
*+action_controller.compile_config_methods+* Initializes methods for the config settings specified so that they are quicker to access.
|
||||
|
||||
h4. +action_view.set_configs+
|
||||
*+active_record.initialize_timezone+* Sets +ActiveRecord::Base.time_zone_aware_attributes+ to true, as well as setting +ActiveRecord::Base.default_timezone+ to UTC. When attributes are read from the database, they will be converted into the time zone specified by +Time.zone+.
|
||||
|
||||
Sets up Action View by using the settings in +config.action_view+ by +send+'ing the method names as setters to +ActionView::Base+ and passing the values through.
|
||||
*+active_record.logger+* Sets +ActiveRecord::Base.logger+ -- if it's not already set -- to +Rails.logger+.
|
||||
|
||||
h4. +action_controller.logger+
|
||||
*+active_record.set_configs+* Sets up Active Record by using the settings in +config.active_record+ by +send+'ing the method names as setters to +ActiveRecord::Base+ and passing the values through.
|
||||
|
||||
Sets +ActionController::Base.logger+ -- if it's not already set -- to +Rails.logger+.
|
||||
*+active_record.initialize_database+* Loads the database configuration (by default) from +config/database.yml+ and establishes a connection for the current environment.
|
||||
|
||||
h4. +action_controller.initialize_framework_caches+
|
||||
*+active_record.log_runtime+* Includes +ActiveRecord::Railties::ControllerRuntime+ which is responsible for reporting the length of time Active Record calls took for the request back to the logger.
|
||||
|
||||
Sets +ActionController::Base.cache_store+ -- if it's not already set -- to +RAILS_CACHE+.
|
||||
*+active_record.set_dispatch_hooks+* If +config.cache_classes+ is set to false, all reloadable connections to the database will be reset.
|
||||
|
||||
h4. +action_controller.set_configs+
|
||||
*+action_mailer.logger+* Sets +ActionMailer::Base.logger+ -- if it's not already set -- to +Rails.logger+.
|
||||
|
||||
Sets up Action Controller by using the settings in +config.action_controller+ by +send+'ing the method names as setters to +ActionController::Base+ and passing the values through.
|
||||
*+action_mailer.set_configs+* Sets up Action Mailer by using the settings in +config.action_mailer+ by +send+'ing the method names as setters to +ActionMailer::Base+ and passing the values through.
|
||||
|
||||
h4. +action_controller.compile_config_methods+
|
||||
*+action_mailer.compile_config_methods+* Initializes methods for the config settings specified so that they are quicker to access.
|
||||
|
||||
Initializes methods for the config settings specified so that they are quicker to access.
|
||||
*+active_resource.set_configs+* Sets up Active Resource by using the settings in +config.active_resource+ by +send+'ing the method names as setters to +ActiveResource::Base+ and passing the values through.
|
||||
|
||||
h4. +active_record.initialize_timezone+
|
||||
*+set_load_path+* This initializer runs before +bootstrap_hook+. Adds the +vendor+, +lib+, all directories of +app+ and any paths specified by +config.load_paths+ to +$LOAD_PATH+.
|
||||
|
||||
Sets +ActiveRecord::Base.time_zone_aware_attributes+ to true, as well as setting +ActiveRecord::Base.default_timezone+ to UTC. When attributes are read from the database, they will be converted into the time zone specified by +Time.zone+.
|
||||
*+set_autoload_path+* This initializer runs before +bootstrap_hook+. Adds all sub-directories of +app+ and paths specified by +config.autoload_paths+ to +ActiveSupport::Dependencies.autoload_paths+.
|
||||
|
||||
h4. +active_record.logger+
|
||||
*+add_routing_paths+* Loads (by default) all +config/routes.rb+ files (in the application and railties, including engines) and sets up the routes for the application.
|
||||
|
||||
Sets +ActiveRecord::Base.logger+ -- if it's not already set -- to +Rails.logger+.
|
||||
*+add_locales+* Adds the files in +config/locales+ (from the application, railties and engines) to +I18n.load_path+, making available the translations in these files.
|
||||
|
||||
h4. +active_record.set_configs+
|
||||
*+add_view_paths+* Adds the directory +app/views+ from the application, railties and engines to the lookup path for view files for the application.
|
||||
|
||||
Sets up Active Record by using the settings in +config.active_record+ by +send+'ing the method names as setters to +ActiveRecord::Base+ and passing the values through.
|
||||
*+load_environment_config+* Loads the +config/environments+ file for the current environment.
|
||||
|
||||
h4. +active_record.initialize_database+
|
||||
*+append_asset_paths+* Finds asset paths for the application and all attached railties and keeps a track of the available directories in +config.static_asset_paths+.
|
||||
|
||||
Loads the database configuration (by default) from +config/database.yml+ and establishes a connection for the current environment.
|
||||
*+prepend_helpers_path+* Adds the directory +app/helpers+ from the application, railties and engines to the lookup path for helpers for the application.
|
||||
|
||||
h4. +active_record.log_runtime+
|
||||
|
||||
Includes +ActiveRecord::Railties::ControllerRuntime+ which is responsible for reporting the length of time Active Record calls took for the request back to the logger.
|
||||
|
||||
h4. +active_record.set_dispatch_hooks+
|
||||
|
||||
If +config.cache_classes+ is set to false, all reloadable connections to the database will be reset.
|
||||
|
||||
h4. +action_mailer.logger+
|
||||
|
||||
Sets +ActionMailer::Base.logger+ -- if it's not already set -- to +Rails.logger+.
|
||||
|
||||
h4. +action_mailer.set_configs+
|
||||
|
||||
Sets up Action Mailer by using the settings in +config.action_mailer+ by +send+'ing the method names as setters to +ActionMailer::Base+ and passing the values through.
|
||||
|
||||
h4. +action_mailer.compile_config_methods+
|
||||
|
||||
Initializes methods for the config settings specified so that they are quicker to access.
|
||||
|
||||
h4. +active_resource.set_configs+
|
||||
|
||||
Sets up Active Resource by using the settings in +config.active_resource+ by +send+'ing the method names as setters to +ActiveResource::Base+ and passing the values through.
|
||||
|
||||
h4. +set_load_path+
|
||||
|
||||
This initializer runs before +bootstrap_hook+. Adds the +vendor+, +lib+, all directories of +app+ and any paths specified by +config.load_paths+ to +$LOAD_PATH+.
|
||||
|
||||
h4. +set_autoload_path+
|
||||
|
||||
This initializer runs before +bootstrap_hook+. Adds all sub-directories of +app+ and paths specified by +config.autoload_paths+ to +ActiveSupport::Dependencies.autoload_paths+.
|
||||
|
||||
h4. +add_routing_paths+
|
||||
|
||||
Loads (by default) all +config/routes.rb+ files (in the application and railties, including engines) and sets up the routes for the application.
|
||||
|
||||
h4. +add_locales+
|
||||
|
||||
Adds the files in +config/locales+ (from the application, railties and engines) to +I18n.load_path+, making available the translations in these files.
|
||||
|
||||
h4. +add_view_paths+
|
||||
|
||||
Adds the directory +app/views+ from the application, railties and engines to the lookup path for view files for the application.
|
||||
|
||||
h4. +load_environment_config+
|
||||
|
||||
Loads the +config/environments+ file for the current environment.
|
||||
|
||||
h4. +append_asset_paths+
|
||||
|
||||
Finds asset paths for the application and all attached railties and keeps a track of the available directories in +config.static_asset_paths+.
|
||||
|
||||
h4. +prepend_helpers_path+
|
||||
|
||||
Adds the directory +app/helpers+ from the application, railties and engines to the lookup path for helpers for the application.
|
||||
|
||||
h4. +load_config_initializers+
|
||||
|
||||
Loads all files from +config/initializers+ in the application, railties and engines. The files in this directory can be used to hold configuration settings that should be made after all of the frameworks and plugins are loaded.
|
||||
*+load_config_initializers+* Loads all Ruby files from +config/initializers+ in the application, railties and engines. The files in this directory can be used to hold configuration settings that should be made after all of the frameworks and plugins are loaded.
|
||||
|
||||
NOTE: You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the +initializers+ folder on down.
|
||||
|
||||
TIP: If you have any ordering dependency in your initializers, you can control the load order by naming. For example, +01_critical.rb+ will be loaded before +02_normal.rb+.
|
||||
|
||||
h4. +engines_blank_point+
|
||||
*+engines_blank_point+* Provides a point-in-initialization to hook into if you wish to do anything before engines are loaded. After this point, all railtie and engine initializers are ran.
|
||||
|
||||
Provides a point-in-initialization to hook into if you wish to do anything before engines are loaded. After this point, all railtie and engine initializers are ran.
|
||||
*+add_generator_templates+* Finds templates for generators at +lib/templates+ for the application, railities and engines and adds these to the +config.generators.templates+ setting, which will make the templates available for all generators to reference.
|
||||
|
||||
h4. +add_generator_templates+
|
||||
*+ensure_autoload_once_paths_as_subset+* Ensures that the +config.autoload_once_paths+ only contains paths from +config.autoload_paths+. If it contains extra paths, then an exception will be raised.
|
||||
|
||||
Finds templates for generators at +lib/templates+ for the application, railities and engines and adds these to the +config.generators.templates+ setting, which will make the templates available for all generators to reference.
|
||||
*+add_to_prepare_blocks+* The block for every +config.to_prepare+ call in the application, a railtie or engine is added to the +to_prepare+ callbacks for Action Dispatch which will be ran per request in development, or before the first request in production.
|
||||
|
||||
h4. +ensure_autoload_once_paths_as_subset+
|
||||
*+add_builtin_route+* If the application is running under the development environment then this will append the route for +rails/info/properties+ to the application routes. This route provides the detailed information such as Rails and Ruby version for +public/index.html+ in a default Rails application.
|
||||
|
||||
Ensures that the +config.autoload_once_paths+ only contains paths from +config.autoload_paths+. If it contains extra paths, then an exception will be raised.
|
||||
*+build_middleware_stack+* Builds the middleware stack for the application, returning an object which has a +call+ method which takes a Rack environment object for the request.
|
||||
|
||||
h4. +add_to_prepare_blocks+
|
||||
*+eager_load!+* If +config.cache_classes+ is +true+, runs the +config.before_eager_load+ hooks and then calls +eager_load!+ which will load all the Ruby files from +config.eager_load_paths+.
|
||||
|
||||
The block for every +config.to_prepare+ call in the application, a railtie or engine is added to the +to_prepare+ callbacks for Action Dispatch which will be ran per request in development, or before the first request in production.
|
||||
*+finisher_hook+* Provides a hook for after the initialization of process of the application is complete, as well as running all the +config.after_initialize+ blocks for the application, railties and engines.
|
||||
|
||||
h4. +add_builtin_route+
|
||||
*+set_routes_reloader+* Configures Action Dispatch to reload the routes file using +ActionDispatch::Callbacks.to_prepare+.
|
||||
|
||||
If the application is running under the development environment then this will append the route for +rails/info/properties+ to the application routes. This route provides the detailed information such as Rails and Ruby version for +public/index.html+ in a default Rails application.
|
||||
|
||||
h4. +build_middleware_stack+
|
||||
|
||||
Builds the middleware stack for the application, returning an object which has a +call+ method which takes a Rack environment object for the request.
|
||||
|
||||
h4. +eager_load!+
|
||||
|
||||
If +config.cache_classes+ is +true+, runs the +config.before_eager_load+ hooks and then calls +eager_load!+ which will load all the Ruby files from +config.eager_load_paths+.
|
||||
|
||||
h4. +finisher_hook+
|
||||
|
||||
Provides a hook for after the initialization of process of the application is complete, as well as running all the +config.after_initialize+ blocks for the application, railties and engines.
|
||||
|
||||
h4. +set_routes_reloader+
|
||||
|
||||
Configures Action Dispatch to reload the routes file using +ActionDispatch::Callbacks.to_prepare+.
|
||||
|
||||
h4. +disable_dependency_loading+
|
||||
*+disable_dependency_loading+*
|
||||
|
||||
|
||||
h3. Changelog
|
||||
|
|
Loading…
Reference in a new issue