Skip to content

Commit f67c8f7

Browse files
committed
Update ArticlePage.published logic
1 parent 2eae045 commit f67c8f7

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

app/content/models/article_page.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@ class ArticlePage < Sitepress::Model
88
delegate :mime_type, :handler, to: :page
99

1010
def self.published
11-
all
12-
.filter { |article| article.published.present? }
11+
all.filter(&:published?)
1312
.sort { |a, b| b.published_on <=> a.published_on } # DESC order
1413
end
1514

1615
def self.draft
17-
all.filter { |article| article.published.blank? }
16+
all.filter(&:draft?)
1817
end
1918

19+
def published?
20+
published_on.presence && published_on <= Date.today
21+
end
22+
23+
def draft? = !published?
24+
2025
def persisted?
2126
false
2227
end
2328

2429
def published_on
25-
published.to_date
30+
published&.to_date
2631
end
2732
alias_method :published_at, :published_on
2833
alias_method :created_at, :published_on

app/content/pages/articles/index.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
title: Articles
3+
description: Read the latest articles from Joy of Rails
4+
---
15
<section>
26
<div class="main-content container py-gap lg:py-3xl">
37
<% ArticlePage.published.take(20).zip(%w[left right]).each do |(page, side)| %>

spec/requests/site/articles_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Site: articles" do
4+
let(:first_article) { ArticlePage.published.first }
5+
6+
# Filter out index.html.erb from article pages before selecting first draft
7+
let(:first_draft) { ArticlePage.draft.lazy.filter { |article| article.page.request_path != "/articles" }.first }
8+
9+
describe "GET /articles" do
10+
it "lists published articles" do
11+
get "/articles"
12+
13+
expect(response).to have_http_status(:ok)
14+
15+
expect(response.body).to include(first_article.title)
16+
expect(response.body).not_to include(first_draft.title)
17+
end
18+
end
19+
20+
describe "GET /drafts" do
21+
it "lists draft articles" do
22+
get "/drafts"
23+
24+
expect(response).to have_http_status(:ok)
25+
26+
expect(response.body).to include(first_draft.title)
27+
expect(response.body).not_to include(first_article.title)
28+
end
29+
end
30+
end

spec/requests/site_spec.rb renamed to spec/requests/site/conditional_get_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require "rails_helper"
22

3-
RSpec.describe "site: http caching" do
3+
RSpec.describe "Site: http caching" do
44
let(:file_path) { Rails.root.join("app", "content", "pages", "articles", "introducing-joy-of-rails.html.mdrb") }
55
let(:file) { File.open(file_path) }
66

0 commit comments

Comments
 (0)