Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API docs for run_load_hooks [ci-skip] #44893

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions activesupport/lib/active_support/lazy_load_hooks.rb
@@ -1,14 +1,14 @@
# frozen_string_literal: true

module ActiveSupport
# lazy_load_hooks allows Rails to lazily load a lot of components and thus
# LazyLoadHooks allows Rails to lazily load a lot of components and thus
# making the app boot faster. Because of this feature now there is no need to
# require <tt>ActiveRecord::Base</tt> at boot time purely to apply
# configuration. Instead a hook is registered that applies configuration once
# <tt>ActiveRecord::Base</tt> is loaded. Here <tt>ActiveRecord::Base</tt> is
# used as example but this feature can be applied elsewhere too.
#
# Here is an example where +on_load+ method is called to register a hook.
# Here is an example where on_load method is called to register a hook.
ghousemohamed marked this conversation as resolved.
Show resolved Hide resolved
#
# initializer 'active_record.initialize_timezone' do
# ActiveSupport.on_load(:active_record) do
Expand All @@ -18,10 +18,14 @@ module ActiveSupport
# end
#
# When the entirety of +ActiveRecord::Base+ has been
# evaluated then +run_load_hooks+ is invoked. The very last line of
# evaluated then run_load_hooks is invoked. The very last line of
# +ActiveRecord::Base+ is:
#
# ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
#
# run_load_hooks will then execute all the hooks that were registered
# with the on_load method. In the case of the above example, it will
# execute the block of code that is in the +initializer+.
module LazyLoadHooks
def self.extended(base) # :nodoc:
base.class_eval do
Expand All @@ -46,6 +50,13 @@ def on_load(name, options = {}, &block)
@load_hooks[name] << [block, options]
end

# Executes all blocks registered to +name+ via on_load, using +base+ as the
# evaluation context.
#
# ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
#
# In the case of the above example, it will execute all hooks registered
# for +:active_record+ within the class +ActiveRecord::Base+.
def run_load_hooks(name, base = Object)
@loaded[name] << base
@load_hooks[name].each do |hook, options|
Expand Down