Skip to content

Commit 6704d85

Browse files
committed
Removed time_diff_words method and replaced with rails distance_of_time_in_words
1 parent f822df6 commit 6704d85

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

app/services/cms/models/collections/email_template.rb

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module Cms
22
module Models
33
module Collections
44
class EmailTemplate
5+
include ActionView::Helpers::DateHelper
6+
57
attr_accessor :slug, :email_content, :programme, :completed_programme_activity_groups, :activity_state, :enrolled
68

79
def initialize(slug:, subject:, email_content:, programme_slug:, completed_programme_activity_group_slugs:, activity_state:, enrolled:)
@@ -47,8 +49,10 @@ def merge_content(text, user)
4749
achievements = user.sorted_completed_cpd_achievements_by(programme: @programme)
4850
if achievements.any?
4951
achievement = achievements.last
52+
last_cpd_completed_ago = distance_of_time_in_words(achievement.updated_at, DateTime.now)
53+
5054
merges += [
51-
["{last_cpd_completed_ago}", time_diff_words(achievement.updated_at)],
55+
["{last_cpd_completed_ago}", last_cpd_completed_ago],
5256
["{last_cpd_completed_year}", "in #{achievement.updated_at.year}"],
5357
["{last_cpd_title}", achievement.activity.title]
5458
]
@@ -57,21 +61,6 @@ def merge_content(text, user)
5761
merges.each { new_text.gsub!(_1[0], _1[1]) }
5862
new_text
5963
end
60-
61-
def time_diff_words(date)
62-
from_time = date.to_time
63-
to_time = DateTime.now.to_time
64-
65-
months = (to_time.year * 12 + to_time.month) - (from_time.year * 12 + from_time.month)
66-
months = 1 if months == 0
67-
68-
if months >= 12
69-
years = months / 12
70-
"#{years} #{"year".pluralize(years)}"
71-
else
72-
"#{months} #{"month".pluralize(months)}"
73-
end
74-
end
7564
end
7665
end
7766
end

spec/services/cms/models/collections/email_template_spec.rb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
type: "text"
3636
}
3737
]
38+
},
39+
{
40+
type: "paragraph",
41+
children: [
42+
{
43+
text: "You completed {last_cpd_title} {last_cpd_completed_ago} ago.",
44+
type: "text"
45+
}
46+
]
3847
}
3948
]
4049
}
@@ -67,21 +76,26 @@
6776
expect(text).to eq("Frodo should click this link")
6877
end
6978

70-
context "time_diff_words" do
71-
it "should return months" do
72-
expect(@model.time_diff_words(3.months.ago)).to eq("3 months")
73-
end
79+
context "with achievement" do
80+
let(:activity) { create(:activity, title: "Past activity") }
81+
let!(:achievement) { create(:completed_achievement, activity:, user:) }
82+
let(:updated_at) { DateTime.parse("2025-04-01") }
83+
84+
before do
85+
allow(achievement).to receive(:updated_at).and_return(updated_at)
86+
allow(user).to receive(:sorted_completed_cpd_achievements_by).and_return([achievement])
7487

75-
it "should return month when only one" do
76-
expect(@model.time_diff_words(1.months.ago)).to eq("1 month")
88+
travel_to DateTime.parse("2025-05-01")
7789
end
7890

79-
it "should return year when over 12 months" do
80-
expect(@model.time_diff_words(12.months.ago)).to eq("1 year")
91+
after do
92+
travel_back
8193
end
8294

83-
it "should return years when over 24 months" do
84-
expect(@model.time_diff_words(25.months.ago)).to eq("2 years")
95+
it "should replace achievement placeholders using distance_of_time_in_words" do
96+
content = @model.process_blocks(text_content, user)
97+
text = content.dig(2, :children, 0, :text)
98+
expect(text).to eq("You completed Past activity about 1 month ago.")
8599
end
86100
end
87101

0 commit comments

Comments
 (0)