Skip to content

Commit

Permalink
Add relative date formatting for date/datetime columns
Browse files Browse the repository at this point in the history
  • Loading branch information
hasarindaKI committed Feb 29, 2024
1 parent f2859e1 commit c35da02
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
44 changes: 16 additions & 28 deletions app/helpers/koi/date_helper.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
# frozen_string_literal: true

# rubocop:disable Naming/MethodName
module Koi
module DateHelper
# @deprecated
def date_format(date, format)
date.strftime format.gsub(/yyyy/, "%Y")
.gsub(/yy/, "%y")
.gsub(/Month/, "%B")
.gsub(/M/, "%b")
.gsub(/mm/, "%m")
.gsub(/m/, "%-m")
.gsub(/Day/, "%A")
.gsub(/D/, "%a")
.gsub(/dd/, "%d")
.gsub(/d/, "%-d")
end

# @deprecated
def date_Month_d_yyyy(date)
date.strftime "%B %-d, %Y"
end

# @deprecated
def date_d_Month_yyyy(date)
date.strftime "%-d %B %Y"
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

# @deprecated
def date_d_M_yy(date)
date.strftime "%-d %b %y"
case distance_in_days
when 0
"today"
when 1
"yesterday"
when -1
"tomorrow"
when 2..5
"#{distance_in_days} days ago"
when -5..-2
"#{distance_in_days.abs} days from now"
end
end
end
end
# rubocop:enable Naming/MethodName
50 changes: 49 additions & 1 deletion spec/components/koi/tables/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,71 @@
end

describe Koi::Tables::Body::DateComponent do
let(:record) { create(:post, published_on: 1.month.ago) }

it "renders column" do
component = described_class.new(table, record, :published_on)
rendered = render_inline(component)
expect(rendered).to match_html(<<~HTML)
<td>#{I18n.l(record.published_on, format: :admin)}</td>
HTML
end

context "when not relative" do
let(:record) { create(:post, published_on: Date.current) }

it "renders column" do
component = described_class.new(table, record, :published_on, relative: false)
rendered = render_inline(component)
expect(rendered).to match_html(<<~HTML)
<td>#{I18n.l(record.published_on, format: :admin)}</td>
HTML
end
end

context "when date is within 5 days" do
let(:record) { create(:post, published_on: 2.days.ago) }

it "renders column" do
component = described_class.new(table, record, :published_on)
rendered = render_inline(component)
expect(rendered).to match_html(<<~HTML)
<td title="#{I18n.l(record.published_on, format: :admin)}">2 days ago</td>
HTML
end
end

context "when future date" do
let(:record) { create(:post, published_on: 3.days.from_now) }

it "renders column" do
component = described_class.new(table, record, :published_on)
rendered = render_inline(component)
expect(rendered).to match_html(<<~HTML)
<td title="#{I18n.l(record.published_on, format: :admin)}">3 days from now</td>
HTML
end
end
end

describe Koi::Tables::Body::DatetimeComponent do
it "renders column" do
component = described_class.new(table, record, :created_at)
rendered = render_inline(component)
expect(rendered).to match_html(<<~HTML)
<td>#{I18n.l(record.created_at, format: :admin)}</td>
<td title="#{I18n.l(record.created_at, format: :admin)}">Less than a minute ago</td>
HTML
end

context "when not relative" do
it "renders column" do
component = described_class.new(table, record, :created_at, relative: false)
rendered = render_inline(component)
expect(rendered).to match_html(<<~HTML)
<td>#{I18n.l(record.created_at, format: :admin)}</td>
HTML
end
end
end

describe Koi::Tables::Body::NumberComponent do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<% row.ordinal %>
<% row.link :name %>
<% row.image :image %>
<% row.datetime :created_at %>

0 comments on commit c35da02

Please sign in to comment.