Skip to content

A platform for community discussion. Free, open, simple.

DEV: Extract embed post date title into i18n-aware helper (#38594)

Isaac Janzenb1f4499768e6
diff --git a/app/helpers/embed_helper.rb b/app/helpers/embed_helper.rb
index 3af784f89aa..bcd8e8c3587 100644
--- a/app/helpers/embed_helper.rb
+++ b/app/helpers/embed_helper.rb
@@ -1,6 +1,10 @@
# frozen_string_literal: true
module EmbedHelper
+ def embed_post_date_title(dt)
+ dt.strftime I18n.t("datetime_formats.formats.long")
+ end
+
def embed_post_date(dt)
current = Time.now
diff --git a/app/views/embed/comments.html.erb b/app/views/embed/comments.html.erb
index ffe98eb97a3..fcf286db700 100644
--- a/app/views/embed/comments.html.erb
+++ b/app/views/embed/comments.html.erb
@@ -10,7 +10,7 @@
<%- if @topic_view.posts.present? %>
<%- @topic_view.posts.each do |post| %>
<article class='post<%- if post.trashed? %> deleted<% end %>' id='post-<%= post.id.to_s %>'>
- <%= link_to embed_post_date(post.created_at), post.full_url, title: post.created_at.strftime("%B %e, %Y %l:%M%P"), class: 'post-date', target: "_blank" %>
+ <%= link_to embed_post_date(post.created_at), post.full_url, title: embed_post_date_title(post.created_at), class: 'post-date', target: "_blank" %>
<%- if post.reply_to_post.present? && !post.cooked.index('aside') %>
<%= link_to I18n.t('embed.in_reply_to', username: post.reply_to_post.username), post.reply_to_post.full_url, 'data-link-to-post' => post.reply_to_post.id.to_s, :class => 'in-reply-to', target: "_blank" %>
<%- end %>
diff --git a/spec/helpers/embed_helper_spec.rb b/spec/helpers/embed_helper_spec.rb
new file mode 100644
index 00000000000..450b8f4f937
--- /dev/null
+++ b/spec/helpers/embed_helper_spec.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+RSpec.describe EmbedHelper do
+ describe "#embed_post_date_title" do
+ it "returns localized long format" do
+ date = Time.zone.local(2026, 5, 15, 14, 30)
+ expected_format = I18n.t("datetime_formats.formats.long")
+ expect(helper.embed_post_date_title(date)).to eq(date.strftime(expected_format))
+ end
+ end
+
+ describe "#embed_post_date" do
+ it "returns relative time for dates within the last day" do
+ freeze_time
+ expect(helper.embed_post_date(12.hours.ago)).to eq(
+ distance_of_time_in_words(12.hours.ago, Time.now),
+ )
+ end
+
+ it "returns localized format for dates in the current year" do
+ freeze_time DateTime.parse("2026-06-15")
+ date = 2.months.ago
+ expected_format = I18n.t("datetime_formats.formats.short_no_year")
+ expect(helper.embed_post_date(date)).to eq(date.strftime(expected_format))
+ end
+
+ it "returns localized format for dates in a previous year" do
+ date = 2.years.ago
+ expected_format = I18n.t("datetime_formats.formats.no_day")
+ expect(helper.embed_post_date(date)).to eq(date.strftime(expected_format))
+ end
+ end
+end