From 22668023f8190df4bdcf687054b3888b0761049e Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Thu, 14 Apr 2022 11:42:59 -0500 Subject: [PATCH] Merge pull request #44893 from ghousemohamed/add-docs-for-run-load-hooks Add API docs for `run_load_hooks` [ci-skip] (cherry picked from commit cdabe88d98848779647d7fdddea0c97531d0c32e) --- .../lib/active_support/lazy_load_hooks.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/lazy_load_hooks.rb b/activesupport/lib/active_support/lazy_load_hooks.rb index c6f7ccf0a2802..94f25651b521d 100644 --- a/activesupport/lib/active_support/lazy_load_hooks.rb +++ b/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 ActiveRecord::Base at boot time purely to apply # configuration. Instead a hook is registered that applies configuration once # ActiveRecord::Base is loaded. Here ActiveRecord::Base 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. # # initializer 'active_record.initialize_timezone' do # ActiveSupport.on_load(:active_record) do @@ -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 @@ -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|