aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-03-26 22:00:23 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-03-26 22:00:23 +0000
commit520dae295bbd2e406739048b5d43e5e4cd1342d5 (patch)
tree682211831b46727358bc1685837ab4aa0cc3059b /actionpack/lib
parent9fb6a54a164e7a1b64cf36efcba657c4215858b8 (diff)
downloadrails-520dae295bbd2e406739048b5d43e5e4cd1342d5.tar.gz
rails-520dae295bbd2e406739048b5d43e5e4cd1342d5.tar.bz2
rails-520dae295bbd2e406739048b5d43e5e4cd1342d5.zip
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
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_controller/verification.rb2
-rwxr-xr-xactionpack/lib/action_view/helpers/date_helper.rb25
2 files changed, 19 insertions, 8 deletions
diff --git a/actionpack/lib/action_controller/verification.rb b/actionpack/lib/action_controller/verification.rb
index 88b2331273..db3ea39168 100644
--- a/actionpack/lib/action_controller/verification.rb
+++ b/actionpack/lib/action_controller/verification.rb
@@ -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
diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb
index 7ec24f2428..adb41330e5 100755
--- a/actionpack/lib/action_view/helpers/date_helper.rb
+++ b/actionpack/lib/action_view/helpers/date_helper.rb
@@ -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