1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

updating documentation about lazy hooks in ActiveSupport

This commit is contained in:
Neeraj Singh 2010-07-29 12:52:15 -04:00
parent e42945333b
commit a506d5a524

View file

@ -258,28 +258,23 @@ This file goes on to define some classes that will be automatically loaded using
h4. Lazy Hooks
At the top if the +ActiveSupport::Autoload+ module is the +def self.extended+ method:
<ruby>
def self.extended(base)
base.extend(LazyLoadHooks)
end
</ruby>
This is called when we extend this module into one of our classes or modules, such is the case later on when we call +extend ActiveSupport::LazyLoadHooks+ not only in the +ActiveSupport+ module, but in all of the Railtie modules (+ActiveRecord+ and so on), as well as in a couple of places.
+ActiveSupport::LazyLoadHooks+ is responsible for defining methods used for running hooks that are defined during the initialization process, such as the one defined inside the +active_record.initialize_timezone+ initializer:
<ruby>
initializer "active_record.initialize_timezone" do
ActiveRecord.base_hook do
ActiveSupport.on_load(:active_record) do
self.time_zone_aware_attributes = true
self.default_timezone = :utc
end
end
</ruby>
When the initializer is ran it defines a +base_hook+ for +ActiveRecord+ and will only run it when +run_base_hooks+ is called, which in the case of Active Record, is ran after the entirety of +activerecord/lib/active_record/base.rb+ has been evaluated.
When the initializer runs it invokes method +on_load+ for +ActiveRecord+ and the block passed to it would be called only when +run_load_hooks+ is called.
When the entirety of +activerecord/lib/active_record/base.rb+ has been evaluated then +run_load_hooks+ is invoked. The very last line of +activerecord/lib/active_record/base.rb+ is:
<ruby>
ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
</ruby>
h4. +require 'active_support'+ cont'd.