From 955372327883ea248dcd7eb9b44d5ac2ea0a04f5 Mon Sep 17 00:00:00 2001 From: Sam Duncan Date: Fri, 6 Sep 2024 10:54:22 -0500 Subject: [PATCH] [PLAY-1477] Timestamp Kit Rails: Elapsed Time Variant Issue (#3606) **What does this PR do?** A clear and concise description with your runway ticket url. https://runway.powerhrg.com/backlog_items/PLAY-1477 Creating private method in the PbTimestamp module to replace the "time_ago_in_words" ruby method to resolve issue in Nitro where the latter method conflicts with our Nitro behavior. **Screenshots:** Screenshots to visualize your addition/change **How to test?** Steps to confirm the desired behavior: 1. Go to '...' 2. Click on '....' 3. Scroll down to '....' 4. See addition/change #### Checklist: - [x] **LABELS** Add a label: `enhancement`, `bug`, `improvement`, `new kit`, `deprecated`, or `breaking`. See [Changelog & Labels](https://github.com/powerhome/playbook/wiki/Changelog-&-Labels) for details. - [ ] **DEPLOY** I have added the `milano` label to show I'm ready for a review. - [ ] **TESTS** I have added test coverage to my code. --- .../docs/_timestamp_elapsed.html.erb | 4 +- .../playbook/pb_timestamp/timestamp.rb | 48 ++++++++++++++++++- .../pb_kits/playbook/kits/timestamp_spec.rb | 4 +- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/playbook/app/pb_kits/playbook/pb_timestamp/docs/_timestamp_elapsed.html.erb b/playbook/app/pb_kits/playbook/pb_timestamp/docs/_timestamp_elapsed.html.erb index f9e13d7ccd..761bbebf18 100644 --- a/playbook/app/pb_kits/playbook/pb_timestamp/docs/_timestamp_elapsed.html.erb +++ b/playbook/app/pb_kits/playbook/pb_timestamp/docs/_timestamp_elapsed.html.erb @@ -8,7 +8,7 @@
<%= pb_rails("timestamp", props: { - timestamp: DateTime.now, + timestamp: DateTime.now - 3.months, variant: "elapsed", show_user: false }) %> @@ -16,7 +16,7 @@
<%= pb_rails("timestamp", props: { - timestamp: DateTime.now, + timestamp: DateTime.now - 320.days, variant: "elapsed", show_user: false, hide_updated: true diff --git a/playbook/app/pb_kits/playbook/pb_timestamp/timestamp.rb b/playbook/app/pb_kits/playbook/pb_timestamp/timestamp.rb index 6213c8102b..af800cfe60 100644 --- a/playbook/app/pb_kits/playbook/pb_timestamp/timestamp.rb +++ b/playbook/app/pb_kits/playbook/pb_timestamp/timestamp.rb @@ -27,6 +27,18 @@ class Timestamp < Playbook::KitBase values: %w[default elapsed updated], default: "default" + # Variables to use with pb_time_ago method + SECS_FORTY_FIVE = 45 + SECS_PER_MIN = 60 + SECS_PER_HOUR = 60 * SECS_PER_MIN # 3,600 seconds + SECS_PER_DAY = 24 * SECS_PER_HOUR # 86,400 seconds + SECS_PER_WEEK = 7 * SECS_PER_DAY # 604,800 seconds + SECS_PER_26 = 26 * SECS_PER_DAY # 26 days = 2,246,400 seconds + SECS_PER_MONTH = 4 * SECS_PER_WEEK # 2,419,200 seconds + SECS_PER_YEAR = 12 * SECS_PER_MONTH # 29,030,400 seconds + SECS_PER_320 = 320 * SECS_PER_DAY # 320 days = 27,648,000 seconds + SECS_PER_CENT = 100 * SECS_PER_YEAR # 3,153,600,000 seconds + def classname generate_classname("pb_timestamp_kit", variant_class, align) end @@ -73,12 +85,44 @@ def format_updated_string def format_elapsed_string user_string = show_user ? " by #{text}" : "" - datetime_string = " #{time_ago_in_words(pb_date_time.convert_to_timestamp)} ago" + datetime_string = " #{pb_time_ago(pb_date_time.convert_to_timestamp)} ago" + datetime_string[1] = hide_updated ? datetime_string[1].upcase : datetime_string[1] updated_string = hide_updated ? "" : "Last updated" - "#{updated_string}#{user_string}#{datetime_string}" end + def pb_time_ago(value) + time_ago = DateTime.now.to_i - value.to_i + case time_ago + when (0...SECS_FORTY_FIVE) + "a few seconds" + when (SECS_FORTY_FIVE...SECS_PER_MIN) + "a minute" + when (SECS_PER_MIN...SECS_PER_HOUR) + time = time_ago / SECS_PER_MIN + time == 1 ? "a minute" : "#{time_ago / SECS_PER_MIN} minutes" + when (SECS_PER_HOUR...SECS_PER_DAY) + time = time_ago / SECS_PER_HOUR + time == 1 ? "an hour" : "#{time_ago / SECS_PER_HOUR} hours" + when (SECS_PER_DAY...SECS_PER_WEEK) + time = time_ago / SECS_PER_DAY + time == 1 ? "a day" : "#{time_ago / SECS_PER_DAY} days" + when (SECS_PER_WEEK...SECS_PER_26) + time = time_ago / SECS_PER_WEEK + time == 1 ? "a week" : "#{time_ago / SECS_PER_WEEK} weeks" + when (SECS_PER_26...SECS_PER_MONTH) + "a month" + when (SECS_PER_MONTH...SECS_PER_320) + time = time_ago / SECS_PER_MONTH + time == 1 ? "a month" : "#{time_ago / SECS_PER_MONTH} months" + when (SECS_PER_320...SECS_PER_YEAR) + "a year" + when (SECS_PER_YEAR...SECS_PER_CENT) + time = time_ago / SECS_PER_YEAR + time == 1 ? "a year" : "#{time_ago / SECS_PER_YEAR} years" + end + end + def datetime_or_text timestamp ? format_datetime_string : text end diff --git a/playbook/spec/pb_kits/playbook/kits/timestamp_spec.rb b/playbook/spec/pb_kits/playbook/kits/timestamp_spec.rb index 56e7265bf1..42294019ed 100644 --- a/playbook/spec/pb_kits/playbook/kits/timestamp_spec.rb +++ b/playbook/spec/pb_kits/playbook/kits/timestamp_spec.rb @@ -128,7 +128,7 @@ it "returns time elaspsed string including user's name" do timestamp = DateTime.new(2020, 10, 10, 20, 30, 0o0).in_time_zone("America/New_York").freeze - expect(subject.new(timestamp: timestamp, variant: variant, show_user: show_user, text: name).send(:format_elapsed_string)).to eq("Last updated by #{name} #{time_ago_in_words(timestamp)} ago") + expect(subject.new(timestamp: timestamp, variant: variant, show_user: show_user, text: name).send(:format_elapsed_string)).to eq("Last updated by #{name} #{subject.new.send(:pb_time_ago, timestamp)} ago") end end @@ -137,7 +137,7 @@ it "returns time elaspsed string sans user's name" do timestamp = DateTime.new(2020, 10, 10, 20, 30, 0o0).in_time_zone("America/New_York").freeze - expect(subject.new(timestamp: timestamp, variant: variant, show_user: show_user).send(:format_elapsed_string)).to eq("Last updated #{time_ago_in_words(timestamp)} ago") + expect(subject.new(timestamp: timestamp, variant: variant, show_user: show_user).send(:format_elapsed_string)).to eq("Last updated #{subject.new.send(:pb_time_ago, timestamp)} ago") end end end