Skip to content

Commit

Permalink
Add years counter to humanize helper
Browse files Browse the repository at this point in the history
  • Loading branch information
potomak committed Nov 29, 2011
1 parent 39dc768 commit fc25f43
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Expand Up @@ -11,7 +11,7 @@ def relative_day(date)
end

def humanize(secs)
[[60, "second"], [60, "minute"], [24, "hour"], [1000, "day"]].map do |count, name|
[[60, "second"], [60, "minute"], [24, "hour"], [365, "day"], [1000, "year"]].map do |count, name|
if secs > 0
secs, n = secs.divmod(count)
pluralize(n.to_i, name) if n.to_i > 0
Expand Down
5 changes: 4 additions & 1 deletion test/unit/helpers/application_helper_test.rb
Expand Up @@ -22,7 +22,10 @@ class ApplicationHelperTest < ActionView::TestCase
[
{:secs => 30, :string => '30 seconds'},
{:secs => 60 + 1, :string => '1 minute, 1 second'},
{:secs => 60*2 + 1, :string => '2 minutes, 1 second'}
{:secs => 60*2 + 1, :string => '2 minutes, 1 second'},
{:secs => 60*60*2 + 60*2 + 1, :string => '2 hours, 2 minutes, 1 second'},
{:secs => 24*60*60*2 + 60*60*2 + 60*2 + 1, :string => '2 days, 2 hours, 2 minutes, 1 second'},
{:secs => 365*24*60*60*2 + 24*60*60*2 + 60*60*2 + 60*2 + 1, :string => '2 years, 2 days, 2 hours, 2 minutes, 1 second'}
].each do |data|
test "humanize #{data[:secs]} secs returns '#{data[:string]}'" do
assert humanize(data[:secs]) == data[:string]
Expand Down

0 comments on commit fc25f43

Please sign in to comment.