Permalink
Browse files

make #inspect if zero length duration return '0 seconds' instead of e…

…mpty string [#2838 state:resolved]

Signed-off-by: Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>
  • Loading branch information...
1 parent 281c1a8 commit d8fff7d9d5ac4283fcabd7ecf64af5709410424f @levinalex levinalex committed with Yehuda Katz + Carl Lerche Jun 25, 2009
Showing with 5 additions and 2 deletions.
  1. +4 −2 activesupport/lib/active_support/duration.rb
  2. +1 −0 activesupport/test/core_ext/duration_test.rb
View
6 activesupport/lib/active_support/duration.rb
@@ -67,10 +67,12 @@ def ago(time = ::Time.current)
def inspect #:nodoc:
consolidated = parts.inject(::Hash.new(0)) { |h,part| h[part.first] += part.last; h }
- [:years, :months, :days, :minutes, :seconds].map do |length|
+ parts = [:years, :months, :days, :minutes, :seconds].map do |length|
n = consolidated[length]
"#{n} #{n == 1 ? length.to_s.singularize : length.to_s}" if n.nonzero?
- end.compact.to_sentence(:locale => :en)
+ end.compact
+ parts = ["0 seconds"] if parts.empty?
+ parts.to_sentence(:locale => :en)
end
protected
View
1 activesupport/test/core_ext/duration_test.rb
@@ -2,6 +2,7 @@
class DurationTest < ActiveSupport::TestCase
def test_inspect
+ assert_equal '0 seconds', 0.seconds.inspect
assert_equal '1 month', 1.month.inspect
assert_equal '1 month and 1 day', (1.month + 1.day).inspect
assert_equal '6 months and -2 days', (6.months - 2.days).inspect

0 comments on commit d8fff7d

Please sign in to comment.