Skip to content

Commit 811f285

Browse files
Guides: Update serialization section [ci skip]
The current guidance on how to initialize a custom serializer does not work. Instead, it raises a `NameError`. I found I needed to use the `to_prepare` [initialization events][] to correctly configure a custom serializer: Additionally, add the private `klass` instance method to match the example in the API. [initialization events]: https://guides.rubyonrails.org/configuring.html#initialization-events
1 parent be4bd3d commit 811f285

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

guides/source/active_job_basics.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,6 @@ You can extend the list of supported argument types. You just need to define you
600600
```ruby
601601
# app/serializers/money_serializer.rb
602602
class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
603-
# Checks if an argument should be serialized by this serializer.
604-
def serialize?(argument)
605-
argument.is_a? Money
606-
end
607-
608603
# Converts an object to a simpler representative using supported object types.
609604
# The recommended representative is a Hash with a specific key. Keys can be of basic types only.
610605
# You should call `super` to add the custom serializer type to the hash.
@@ -619,6 +614,12 @@ class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
619614
def deserialize(hash)
620615
Money.new(hash["amount"], hash["currency"])
621616
end
617+
618+
private
619+
# Checks if an argument should be serialized by this serializer.
620+
def klass
621+
Money
622+
end
622623
end
623624
```
624625

@@ -636,7 +637,7 @@ to set-up serializers to be loaded only once, e.g. by amending `config/applicati
636637
# config/application.rb
637638
module YourApp
638639
class Application < Rails::Application
639-
config.autoload_once_paths << Rails.root.join('app', 'serializers')
640+
config.autoload_once_paths << "#{root}/app/serializers"
640641
end
641642
end
642643
```

0 commit comments

Comments
 (0)