From 475397e42e9e4f3ca24bbe97a0cd1e4aba013445 Mon Sep 17 00:00:00 2001 From: Levin Alexander Date: Thu, 25 Jun 2009 22:47:27 +0200 Subject: [PATCH] make #inspect if zero length duration return '0 seconds' instead of empty string --- activesupport/lib/active_support/duration.rb | 6 ++++-- activesupport/test/core_ext/duration_test.rb | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index a33586f..713ae1b 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -68,10 +68,12 @@ module ActiveSupport 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 diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 6f16621..42b4f10 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -3,6 +3,7 @@ require 'active_support/time' 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 -- 1.6.0.4