diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-02-09 17:27:39 -0500 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-02-14 13:10:08 -0500 |
commit | 71721dc1c9b769d3c06317122dc88cad4a346580 (patch) | |
tree | 886889f415e9debca51020c364d6f5bcd424b6ce /guides | |
parent | a5f7357a3dff2617ba13a274feb8d8ac2492f26a (diff) | |
download | rails-71721dc1c9b769d3c06317122dc88cad4a346580.tar.gz rails-71721dc1c9b769d3c06317122dc88cad4a346580.tar.bz2 rails-71721dc1c9b769d3c06317122dc88cad4a346580.zip |
Improve documentation on custom serializers
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_job_basics.md | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/guides/source/active_job_basics.md b/guides/source/active_job_basics.md index eea64f9367..0ee522e23d 100644 --- a/guides/source/active_job_basics.md +++ b/guides/source/active_job_basics.md @@ -345,6 +345,12 @@ Supported types for arguments ActiveJob supports the following types of arguments by default: - Basic types (`NilClass`, `String`, `Integer`, `Fixnum`, `Bignum`, `Float`, `BigDecimal`, `TrueClass`, `FalseClass`) + - `Symbol + - `ActiveSupport::Duration` + - `Date` + - `Time` + - `DateTime` + - `ActiveSupport::TimeWithZone` - `Hash`. Keys should be of `String` or `Symbol` type - `ActiveSupport::HashWithIndifferentAccess` - `Array` @@ -382,38 +388,31 @@ by default has been mixed into Active Record classes. You can extend list of supported types for arguments. You just need to define your own serializer. ```ruby -class MySpecialSerializer - class << self - # Check if this object should be serialized using this serializer +class MoneySerializer < ActiveJob::Serializers::ObjectSerializer + # Check if this object should be serialized using this serializer. def serialize?(argument) - argument.is_a? MySpecialValueObject + argument.is_a? Money end # Convert an object to a simpler representative using supported object types. - # The recommended representative is a Hash with a specific key. Keys can be of basic types only + # The recommended representative is a Hash with a specific key. Keys can be of basic types only. + # You should call `super` to add the custom serializer type to the hash def serialize(object) - { - key => ActiveJob::Serializers.serialize(object.value) - 'another_attribute' => ActiveJob::Serializers.serialize(object.another_attribute) - } + super( + "cents" => object.cents, + "currency" => object.currency + ) end - # Check if this serialized value be deserialized using this serializer + # Check if this serialized value be deserialized using this serializer. + # ActiveJob::Serializers::ObjectSerializer#deserialize? already take care of this. def deserialize?(argument) - argument.is_a?(Hash) && argument.keys == [key, 'another_attribute'] + super end # Convert serialized value into a proper object - def deserialize(object) - value = ActiveJob::Serializers.deserialize(object[key]) - another_attribute = ActiveJob::Serializers.deserialize(object['another_attribute']) - MySpecialValueObject.new value, another_attribute - end - - # Define this method if you are using a hash as a representative. - # This key will be added to a list of restricted keys for hashes. Use basic types only - def key - "_aj_custom_dummy_value_object" + def deserialize(hash) + Money.new hash["cents"], hash["currency"] end end end @@ -422,7 +421,7 @@ end And now you just need to add this serializer to a list: ```ruby -ActiveJob::Base.add_serializers(MySpecialSerializer) +Rails.application.config.active_job.custom_serializers << MySpecialSerializer ``` |