aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2018-02-14 14:47:46 -0500
committerGitHub <noreply@github.com>2018-02-14 14:47:46 -0500
commitfa9e791e014a650f5ea6a14b283fed9621fc83e2 (patch)
tree42d892b55c66fd57cc62b7c996b01e30746341c5 /guides
parent57495086f0bb1315ea287f8d6bafad8fd48e18b2 (diff)
parent25a14bf2bde90224debee343ebfbb882c02b9588 (diff)
downloadrails-fa9e791e014a650f5ea6a14b283fed9621fc83e2.tar.gz
rails-fa9e791e014a650f5ea6a14b283fed9621fc83e2.tar.bz2
rails-fa9e791e014a650f5ea6a14b283fed9621fc83e2.zip
Merge pull request #30941 from toptal/introduce-custom-serializers-to-activejob-arguments
Introduce custom serializers to ActiveJob arguments
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_job_basics.md54
-rw-r--r--guides/source/configuring.md2
2 files changed, 54 insertions, 2 deletions
diff --git a/guides/source/active_job_basics.md b/guides/source/active_job_basics.md
index 914ef2c327..92a04c585f 100644
--- a/guides/source/active_job_basics.md
+++ b/guides/source/active_job_basics.md
@@ -339,8 +339,23 @@ UserMailer.welcome(@user).deliver_later # Email will be localized to Esperanto.
```
-GlobalID
---------
+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`
+
+### GlobalID
Active Job supports GlobalID for parameters. This makes it possible to pass live
Active Record objects to your job instead of class/id pairs, which you then have
@@ -368,6 +383,41 @@ end
This works with any class that mixes in `GlobalID::Identification`, which
by default has been mixed into Active Record classes.
+### Serializers
+
+You can extend list of supported types for arguments. You just need to define your own serializer.
+
+```ruby
+class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
+ # Check if this object should be serialized using this serializer.
+ def serialize?(argument)
+ 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.
+ # You should call `super` to add the custom serializer type to the hash
+ def serialize(object)
+ super(
+ "cents" => object.cents,
+ "currency" => object.currency
+ )
+ end
+
+ # Convert serialized value into a proper object
+ def deserialize(hash)
+ Money.new hash["cents"], hash["currency"]
+ end
+ end
+end
+```
+
+And now you just need to add this serializer to a list:
+
+```ruby
+Rails.application.config.active_job.custom_serializers << MySpecialSerializer
+```
+
Exceptions
----------
diff --git a/guides/source/configuring.md b/guides/source/configuring.md
index b0f39e7ab5..fd747c1686 100644
--- a/guides/source/configuring.md
+++ b/guides/source/configuring.md
@@ -741,6 +741,8 @@ There are a few configuration options available in Active Support:
* `config.active_job.logger` accepts a logger conforming to the interface of Log4r or the default Ruby Logger class, which is then used to log information from Active Job. You can retrieve this logger by calling `logger` on either an Active Job class or an Active Job instance. Set to `nil` to disable logging.
+* `config.active_job.custom_serializers` allows to set custom argument serializers. Defaults to `[]`.
+
### Configuring Action Cable
* `config.action_cable.url` accepts a string for the URL for where