Module: Athar::Dashboard::FormattingHelper
- Included in:
- Athar::DashboardHelper
- Defined in:
- app/helpers/athar/dashboard/formatting_helper.rb
Overview
Compact, terminal-style formatters for time, duration, and large numbers — used in the table rows, sidebar, KPI strip, and topbar.
Instance Method Summary collapse
- #absolute_time(time) ⇒ Object
-
#compact_duration(seconds) ⇒ Object
Format a duration in seconds as “Xd”, “Xy”, etc.
-
#compact_number(number) ⇒ Object
Format a large integer compactly: 1_200_000 → “1.2M”, 150_000 → “150K”.
- #relative_time(time, now: Time.current) ⇒ Object
Instance Method Details
#absolute_time(time) ⇒ Object
18 19 20 |
# File 'app/helpers/athar/dashboard/formatting_helper.rb', line 18 def absolute_time(time) time.utc.strftime("%Y-%m-%d %H:%M:%SZ") end |
#compact_duration(seconds) ⇒ Object
Format a duration in seconds as “Xd”, “Xy”, etc. Used by the sidebar’s retention pill.
37 38 39 40 41 42 43 44 |
# File 'app/helpers/athar/dashboard/formatting_helper.rb', line 37 def compact_duration(seconds) return "—" if seconds.nil? days = seconds / 86_400 return "#{days / 365}y" if days >= 730 "#{days}d" end |
#compact_number(number) ⇒ Object
Format a large integer compactly: 1_200_000 → “1.2M”, 150_000 → “150K”.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/helpers/athar/dashboard/formatting_helper.rb', line 23 def compact_number(number) return number.to_s if number.nil? || number < 1_000 if number >= 1_000_000 formatted = (number / 1_000_000.0).round(1) "#{formatted.to_s.sub(/\.0$/, "")}M" else formatted = (number / 1_000.0).round(1) "#{formatted.to_s.sub(/\.0$/, "")}K" end end |
#relative_time(time, now: Time.current) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'app/helpers/athar/dashboard/formatting_helper.rb', line 8 def relative_time(time, now: Time.current) diff = (now - time).to_i return "#{diff}s ago" if diff < 60 return "#{diff / 60}m ago" if diff < 3_600 return "#{diff / 3_600}h ago" if diff < 86_400 return "#{diff / 86_400}d ago" if diff < 604_800 "#{diff / 604_800}w ago" end |