2018-07-19 14:43:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-15 05:36:39 -05:00
|
|
|
module EntityDateHelper
|
|
|
|
include ActionView::Helpers::DateHelper
|
2018-04-19 10:43:20 -04:00
|
|
|
include ActionView::Helpers::TagHelper
|
2016-11-15 05:36:39 -05:00
|
|
|
|
|
|
|
def interval_in_words(diff)
|
2016-11-25 05:22:44 -05:00
|
|
|
return 'Not started' unless diff
|
2016-11-24 06:38:54 -05:00
|
|
|
|
2017-06-01 23:43:19 -04:00
|
|
|
distance_of_time_in_words(Time.now, diff, scope: 'datetime.time_ago_in_words')
|
2016-11-15 05:36:39 -05:00
|
|
|
end
|
2016-11-16 09:55:20 -05:00
|
|
|
|
|
|
|
# Converts seconds into a hash such as:
|
|
|
|
# { days: 1, hours: 3, mins: 42, seconds: 40 }
|
|
|
|
#
|
|
|
|
# It returns 0 seconds for zero or negative numbers
|
|
|
|
# It rounds to nearest time unit and does not return zero
|
|
|
|
# i.e { min: 1 } instead of { mins: 1, seconds: 0 }
|
|
|
|
def distance_of_time_as_hash(diff)
|
|
|
|
diff = diff.abs.floor
|
|
|
|
|
|
|
|
return { seconds: 0 } if diff == 0
|
|
|
|
|
|
|
|
mins = (diff / 60).floor
|
|
|
|
seconds = diff % 60
|
|
|
|
hours = (mins / 60).floor
|
|
|
|
mins = mins % 60
|
|
|
|
days = (hours / 24).floor
|
|
|
|
hours = hours % 24
|
|
|
|
|
|
|
|
duration_hash = {}
|
|
|
|
|
|
|
|
duration_hash[:days] = days if days > 0
|
|
|
|
duration_hash[:hours] = hours if hours > 0
|
|
|
|
duration_hash[:mins] = mins if mins > 0
|
|
|
|
duration_hash[:seconds] = seconds if seconds > 0
|
|
|
|
|
|
|
|
duration_hash
|
|
|
|
end
|
2018-04-19 10:43:20 -04:00
|
|
|
|
|
|
|
# Generates an HTML-formatted string for remaining dates based on start_date and due_date
|
|
|
|
#
|
|
|
|
# It returns "Past due" for expired entities
|
|
|
|
# It returns "Upcoming" for upcoming entities
|
|
|
|
# If due date is provided, it returns "# days|weeks|months remaining|ago"
|
|
|
|
# If start date is provided and elapsed, with no due date, it returns "# days elapsed"
|
2018-11-29 23:03:35 -05:00
|
|
|
def remaining_days_in_words(due_date, start_date = nil)
|
|
|
|
if due_date&.past?
|
2019-08-22 07:29:40 -04:00
|
|
|
content_tag(:strong, _('Past due'))
|
|
|
|
elsif due_date&.today?
|
|
|
|
content_tag(:strong, _('Today'))
|
2018-11-29 23:03:35 -05:00
|
|
|
elsif start_date&.future?
|
2019-08-22 07:29:40 -04:00
|
|
|
content_tag(:strong, _('Upcoming'))
|
2018-11-27 11:53:16 -05:00
|
|
|
elsif due_date
|
|
|
|
is_upcoming = (due_date - Date.today).to_i > 0
|
2019-08-22 07:29:40 -04:00
|
|
|
time_ago = distance_of_time_in_words(due_date, Date.today)
|
2018-07-19 14:43:13 -04:00
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
# https://gitlab.com/gitlab-org/gitlab-foss/issues/49440
|
2018-07-19 14:43:13 -04:00
|
|
|
#
|
|
|
|
# Need to improve the i18n here and do a full translation
|
|
|
|
# of the string instead of piecewise translations.
|
|
|
|
content = time_ago
|
|
|
|
.gsub(/\d+/) { |match| "<strong>#{match}</strong>" }
|
2019-08-22 07:29:40 -04:00
|
|
|
.remove('about ')
|
|
|
|
remaining_or_ago = is_upcoming ? _('remaining') : _('ago')
|
2018-07-19 14:43:13 -04:00
|
|
|
|
|
|
|
"#{content} #{remaining_or_ago}".html_safe
|
2018-11-29 23:03:35 -05:00
|
|
|
elsif start_date&.past?
|
2018-11-27 11:53:16 -05:00
|
|
|
days = (Date.today - start_date).to_i
|
2018-07-19 14:43:13 -04:00
|
|
|
"#{content_tag(:strong, days)} #{'day'.pluralize(days)} elapsed".html_safe
|
2018-04-19 10:43:20 -04:00
|
|
|
end
|
|
|
|
end
|
2016-11-16 06:01:10 -05:00
|
|
|
end
|