-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add relative date formatting for date/datetime columns
- Loading branch information
1 parent
2e16551
commit 9955b83
Showing
5 changed files
with
165 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,26 @@ | ||
# 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 | ||
# Returns a string representing the number of days ago or from now. | ||
# If the date is not 'recent' returns nil. | ||
def days_ago_in_words(value) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Koi::DateHelper do | ||
describe "#days_ago_in_words" do | ||
it "renders today" do | ||
expect(helper.days_ago_in_words(Date.current)).to eq("today") | ||
end | ||
|
||
it "renders tomorrow" do | ||
expect(helper.days_ago_in_words(Date.tomorrow)).to eq("tomorrow") | ||
end | ||
|
||
it "renders two days from now" do | ||
expect(helper.days_ago_in_words(Date.current + 2.days)).to eq("2 days from now") | ||
end | ||
|
||
it "renders two days ago" do | ||
expect(helper.days_ago_in_words(Date.current - 2.days)).to eq("2 days ago") | ||
end | ||
|
||
it "returns nil" do | ||
expect(helper.days_ago_in_words(Date.current - 6.days)).to be_nil | ||
end | ||
end | ||
end |