Module: DatesHelper
- Defined in:
- app/helpers/dates_helper.rb
Overview
View helper for formatting dates, datetimes, times, and durations.
Instance Method Summary collapse
-
#format_duration(seconds) ⇒ String
Format duration in seconds to human-readable format (e.g., "1h 25m 15s").
-
#render_date(date, strftime_format = nil) ⇒ String?
Renders a date value using the CRM date format or a custom strftime format.
-
#render_datetime(date_or_datetime, skip_hours_at_midnight: true) ⇒ String?
Renders a date or datetime value using CRM formats.
-
#render_time(date_time) ⇒ String?
Renders a time value using the CRM time format.
-
#smart_render_datetime(date_or_datetime) ⇒ String?
Renders a date or datetime, showing only the time when the value is today.
Instance Method Details
#format_duration(seconds) ⇒ String
Format duration in seconds to human-readable format (e.g., "1h 25m 15s")
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/helpers/dates_helper.rb', line 88 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) ⇒ String?
Renders a date value using the CRM date format or a custom strftime format.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/helpers/dates_helper.rb', line 56 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) ⇒ String?
Renders a date or datetime value using CRM formats.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/helpers/dates_helper.rb', line 9 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) ⇒ String?
Renders a time value using the CRM time format.
79 80 81 82 83 |
# File 'app/helpers/dates_helper.rb', line 79 def render_time(date_time) return unless date_time date_time.try(:to_fs, :crm_time_only) end |
#smart_render_datetime(date_or_datetime) ⇒ String?
Renders a date or datetime, showing only the time when the value is today.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/helpers/dates_helper.rb', line 32 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 |