aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2018-02-07 15:06:21 -0500
committerGitHub <noreply@github.com>2018-02-07 15:06:21 -0500
commit5ae2ecab6d3365f6f17e3c8cb298dfeeea113774 (patch)
tree504921f5f0fa56f69780cd0dcaea29b32615754c
parentc5d431e469505ec4438f9fd37530dd9c79c8a872 (diff)
parenta467c6bdd5722c09f5a15c0a7c5c40cc99c8be25 (diff)
downloadrails-5ae2ecab6d3365f6f17e3c8cb298dfeeea113774.tar.gz
rails-5ae2ecab6d3365f6f17e3c8cb298dfeeea113774.tar.bz2
rails-5ae2ecab6d3365f6f17e3c8cb298dfeeea113774.zip
Merge pull request #31923 from jdelStrother/duration-deserialization
Fix yaml deserialization of ActiveSupport::Duration
-rw-r--r--activesupport/lib/active_support/duration.rb8
-rw-r--r--activesupport/test/core_ext/duration_test.rb6
2 files changed, 14 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb
index fe1058762b..88897f811e 100644
--- a/activesupport/lib/active_support/duration.rb
+++ b/activesupport/lib/active_support/duration.rb
@@ -383,6 +383,14 @@ module ActiveSupport
to_i
end
+ def init_with(coder) #:nodoc:
+ initialize(coder["value"], coder["parts"])
+ end
+
+ def encode_with(coder) #:nodoc:
+ coder.map = { "value" => @value, "parts" => @parts }
+ end
+
# Build ISO 8601 Duration string for this duration.
# The +precision+ parameter can be used to limit seconds' precision of duration.
def iso8601(precision: nil)
diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb
index 4a02f27def..be33324f25 100644
--- a/activesupport/test/core_ext/duration_test.rb
+++ b/activesupport/test/core_ext/duration_test.rb
@@ -642,6 +642,12 @@ class DurationTest < ActiveSupport::TestCase
assert_equal time + d1, time + d2
end
+ def test_durations_survive_yaml_serialization
+ d1 = YAML.load(YAML.dump(10.minutes))
+ assert_equal 600, d1.to_i
+ assert_equal 660, (d1 + 60).to_i
+ end
+
private
def eastern_time_zone
if Gem.win_platform?