Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize classes and modules with ActiveJob #37159

Merged
merged 1 commit into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions activejob/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Allow `Class` and `Module` instances to be serialized.

*Kevin Deisz*

* Log potential matches in `assert_enqueued_with` and `assert_performed_with`

*Gareth du Plooy*
Expand Down
4 changes: 3 additions & 1 deletion activejob/lib/active_job/serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Serializers # :nodoc:
autoload :DateSerializer
autoload :TimeWithZoneSerializer
autoload :TimeSerializer
autoload :ModuleSerializer

mattr_accessor :_additional_serializers
self._additional_serializers = Set.new
Expand Down Expand Up @@ -58,6 +59,7 @@ def add_serializers(*new_serializers)
DateTimeSerializer,
DateSerializer,
TimeWithZoneSerializer,
TimeSerializer
TimeSerializer,
ModuleSerializer
end
end
20 changes: 20 additions & 0 deletions activejob/lib/active_job/serializers/module_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module ActiveJob
module Serializers
class ModuleSerializer < ObjectSerializer # :nodoc:
def serialize(constant)
super("value" => constant.name)
end

def deserialize(hash)
hash["value"].constantize
end

private
def klass
Module
end
end
end
end
13 changes: 11 additions & 2 deletions activejob/test/cases/argument_serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
require "support/stubs/strong_parameters"

class ArgumentSerializationTest < ActiveSupport::TestCase
module ModuleArgument
class ClassArgument; end
end

class ClassArgument; end

setup do
@person = Person.find("5")
end
Expand All @@ -18,14 +24,17 @@ class ArgumentSerializationTest < ActiveSupport::TestCase
DateTime.new(2001, 2, 3, 4, 5, 6, "+03:00"),
ActiveSupport::TimeWithZone.new(Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone["UTC"]),
[ 1, "a" ],
{ "a" => 1 }
{ "a" => 1 },
ModuleArgument,
ModuleArgument::ClassArgument,
ClassArgument
].each do |arg|
test "serializes #{arg.class} - #{arg} verbatim" do
assert_arguments_unchanged arg
end
end

[ Object.new, self, Person.find("5").to_gid ].each do |arg|
[ Object.new, Person.find("5").to_gid ].each do |arg|
test "does not serialize #{arg.class}" do
assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [ arg ]
Expand Down
2 changes: 2 additions & 0 deletions guides/source/active_job_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ ActiveJob supports the following types of arguments by default:
- `Hash` (Keys should be of `String` or `Symbol` type)
- `ActiveSupport::HashWithIndifferentAccess`
- `Array`
- `Module`
- `Class`

### GlobalID

Expand Down