File tree Expand file tree Collapse file tree 3 files changed +38
-2
lines changed
lib/active_job/serializers Expand file tree Collapse file tree 3 files changed +38
-2
lines changed Original file line number Diff line number Diff line change
1
+ * Preserve the serialized timezone when deserializing ` ActiveSupport::TimeWithZone ` arguments.
2
+
3
+ * Joshua Young*
4
+
1
5
* Remove deprecated ` :exponentially_longer ` value for the ` :wait ` in ` retry_on ` .
2
6
3
7
* Rafael Mendonça França*
Original file line number Diff line number Diff line change 2
2
3
3
module ActiveJob
4
4
module Serializers
5
- class TimeWithZoneSerializer < TimeObjectSerializer # :nodoc:
5
+ class TimeWithZoneSerializer < ObjectSerializer # :nodoc:
6
+ NANO_PRECISION = 9
7
+
8
+ def serialize ( time_with_zone )
9
+ super (
10
+ "value" => time_with_zone . iso8601 ( NANO_PRECISION ) ,
11
+ "time_zone" => time_with_zone . time_zone . tzinfo . name
12
+ )
13
+ end
14
+
6
15
def deserialize ( hash )
7
- Time . iso8601 ( hash [ "value" ] ) . in_time_zone
16
+ Time . iso8601 ( hash [ "value" ] ) . in_time_zone ( hash [ "time_zone" ] || Time . zone )
8
17
end
9
18
10
19
private
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require "helper"
4
+
5
+ class TimeWithZoneSerializerTest < ActiveSupport ::TestCase
6
+ test "#deserialize preserves serialized time zone" do
7
+ time_zone_before = Time . zone
8
+
9
+ Time . zone = "America/Los_Angeles"
10
+ time_with_zone = Time . parse ( "08:00" ) . in_time_zone
11
+
12
+ assert_equal "America/Los_Angeles" , time_with_zone . time_zone . tzinfo . name
13
+
14
+ serialized = ActiveJob ::Serializers ::TimeWithZoneSerializer . serialize ( time_with_zone )
15
+ Time . zone = "Europe/London"
16
+ deserialized = ActiveJob ::Serializers ::TimeWithZoneSerializer . deserialize ( serialized )
17
+
18
+ assert_equal "America/Los_Angeles" , deserialized . time_zone . tzinfo . name
19
+ assert_equal time_with_zone , deserialized
20
+ ensure
21
+ Time . zone = time_zone_before
22
+ end
23
+ end
You can’t perform that action at this time.
0 commit comments