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

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
This commit is contained in:
David Heinemeier Hansson 2005-03-26 22:00:23 +00:00
parent 9fb6a54a16
commit 520dae295b
5 changed files with 55 additions and 8 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* 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]
* Added fourth option to process in test cases to specify the content of the flash #949 [Jamis Buck]
* 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]

View file

@ -15,7 +15,7 @@ module ActionController #:nodoc:
# class GlobalController < ActionController::Base
# # prevent the #update_settings action from being invoked unless
# # the 'admin_privileges' request parameter exists.
# verify :params => "admin_privileges", :only => :update_post
# verify :params => "admin_privileges", :only => :update_post,
# :redirect_to => { :action => "settings" }
#
# # disallow a post from being updated if there was no information

View file

@ -15,12 +15,23 @@ module ActionView
# Reports the approximate distance in time between to Time objects. For example, if the distance is 47 minutes, it'll return
# "about 1 hour". See the source for the complete wording list.
def distance_of_time_in_words(from_time, to_time)
distance_in_minutes = ((to_time - from_time) / 60).round
#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)
distance_in_minutes = ((to_time - from_time) / 60).round.abs
distance_in_seconds = ((to_time - from_time)).round.abs
case distance_in_minutes
when 0 then "less than a minute"
when 1 then "1 minute"
when 0..1
return (distance_in_minutes==0) ? "less than a minute" : "1 minute" unless include_seconds
case distance_in_seconds
when 0..5 then "less than 5 seconds"
when 6..10 then "less than 10 seconds"
when 11..20 then "less than 20 seconds"
when 21..40 then "half a minute"
when 41..59 then "less than a minute"
else "1 minute"
end
when 2..45 then "#{distance_in_minutes} minutes"
when 46..90 then "about 1 hour"
when 90..1440 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
@ -28,10 +39,10 @@ module ActionView
else "#{(distance_in_minutes / 1440).round} days"
end
end
# Like distance_of_time_in_words, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.
def distance_of_time_in_words_to_now(from_time)
distance_of_time_in_words(from_time, Time.now)
def distance_of_time_in_words_to_now(from_time, include_seconds = false)
distance_of_time_in_words(from_time, Time.now, include_seconds)
end
# Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute (identified by

View file

@ -13,6 +13,22 @@ class DateHelperTest < Test::Unit::TestCase
assert_equal "about 3 hours", distance_of_time_in_words(from, Time.mktime(2004, 3, 7, 0, 41))
assert_equal "about 4 hours", distance_of_time_in_words(from, Time.mktime(2004, 3, 7, 1, 20))
assert_equal "2 days", distance_of_time_in_words(from, Time.mktime(2004, 3, 9, 15, 40))
# include seconds
assert_equal "less than a minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 19), false)
assert_equal "less than 5 seconds", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 19), true)
assert_equal "less than 10 seconds", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 28), true)
assert_equal "less than 20 seconds", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 38), true)
assert_equal "half a minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 48), true)
assert_equal "less than a minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 42, 17), true)
assert_equal "1 minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 42, 18), true)
assert_equal "1 minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 42, 28), true)
assert_equal "2 minutes", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 42, 48), true)
# 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)
end
def test_select_day

View file

@ -1,5 +1,6 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../../lib/action_view/helpers/text_helper'
require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/core_ext/numeric' # for human_size
class TextHelperTest < Test::Unit::TestCase
include ActionView::Helpers::TextHelper
@ -52,6 +53,22 @@ class TextHelperTest < Test::Unit::TestCase
highlight("This is a beautiful? morning", "beautiful? morning")
)
end
def test_human_size
assert_equal("0 Bytes", human_size(0))
assert_equal("3 Bytes", human_size(3.14159265))
assert_equal("123 Bytes", human_size(123.0))
assert_equal("123 Bytes", human_size(123))
assert_equal("1.2 KB", human_size(1234))
assert_equal("12.1 KB", human_size(12345))
assert_equal("1.2 MB", human_size(1234567))
assert_equal("1.1 GB", human_size(1234567890))
assert_equal("1.1 TB", human_size(1234567890123))
assert_equal("444.0 KB", human_size(444.kilobytes))
assert_equal("1023.0 MB", human_size(1023.megabytes))
assert_equal("3.0 TB", human_size(3.terabytes))
assert_nil human_size('x')
end
def test_excerpt
assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5))
@ -72,4 +89,5 @@ class TextHelperTest < Test::Unit::TestCase
assert_equal %(Go to http://www.rubyonrails.com), auto_link("Go to http://www.rubyonrails.com", :email_addresses)
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")
end
end