public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Added include_seconds option as the third parameter to 
distance_of_time_in_words which will render "less than a minute" 
in higher resolution ("less than 10 seconds" etc) #944 
[thomas@fesch.at]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1010 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
dhh (author)
Sat Mar 26 14:00:23 -0800 2005
commit  520dae295bbd2e406739048b5d43e5e4cd1342d5
tree    682211831b46727358bc1685837ab4aa0cc3059b
parent  9fb6a54a164e7a1b64cf36efcba657c4215858b8
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Added include_seconds option as the third parameter to distance_of_time_in_words which will render "less than a minute" in higher resolution ("less than 10 seconds" etc) #944 [thomas@fesch.at]
0
+
0
 * Added fourth option to process in test cases to specify the content of the flash #949 [Jamis Buck]
0
 
0
 * Added Verifications that allows you to specify preconditions to actions in form of statements like <tt>verify :only => :update_post, :params => "admin_privileges", :redirect_to => { :action => "settings" }</tt>, which ensure that the update_post action is only called if admin_privileges is available as a parameter -- otherwise the user is redirected to settings. #897 [Jamis Buck]
...
15
16
17
18
 
19
20
21
...
15
16
17
 
18
19
20
21
0
@@ -15,7 +15,7 @@
0
   # class GlobalController < ActionController::Base
0
   # # prevent the #update_settings action from being invoked unless
0
   # # the 'admin_privileges' request parameter exists.
0
- # verify :params => "admin_privileges", :only => :update_post
0
+ # verify :params => "admin_privileges", :only => :update_post,
0
   # :redirect_to => { :action => "settings" }
0
   #
0
   # # disallow a post from being updated if there was no information
...
15
16
17
18
19
 
 
 
 
20
21
22
23
 
 
 
 
 
 
 
 
 
 
 
24
25
26
27
...
28
29
30
31
 
32
33
34
 
 
35
36
37
...
15
16
17
 
 
18
19
20
21
22
23
 
 
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
...
39
40
41
 
42
43
 
 
44
45
46
47
48
0
@@ -15,12 +15,23 @@
0
 
0
       # Reports the approximate distance in time between to Time objects. For example, if the distance is 47 minutes, it'll return
0
       # "about 1 hour". See the source for the complete wording list.
0
- def distance_of_time_in_words(from_time, to_time)
0
- distance_in_minutes = ((to_time - from_time) / 60).round
0
+ #Set <tt>include_seconds</tt> to true if you want more detailed approximations if distance < 1 minute
0
+ def distance_of_time_in_words(from_time, to_time, include_seconds = false)
0
+ distance_in_minutes = ((to_time - from_time) / 60).round.abs
0
+ distance_in_seconds = ((to_time - from_time)).round.abs
0
 
0
         case distance_in_minutes
0
- when 0 then "less than a minute"
0
- when 1 then "1 minute"
0
+ when 0..1
0
+ return (distance_in_minutes==0) ? "less than a minute" : "1 minute" unless include_seconds
0
+ case distance_in_seconds
0
+ when 0..5 then "less than 5 seconds"
0
+ when 6..10 then "less than 10 seconds"
0
+ when 11..20 then "less than 20 seconds"
0
+ when 21..40 then "half a minute"
0
+ when 41..59 then "less than a minute"
0
+ else "1 minute"
0
+ end
0
+
0
           when 2..45 then "#{distance_in_minutes} minutes"
0
           when 46..90 then "about 1 hour"
0
           when 90..1440 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
0
0
@@ -28,10 +39,10 @@
0
           else "#{(distance_in_minutes / 1440).round} days"
0
         end
0
       end
0
-
0
+
0
       # Like distance_of_time_in_words, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.
0
- def distance_of_time_in_words_to_now(from_time)
0
- distance_of_time_in_words(from_time, Time.now)
0
+ def distance_of_time_in_words_to_now(from_time, include_seconds = false)
0
+ distance_of_time_in_words(from_time, Time.now, include_seconds)
0
       end
0
 
0
       # Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute (identified by
...
13
14
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
17
18
...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
0
@@ -13,6 +13,22 @@
0
     assert_equal "about 3 hours", distance_of_time_in_words(from, Time.mktime(2004, 3, 7, 0, 41))
0
     assert_equal "about 4 hours", distance_of_time_in_words(from, Time.mktime(2004, 3, 7, 1, 20))
0
     assert_equal "2 days", distance_of_time_in_words(from, Time.mktime(2004, 3, 9, 15, 40))
0
+
0
+ # include seconds
0
+ assert_equal "less than a minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 19), false)
0
+ assert_equal "less than 5 seconds", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 19), true)
0
+ assert_equal "less than 10 seconds", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 28), true)
0
+ assert_equal "less than 20 seconds", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 38), true)
0
+ assert_equal "half a minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 48), true)
0
+ assert_equal "less than a minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 42, 17), true)
0
+
0
+ assert_equal "1 minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 42, 18), true)
0
+ assert_equal "1 minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 42, 28), true)
0
+ assert_equal "2 minutes", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 42, 48), true)
0
+
0
+ # test to < from
0
+ assert_equal "about 4 hours", distance_of_time_in_words(Time.mktime(2004, 3, 7, 1, 20), from)
0
+ assert_equal "less than 20 seconds", distance_of_time_in_words(Time.mktime(2004, 3, 6, 21, 41, 38), from, true)
0
   end
0
 
0
   def test_select_day
...
1
2
 
3
4
5
...
52
53
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
56
57
...
72
73
74
 
75
...
1
2
3
4
5
6
...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
...
89
90
91
92
93
0
@@ -1,5 +1,6 @@
0
 require 'test/unit'
0
 require File.dirname(__FILE__) + '/../../lib/action_view/helpers/text_helper'
0
+require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/core_ext/numeric' # for human_size
0
 
0
 class TextHelperTest < Test::Unit::TestCase
0
   include ActionView::Helpers::TextHelper
0
@@ -52,6 +53,22 @@
0
       highlight("This is a beautiful? morning", "beautiful? morning")
0
     )
0
   end
0
+
0
+ def test_human_size
0
+ assert_equal("0 Bytes", human_size(0))
0
+ assert_equal("3 Bytes", human_size(3.14159265))
0
+ assert_equal("123 Bytes", human_size(123.0))
0
+ assert_equal("123 Bytes", human_size(123))
0
+ assert_equal("1.2 KB", human_size(1234))
0
+ assert_equal("12.1 KB", human_size(12345))
0
+ assert_equal("1.2 MB", human_size(1234567))
0
+ assert_equal("1.1 GB", human_size(1234567890))
0
+ assert_equal("1.1 TB", human_size(1234567890123))
0
+ assert_equal("444.0 KB", human_size(444.kilobytes))
0
+ assert_equal("1023.0 MB", human_size(1023.megabytes))
0
+ assert_equal("3.0 TB", human_size(3.terabytes))
0
+ assert_nil human_size('x')
0
+ end
0
 
0
   def test_excerpt
0
     assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5))
0
@@ -72,5 +89,6 @@
0
     assert_equal %(Go to http://www.rubyonrails.com), auto_link("Go to http://www.rubyonrails.com", :email_addresses)
0
     assert_equal %(Go to <a href="http://www.rubyonrails.com">http://www.rubyonrails.com</a> and say hello to <a href="mailto:david@loudthinking.com">david@loudthinking.com</a>), auto_link("Go to http://www.rubyonrails.com and say hello to david@loudthinking.com")
0
   end
0
+
0
 end

Comments

    No one has commented yet.