Module: DatesHelper
- Defined in:
- app/helpers/dates_helper.rb
Overview
View helper: dates.
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) ⇒ Object
- #render_datetime(date_or_datetime, skip_hours_at_midnight: true) ⇒ Object
- #render_time(date_time) ⇒ Object
-
#smart_render_datetime(date_or_datetime) ⇒ Object
When the date is today just render the time.
Instance Method Details
#format_duration(seconds) ⇒ String
Format duration in seconds to human-readable format (e.g., "1h 25m 15s")
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/helpers/dates_helper.rb', line 74 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/helpers/dates_helper.rb', line 45 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
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/helpers/dates_helper.rb', line 4 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
65 66 67 68 69 |
# File 'app/helpers/dates_helper.rb', line 65 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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/helpers/dates_helper.rb', line 25 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 |