Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sfnelson committed Feb 28, 2024
1 parent a51abab commit 143803e
Showing 1 changed file with 60 additions and 14 deletions.
74 changes: 60 additions & 14 deletions app/components/koi/tables/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,68 @@ def rendered_value

# Formats the value as a date
# @param format [String] date format, defaults to :admin
# @param relative [Boolean] if true, the date will be shown as a relative date
# @param relative [Boolean] if true, the date may be shown as a relative date
class DateComponent < BodyCellComponent
include Koi::Tables::RelativeDateFormatter

def initialize(table, record, attribute, format: :admin, relative: false, **options)
def initialize(table, record, attribute, format: :admin, relative: true, **options)
super(table, record, attribute, **options)

@format = format
@relative = relative
end

def value
super&.to_date
end

def rendered_value
value.present? ? format_value : ""
@relative ? relative_time : absolute_time
end

private

def absolute_time
I18n.l(value, format: @format) if value.present?
end

def format_value
@relative ? days_ago_in_words(value.to_date) : I18n.l(value.to_date, format: @format)
def relative_time
if value.blank?
""
else
distance_from_now&.capitalize || absolute_time
end
end

def default_html_attributes
distance_from_now.present? ? { title: absolute_time } : {}
end

def distance_from_now
from_time = value.to_time
to_time = Date.current.to_time
distance_in_days = ((to_time - from_time) / (24.0 * 60.0 * 60.0)).round

case distance_in_days
when 0
"today"
when 1
"yesterday"
when -1
"tomorrow"
when 2..5
"#{distance_in_days} days ago"
when -2..-5
"#{distance_in_days.abs} days from now"
end
end
end

# Formats the value as a datetime
# @param format [String] datetime format, defaults to :admin
# @param relative [Boolean] if true, the datetime will be shown as a relative date/time
class DatetimeComponent < BodyCellComponent
include Koi::Tables::RelativeDateFormatter
include ActionView::Helpers::DateHelper

def initialize(table, record, attribute, format: :admin, relative: false, **options)
super(table, record, attribute, **options)
Expand All @@ -45,24 +82,33 @@ def initialize(table, record, attribute, format: :admin, relative: false, **opti
@relative = relative
end

def value
super&.to_datetime
end

def rendered_value
value.present? ? format_value : ""
@relative ? relative_time : absolute_time
end

private

def format_value
@relative ? time_or_days_ago_in_words(value.to_datetime) : I18n.l(value.to_datetime, format: @format)
def absolute_time
I18n.l(value, format: @format) if value.present?
end

def time_or_days_ago_in_words(time)
days = ((Time.now - time.to_time) / 86400.0).round
if days > 1
days_ago_in_words(time.to_date)
def relative_time
if value.blank?
""
elsif value > DateTime.current
"#{distance_of_time_in_words(value, DateTime.current)} from now"
else
"#{time_ago_in_words(time)} ago"
"#{distance_of_time_in_words(value, DateTime.current)} ago"
end
end

def default_html_attributes
@relative ? { title: absolute_time } : {}
end
end

# Formats the value as a money value
Expand Down

0 comments on commit 143803e

Please sign in to comment.