Extend example code for autoloading serializers

Performing autoloading during initialization is not supported by Rails and will result
in a detailed deprecation warning. Following the proposed fix in the deprecation warning
will not work with the given piece of code however.

According to discussions in https://github.com/rails/rails/issues/37690 and
https://github.com/rails/rails/pull/41849, the example code has been amended to
ensure that serializers will only be loaded once.
This commit is contained in:
Jan Sandbrink 2021-04-06 08:58:33 +02:00 committed by Jan Sandbrink
parent 0184a01935
commit 2d9a64ded5

@ -423,6 +423,7 @@ by default has been mixed into Active Record classes.
You can extend the list of supported argument types. You just need to define your own serializer:
```ruby
# app/serializers/money_serializer.rb
class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
# Checks if an argument should be serialized by this serializer.
def serialize?(argument)
@ -449,9 +450,22 @@ end
and add this serializer to the list:
```ruby
# config/initializer/custom_serializers.rb
Rails.application.config.active_job.custom_serializers << MoneySerializer
```
Note that auto-loading reloadable code during initialization is not supported. Thus it is recommended
to set-up serializers to be loaded only once, e.g. by amending `config/application.rb` like this:
```ruby
# config/application.rb
module YourApp
class Application < Rails::Application
config.autoload_once_paths << Rails.root.join('app', 'serializers')
end
end
```
Exceptions
----------