Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Enhance readability of ActionView DateHelper#distance_of_time_in_words #14334

Merged
merged 1 commit into from

3 participants

Kenny Meyer Arthur Nogueira Neves Godfrey Chan
Kenny Meyer

Refactor numerical constants to module constants which give the numbers
a contextual meaning. This commit aims to provide quicker understanding
for part of the implementation of the
DateHelper#distance_of_time_in_words method.

Kenny Meyer kennym Enhance readability of ActionView DateHelper#distance_of_time_in_words
Refactor numerical constants to module constants which give the numbers
a contextual meaning. This commit aims to provide quicker understanding
for part of the implementation of the
DateHelper#distance_of_time_in_words method.
b5dc91d
Arthur Nogueira Neves
Collaborator

I am :+1: with the changes. Someone else, thoughts?

Godfrey Chan chancancode merged commit 740862a into from
Godfrey Chan
Owner

:+1: Thanks!

Kenny Meyer

Awesome! Thanks for getting to this so quick. :smile:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 10, 2014
  1. Kenny Meyer

    Enhance readability of ActionView DateHelper#distance_of_time_in_words

    kennym authored
    Refactor numerical constants to module constants which give the numbers
    a contextual meaning. This commit aims to provide quicker understanding
    for part of the implementation of the
    DateHelper#distance_of_time_in_words method.
This page is out of date. Refresh to see the latest.
Showing with 8 additions and 4 deletions.
  1. +8 −4 actionview/lib/action_view/helpers/date_helper.rb
12 actionview/lib/action_view/helpers/date_helper.rb
View
@@ -19,6 +19,10 @@ module Helpers
# the <tt>select_month</tt> method would use simply "date" (which can be overwritten using <tt>:prefix</tt>) instead
# of \date[month].
module DateHelper
+ MINUTES_IN_YEAR = 525600
+ MINUTES_IN_QUARTER_YEAR = 131400
+ MINUTES_IN_THREE_QUARTERS_YEAR = 394200
+
# Reports the approximate distance in time between two Time, Date or DateTime objects or integers as seconds.
# Pass <tt>include_seconds: true</tt> if you want more detailed approximations when distance < 1 min, 29 secs.
# Distances are reported based on the following table:
@@ -120,11 +124,11 @@ def distance_of_time_in_words(from_time, to_time = 0, options = {})
else
minutes_with_offset = distance_in_minutes
end
- remainder = (minutes_with_offset % 525600)
- distance_in_years = (minutes_with_offset.div 525600)
- if remainder < 131400
+ remainder = (minutes_with_offset % MINUTES_IN_YEAR)
+ distance_in_years = (minutes_with_offset.div MINUTES_IN_YEAR)
+ if remainder < MINUTES_IN_QUARTER_YEAR
locale.t(:about_x_years, :count => distance_in_years)
- elsif remainder < 394200
+ elsif remainder < MINUTES_IN_THREE_QUARTERS_YEAR
locale.t(:over_x_years, :count => distance_in_years)
else
locale.t(:almost_x_years, :count => distance_in_years + 1)
Something went wrong with that request. Please try again.