Skip to content

Commit aff1a6c

Browse files
committed
Add updated date to page header
Add spec for page header component
1 parent 49ee800 commit aff1a6c

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

app/content/layouts/article.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<%= render Pages::Header.new(
55
title: current_page.data.title!,
66
description: current_page.data.description,
7-
published_on: current_page.data.published&.to_date
7+
published_on: current_page.data.published&.to_date,
8+
updated_on: current_page.data.updated&.to_date
89
) %>
910
<%- if current_page.data.toc -%>
1011
<aside>

app/views/components/pages/header.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ class Pages::Header < ApplicationComponent
44

55
attr_accessor :current_page
66

7-
def initialize(title: nil, description: nil, published_on: nil)
7+
def initialize(title: nil, description: nil, published_on: nil, updated_on: nil)
88
@title = title
99
@description = description
1010
@published_on = published_on
11+
@updated_on = updated_on
1112
end
1213

1314
def view_template
@@ -19,11 +20,27 @@ def view_template
1920
h1 { @title }
2021
end
2122
p(class: "description") { @description } if @description
22-
if @published_on
23+
if @published_on || @updated_on
2324
span(class: "block") do
24-
# <time datetime="2024-03-13T00:00:00Z" itemprop="datePublished" class="dt-published"> March 13th, 2024 </time>
25-
em do
26-
time_tag @published_on, itemprop: "datePublished", class: "dt-published"
25+
if @published_on && @updated_on
26+
plain "Published:"
27+
whitespace
28+
end
29+
if @published_on
30+
em do
31+
time_tag @published_on, itemprop: "datePublished", class: "dt-published"
32+
end
33+
end
34+
if @updated_on
35+
if @published_on
36+
plain " // "
37+
end
38+
plain "Updated:"
39+
whitespace
40+
41+
em do
42+
time_tag @updated_on, itemprop: "dateModified", class: "dt-modified"
43+
end
2744
end
2845
end
2946
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Pages::Header, type: :view do
4+
def render(**kwargs)
5+
described_class.new(**kwargs).call(view_context: view)
6+
end
7+
8+
describe "#call" do
9+
it "renders the header" do
10+
expect(render(title: "Hello")).to have_css(".page-header h1", text: "Hello")
11+
end
12+
13+
it "renders the description" do
14+
expect(render(description: "Describe me")).to have_css(".page-header p", text: "Describe me")
15+
end
16+
17+
it "renders the published date" do
18+
expect(render(published_on: Date.today)).to have_css("time.dt-published", text: Date.today.to_fs(:long))
19+
end
20+
21+
it "renders the updated date" do
22+
expect(render(updated_on: Date.today)).to have_css("time.dt-modified", text: Date.today.to_fs(:long))
23+
end
24+
25+
it "renders the published and updated date" do
26+
rendered = render(published_on: Date.yesterday, updated_on: Date.today)
27+
expect(rendered).to have_css("time.dt-published", text: Date.yesterday.to_fs(:long))
28+
expect(rendered).to have_css("time.dt-modified", text: Date.today.to_fs(:long))
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)