Skip to content

Commit

Permalink
Adds a section about the undocumented before_remove_const in the Clas…
Browse files Browse the repository at this point in the history
…sic to Zeitwerk HOWTO
  • Loading branch information
fxn committed Mar 20, 2022
1 parent 7f3e249 commit 11aaf2f
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions guides/source/classic_to_zeitwerk_howto.md
Expand Up @@ -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:
Expand Down

0 comments on commit 11aaf2f

Please sign in to comment.