diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2005-06-17 13:42:23 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2005-06-17 13:42:23 +0000 |
commit | 959707bfb62108fe9924ed4fefd6f3458972493a (patch) | |
tree | d82c5451a3921d4530910f67ed68cfc3887fb6c1 /actionpack | |
parent | d808cd4582d407a7b0dae93ee94da6fbc8cd0c2b (diff) | |
download | rails-959707bfb62108fe9924ed4fefd6f3458972493a.tar.gz rails-959707bfb62108fe9924ed4fefd6f3458972493a.tar.bz2 rails-959707bfb62108fe9924ed4fefd6f3458972493a.zip |
r1335@iwill: jeremy | 2005-06-17 11:41:50 -0700
Ticket 1458 - distance_of_time_in_words
r1336@iwill: jeremy | 2005-06-17 11:44:50 -0700
Update changelog
r1337@iwill: jeremy | 2005-06-17 11:45:44 -0700
Applied patch.
r1361@iwill: jeremy | 2005-06-17 11:48:02 -0700
Merge changelog
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1449 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG | 4 | ||||
-rwxr-xr-x | actionpack/lib/action_view/helpers/date_helper.rb | 19 | ||||
-rwxr-xr-x | actionpack/test/template/date_helper_test.rb | 9 |
3 files changed, 25 insertions, 7 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 1be2ac66ef..d06a89af90 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,9 @@ *SVN* +* Correct distance_of_time_in_words for integer arguments and make the second ar +g optional, treating the first arg as a duration in seconds. #1458 [madrobby <t +homas@fesch.at>] + * Fixed query parser to deal gracefully with equal signs inside keys and values #1345 [gorou]. Example: /?sig=abcdef=:foobar=&x=y will pass now. diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 6b1fbf7268..214f9b29d9 100755 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -13,14 +13,19 @@ module ActionView module DateHelper DEFAULT_PREFIX = "date" unless const_defined?("DEFAULT_PREFIX") - # Reports the approximate distance in time between to Time objects. For example, if the distance is 47 minutes, it'll return + # Reports the approximate distance in time between to Time objects or integers. + # For example, if the distance is 47 minutes, it'll return # "about 1 hour". See the source for the complete wording list. - #Set <tt>include_seconds</tt> to true if you want more detailed approximations if distance < 1 minute - def distance_of_time_in_words(from_time, to_time, include_seconds = false) - from_time = from_time.to_time - to_time = to_time.to_time - distance_in_minutes = ((to_time - from_time) / 60).round.abs - distance_in_seconds = ((to_time - from_time)).round.abs + # + # Integers are interpreted as seconds. So, + # <tt>distance_of_time_in_words(50)</tt> returns "less than a minute". + # + # Set <tt>include_seconds</tt> to true if you want more detailed approximations if distance < 1 minute + def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false) + from_time = from_time.to_time if from_time.respond_to?(:to_time) + to_time = to_time.to_time if to_time.respond_to?(:to_time) + distance_in_minutes = (((to_time - from_time).abs)/60).round + distance_in_seconds = ((to_time - from_time).abs).round case distance_in_minutes when 0..1 diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 7ef45d2b1c..2bd3dccf8c 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -30,6 +30,15 @@ class DateHelperTest < Test::Unit::TestCase # test to < from assert_equal "about 4 hours", distance_of_time_in_words(Time.mktime(2004, 3, 7, 1, 20), from) assert_equal "less than 20 seconds", distance_of_time_in_words(Time.mktime(2004, 3, 6, 21, 41, 38), from, true) + + # test with integers + assert_equal "less than a minute", distance_of_time_in_words(50) + assert_equal "about 1 hour", distance_of_time_in_words(60*60) + + # more cumbersome test with integers + assert_equal "less than a minute", distance_of_time_in_words(0, 50) + assert_equal "about 1 hour", distance_of_time_in_words(60*60, 0) + end def test_distance_in_words_date |