From 11aaf2f650a7f7c3bb87127a1dbbb418450fb340 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 20 Mar 2022 23:05:19 +0100 Subject: [PATCH] Adds a section about the undocumented before_remove_const in the Classic to Zeitwerk HOWTO --- guides/source/classic_to_zeitwerk_howto.md | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/guides/source/classic_to_zeitwerk_howto.md b/guides/source/classic_to_zeitwerk_howto.md index 0b45fb0067b42..b0be70795afbb 100644 --- a/guides/source/classic_to_zeitwerk_howto.md +++ b/guides/source/classic_to_zeitwerk_howto.md @@ -344,6 +344,31 @@ config.to_prepare do end ``` +### `before_remove_const` + +Rails 3.1 added support for a callback called `before_remove_const` that was invoked if a class or module responded to this method and was about to be reloaded. This callback has remained otherwise undocumented and it is unlikely that your code uses it. + +However, in case it does, you can rewrite something like + +```ruby +class Country < ActiveRecord::Base + def self.before_remove_const + expire_redis_cache + end +end +``` + +as + +```ruby +# config/initializers/country.rb +unless Rails.application.config.cache_classes + Rails.autoloaders.main.on_unload("Country") do |klass, _abspath| + klass.expire_redis_cache + end +end +``` + ### Spring and the `test` Environment Spring reloads the application code if something changes. In the `test` environment you need to enable reloading for that to work: