mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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:
parent
0184a01935
commit
2d9a64ded5
1 changed files with 14 additions and 0 deletions
|
@ -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
|
||||
----------
|
||||
|
||||
|
|
Loading…
Reference in a new issue