Module: DatesHelper

Defined in:
app/helpers/dates_helper.rb

Instance Method Summary collapse

Instance Method Details

#format_duration(seconds) ⇒ String

Format duration in seconds to human-readable format (e.g., "1h 25m 15s")

Parameters:

  • seconds (Integer, nil)

    Duration in seconds

Returns:

  • (String)

    Formatted duration string



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/dates_helper.rb', line 72

def format_duration(seconds)
  return 'N/A' if seconds.nil? || seconds.to_i <= 0

  seconds = seconds.to_i
  hours = seconds / 3600
  minutes = (seconds % 3600) / 60
  secs = seconds % 60

  parts = []
  parts << "#{hours}h" if hours.positive?
  parts << "#{minutes}m" if minutes.positive?
  parts << "#{secs}s" if secs.positive? && hours.zero?

  parts.empty? ? '0s' : parts.join(' ')
end

#render_date(date, strftime_format = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/helpers/dates_helper.rb', line 43

def render_date(date, strftime_format = nil)
  parsed_date = begin
    date.to_date
  rescue StandardError
    nil
  end
  parsed_date ||= begin
    Date.parse(date.to_s)
  rescue StandardError
    nil
  end
  return unless parsed_date

  if strftime_format
    parsed_date.beginning_of_day.try(:strftime, strftime_format)
  else
    parsed_date.beginning_of_day.try(:to_fs, :crm_date_only)
  end
end

#render_datetime(date_or_datetime, skip_hours_at_midnight: true) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/dates_helper.rb', line 2

def render_datetime(date_or_datetime, skip_hours_at_midnight: true)
  datetime = begin
    Time.zone.parse(date_or_datetime)
  rescue StandardError
    nil
  end
  datetime ||= begin
    date_or_datetime.to_datetime
  rescue StandardError
    nil
  end
  return unless datetime

  if datetime.beginning_of_day == datetime && skip_hours_at_midnight
    datetime.try(:to_fs, :crm_date_only)
  else
    datetime.try(:to_fs, :crm_default)
  end
end

#render_time(date_time) ⇒ Object



63
64
65
66
67
# File 'app/helpers/dates_helper.rb', line 63

def render_time(date_time)
  return unless date_time

  date_time.try(:to_fs, :crm_time_only)
end

#smart_render_datetime(date_or_datetime) ⇒ Object

When the date is today just render the time



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/dates_helper.rb', line 23

def smart_render_datetime(date_or_datetime)
  datetime = begin
    Time.zone.parse(date_or_datetime)
  rescue StandardError
    nil
  end
  datetime ||= begin
    date_or_datetime.to_datetime
  rescue StandardError
    nil
  end
  return unless datetime

  if datetime.to_date == Date.current
    "Today at #{datetime.strftime('%I:%M%p')}"
  else
    datetime.try(:to_fs, :crm_default)
  end
end