aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-01-01 01:03:16 +0900
committerGitHub <noreply@github.com>2018-01-01 01:03:16 +0900
commit540c60ff4723670bf106c2ae46e3a6723cbf42a4 (patch)
tree48c1621337d848201054f1b5d647d9fe76f1e43e /activesupport
parent316d87e412e1c6bc9c7a590b866befbb6bab167f (diff)
parent041b2052e6d5d715474e34f3625f88f586941bfd (diff)
downloadrails-540c60ff4723670bf106c2ae46e3a6723cbf42a4.tar.gz
rails-540c60ff4723670bf106c2ae46e3a6723cbf42a4.tar.bz2
rails-540c60ff4723670bf106c2ae46e3a6723cbf42a4.zip
Merge pull request #31310 from kinnrot/duration-modulo
Duration created with no parts will have a default seconds part eqaul to 0
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/duration.rb4
-rw-r--r--activesupport/test/core_ext/duration_test.rb2
2 files changed, 5 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb
index 1af3411a8a..fe1058762b 100644
--- a/activesupport/lib/active_support/duration.rb
+++ b/activesupport/lib/active_support/duration.rb
@@ -194,7 +194,6 @@ module ActiveSupport
end
parts[:seconds] = remainder
- parts.reject! { |k, v| v.zero? }
new(value, parts)
end
@@ -211,6 +210,7 @@ module ActiveSupport
def initialize(value, parts) #:nodoc:
@value, @parts = value, parts.to_h
@parts.default = 0
+ @parts.reject! { |k, v| v.zero? }
end
def coerce(other) #:nodoc:
@@ -370,6 +370,8 @@ module ActiveSupport
alias :before :ago
def inspect #:nodoc:
+ return "0 seconds" if parts.empty?
+
parts.
reduce(::Hash.new(0)) { |h, (l, r)| h[l] += r; h }.
sort_by { |unit, _ | PARTS.index(unit) }.
diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb
index dbee543644..4a02f27def 100644
--- a/activesupport/test/core_ext/duration_test.rb
+++ b/activesupport/test/core_ext/duration_test.rb
@@ -71,6 +71,8 @@ class DurationTest < ActiveSupport::TestCase
assert_equal "7 days", 7.days.inspect
assert_equal "1 week", 1.week.inspect
assert_equal "2 weeks", 1.fortnight.inspect
+ assert_equal "0 seconds", (10 % 5.seconds).inspect
+ assert_equal "10 minutes", (10.minutes + 0.seconds).inspect
end
def test_inspect_locale