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

Empty duration inspect fix

This commit is contained in:
Chen Kinnrot 2017-12-13 10:16:46 +02:00
parent 067fc779c4
commit 041b2052e6
2 changed files with 5 additions and 1 deletions

View file

@ -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) }.

View file

@ -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