1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add argument serializer TimeWithZoneSerializer

The serializer serializes an instance of `ActiveSupport::TimeWithZone`.
The serializer deserializes value to `ActiveSupport::TimeWithZone` if possible.
This commit is contained in:
bogdanvlviv 2018-02-17 00:15:11 +02:00
parent a713fdae4e
commit d2c094aa50
No known key found for this signature in database
GPG key ID: E4ACD76A6DB6DFDD
3 changed files with 33 additions and 2 deletions

View file

@ -12,9 +12,10 @@ module ActiveJob
autoload :ObjectSerializer
autoload :SymbolSerializer
autoload :DurationSerializer
autoload :DateSerializer
autoload :TimeSerializer
autoload :DateTimeSerializer
autoload :DateSerializer
autoload :TimeWithZoneSerializer
autoload :TimeSerializer
mattr_accessor :_additional_serializers
self._additional_serializers = Set.new
@ -57,6 +58,7 @@ module ActiveJob
DurationSerializer,
DateTimeSerializer,
DateSerializer,
TimeWithZoneSerializer,
TimeSerializer
end
end

View file

@ -0,0 +1,21 @@
# frozen_string_literal: true
module ActiveJob
module Serializers
class TimeWithZoneSerializer < ObjectSerializer # :nodoc:
def serialize(time)
super("value" => time.iso8601)
end
def deserialize(hash)
Time.iso8601(hash["value"]).in_time_zone
end
private
def klass
ActiveSupport::TimeWithZone
end
end
end
end

View file

@ -102,6 +102,14 @@ class ArgumentSerializationTest < ActiveSupport::TestCase
assert_instance_of ActiveSupport::HashWithIndifferentAccess, perform_round_trip([indifferent_access]).first
end
test "should maintain time with zone" do
Time.use_zone "Alaska" do
time_with_zone = Time.new(2002, 10, 31, 2, 2, 2).in_time_zone
assert_instance_of ActiveSupport::TimeWithZone, perform_round_trip([time_with_zone]).first
assert_arguments_unchanged time_with_zone
end
end
test "should disallow non-string/symbol hash keys" do
assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [ { 1 => 2 } ]