January 25, 2007 @ 03:02 AM
Understanding the Rails Initialization process: Part 2
Setting up your Environment
In every Rails app there are various things to configure. You may need to set the email address that the exception_notification plugin uses, or maybe you just need custom Inflections. Let’s go over the currently available options at our disposal:
Environment.rb
Use this for general Rails framework stuff, like custom inflections. At this point, your plugins have not been loaded, so don’t use it to access your controllers or models that maybe depending on plugin code. Also, each environment has lets you define environment-specific config options in config/environments/RAILS_ENV.rb. This is how Rails knows not to reload in production mode, or use caching in development mode.
The After Initialization Hook
There is a little known after_initialization task that runs at the very end of the initialization process. You can define this either in the Rails::Initialization block in config/environment.rb, or anywhere in the environment-specific files mentioned above.
1 2 3 4 5 6 7 8 9 |
# config/environments/development.rb config.after_initialize do PaymentProcessor.gateway = :bogus end # config/environments/production.rb config.after_intialize do PaymentProcessor.gateway = :whatever end |
One important fact to remember here is that you can only have one after_initialization block. The precedence order goes config/environment.rb => config/environments/RAILS_ENV.rb => plugins. It’s probably not good practice to define after_initialization blocks in your plugins, since they would clobber any application settings the developer tries to set up. Your best bet is to define this in your environment-specific config files.
Dispatcher Preparation Callbacks
Preparation callbacks are blocks that are executed before the Dispatcher handles any Rails requests. They’re executed just once in production mode and before each request in development mode. One common use I have for them is making sure Liquid uses my filters and drops between development requests. Rails uses one internally to ensure your observers are always loaded as well.
There are currently two ways to set up a Dispatcher preparation callback. The default way is to use Dispatcher directly. If you have access to the Rails::Configuration instance, you can also use config.to_prepare.
1 2 3 4 5 6 7 8 9 10 11 |
Dispatcher.to_prepare do ... end # Use this if you're... # - in the Rails::Initialization block of config/environment.rb # - in one of the environment-specific files in config/environments/#{RAILS_ENV}.rb # - in a plugin's init.rb file. config.to_prepare do ... end |
Note: If you are on the gem release of Rails 1.2.1, config.to_prepare may not work since Dispatcher won’t be required. This has been fixed in edge Rails, and the 1.2 branch, so will work in the next release.
To be concluded
Stay tuned for the final article in this little trilogy, explaining the changes to the plugin initialization process in more detail. As noted in the first comment, Tim Lucas wrote a better article on this: Environments and the Rails initialisation process.

by Chad Humphries on 25 Jan 06:59
Tim Lucas had a nice post on this and config.to_prepare at http://toolmantim.com/article/2006/12/27/environments_and_the_rails_initialisation_process
by rick on 25 Jan 10:52
Neat… I just realized I didn’t even give any examples of adding a preparation callback.
There’s a bug in 1.2 though, config.to_prepare assumes the Dispatcher is already required. In edge, it makes sure to require it before adding the callback.
by Opporbsputs on 06 Oct 02:14
tit big tit huge tit big tit round ass tiny tit tit fuck small tit tit and ass teen tit how big tit work big black tit cum tit perfect tit big tit patrol tit fucking nice tit black tit asian big tit big tit teen big natural tit sexy tit soft tit mature tit fat tit asian tit hot tit tit palace big tit movie saggy tit big tit and ass giant tit natural tit tit flash naked tit big tit porn tit and pussy little tit mature big tit big tit lesbian monster tit flashing tit bouncing tit nude tit rate my tit wife tit massive tit young tit large tit tit sucking lactating tit big tit fucking beach tit tit milk big tit video biggest tit big tit blonde great tit fake tit big tit pic beautiful tit ebony tit tit sex big tit sex old tit milf tit firm tit big tit fuck blonde tit round tit perky tit wet tit big tit bangers anime tit show tit tit job milking tit japanese tit big tit milf big tit blow job lesbian tit big tit hentai gigantic tit indian tit milky tit huge black tit hanging tit hentai tit no tit party tit real tit absolutly big free tit ebony big tit amateur tit suck tit tiny teen tit big tit gallery black lorded tit bbw tit mega tit amateur big tit tit big tit huge tit big tit round ass tiny tit tit fuck small tit tit and ass teen tit how big tit work big black tit cum tit perfect tit big tit patrol tit fucking nice tit black tit asian big tit big tit teen big natural tit sexy tit soft tit mature tit fat tit asian tit hot tit tit palace big tit movie saggy tit big tit and ass giant tit natural tit tit flash naked tit big tit porn tit and pussy little tit mature big tit big tit lesbian monster tit flashing tit bouncing tit nude tit rate my tit wife tit massive tit young tit large tit tit sucking lactating tit big tit fucking beach tit tit milk big tit video biggest tit big tit blonde great tit fake tit big tit pic beautiful tit ebony tit tit sex big tit sex old tit milf tit firm tit big tit fuck blonde tit round tit perky tit wet tit big tit bangers anime tit show tit tit job milking tit japanese tit big tit milf big tit blow job lesbian tit big tit hentai gigantic tit indian tit milky tit huge black tit hanging tit hentai tit no tit party tit real tit absolutly big free tit ebony big tit amateur tit suck tit tiny teen tit big tit gallery black lorded tit bbw tit mega tit amateur big tit