From ee907fc38e7615b9fda23b288828de7af119ea40 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 1 Apr 2022 10:12:13 +0200 Subject: [PATCH] Document a use case for autoloading in after_initialize --- .../autoloading_and_reloading_constants.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/guides/source/autoloading_and_reloading_constants.md b/guides/source/autoloading_and_reloading_constants.md index 376dd4a87f..63cf66c10f 100644 --- a/guides/source/autoloading_and_reloading_constants.md +++ b/guides/source/autoloading_and_reloading_constants.md @@ -235,6 +235,8 @@ Why? Initializers only run once, when the application boots. If you reboot the s ### Use case 1: During boot, load reloadable code +#### Autoload on boot and on each reload + Let's imagine `ApiGateway` is a reloadable class from `app/services` managed by the `main` autoloader and you need to configure its endpoint while the application boots: ```ruby @@ -255,6 +257,21 @@ end NOTE: For historical reasons, this callback may run twice. The code it executes must be idempotent. +#### Autoload on boot only + +Reloadable classes and modules can be autoloaded in `after_initialize` blocks too. These run on boot, but do not run again on reload. In some exceptional cases this may be what you want. + +Preflight checks are a use case for this: + +```ruby +# config/initializers/check_admin_presence.rb +Rails.application.config.after_initialize do + unless Role.where(name: "admin").exists? + abort "The admin role is not present, please seed the database." + end +end +``` + ### Use case 2: During boot, load code that remains cached Some configurations take a class or module object, and they store it in a place that is not reloaded.