Skip to content

Commit 738bc76

Browse files
committed
✨ /news ページを追加してニュース記事一覧を表示
- NewsController#indexアクションを実装 - 既存のNewsモデル(ActiveRecord)を使用 - すべてのニュース記事を最新順に表示するビューを作成 - /news ルーティングを追加 - テスト(spec/requests/news_spec.rb)を追加 /docsと同様に、ニュース記事の一覧ページを提供
1 parent 98d428b commit 738bc76

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

app/controllers/news_controller.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class NewsController < ApplicationController
2+
def index
3+
@title = 'CoderDojo ニュース'
4+
@desc = 'CoderDojo に関する最新のニュースや<br class="ignore-pc">お知らせをまとめたページです。'
5+
@url = request.url
6+
7+
# データベースからニュースデータを取得(最新順)
8+
@news_items = News.recent
9+
end
10+
end

app/views/news/index.html.erb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<% provide :title, strip_tags(@title) %>
2+
<% provide :desc, strip_tags(@desc) %>
3+
<% provide :url, @url %>
4+
<% provide :meta_image, "/img/ogp-news.jpeg" %>
5+
6+
<div class='container'>
7+
<section class='news' style='padding: 50px 0px;'>
8+
<h2 class='text-center'><%= @title.html_safe %></h2>
9+
10+
<p class='text-center' style='padding-bottom: 50px;'>
11+
<%= @desc.html_safe %>
12+
</p>
13+
14+
<% if @news_items.any? %>
15+
<div class="news-list">
16+
<% @news_items.each do |news| %>
17+
<article class="news-item" style="margin-bottom: 30px; padding-bottom: 20px; border-bottom: 1px solid #e0e0e0;">
18+
<h3 style="margin-bottom: 10px;">
19+
<%= link_to news.title, news.url, target: '_blank', rel: 'noopener' %>
20+
</h3>
21+
<p class="news-meta" style="color: #666; font-size: 14px;">
22+
<i class="fa fa-calendar"></i>
23+
<%= l(news.published_at.to_date, format: :long) if news.published_at %>
24+
</p>
25+
</article>
26+
<% end %>
27+
</div>
28+
<% else %>
29+
<p class="text-center">現在、ニュース記事はありません。</p>
30+
<% end %>
31+
</section>
32+
33+
<div style="margin-bottom: 40px;">
34+
<p style="color: #5F5F5F">
35+
<small>
36+
最新情報は
37+
<a href="https://news.coderdojo.jp" target="_blank" rel="noopener">CoderDojo Japan ブログ</a>
38+
<a href="https://www.facebook.com/groups/coderdojo.jp/" target="_blank" rel="noopener">Facebook グループ</a>
39+
でも配信しています! 📰✨
40+
</small>
41+
</p>
42+
</div>
43+
44+
<section class="text-center list" style='margin-bottom: 30px;'>
45+
<%= render 'shared/social_buttons' %>
46+
</section>
47+
</div>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
end
7575
end
7676
resources :docs, only: %i(index show)
77+
resources :news, only: %i(index)
7778
resources :podcasts, only: %i(index show)
7879
resources :spaces, only: %i(index)
7980

spec/requests/news_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe "News", type: :request do
4+
describe "GET /news" do
5+
before do
6+
# テスト用のニュースデータを作成
7+
@news1 = News.create!(
8+
title: "テストニュース1",
9+
url: "https://example.com/news1",
10+
published_at: 2.days.ago
11+
)
12+
@news2 = News.create!(
13+
title: "テストニュース2",
14+
url: "https://example.com/news2",
15+
published_at: 1.day.ago
16+
)
17+
@news3 = News.create!(
18+
title: "テストニュース3",
19+
url: "https://example.com/news3",
20+
published_at: 3.days.ago
21+
)
22+
end
23+
24+
it "正常にレスポンスを返す" do
25+
get news_index_path
26+
expect(response).to have_http_status(:success)
27+
end
28+
29+
it "適切なタイトルを表示する" do
30+
get news_index_path
31+
expect(response.body).to include("CoderDojo ニュース")
32+
end
33+
34+
it "ニュース記事を新しい順に表示する" do
35+
get news_index_path
36+
37+
# 新しい順に表示されることを確認
38+
news2_pos = response.body.index(@news2.title)
39+
news1_pos = response.body.index(@news1.title)
40+
news3_pos = response.body.index(@news3.title)
41+
42+
expect(news2_pos).to be < news1_pos
43+
expect(news1_pos).to be < news3_pos
44+
end
45+
46+
it "ニュースのタイトルとリンクを表示する" do
47+
get news_index_path
48+
49+
expect(response.body).to include(@news1.title)
50+
expect(response.body).to include(@news1.url)
51+
expect(response.body).to include(@news2.title)
52+
expect(response.body).to include(@news2.url)
53+
end
54+
55+
it "ニュースがない場合は適切なメッセージを表示する" do
56+
News.destroy_all
57+
get news_index_path
58+
59+
expect(response.body).to include("現在、ニュース記事はありません")
60+
end
61+
end
62+
end

0 commit comments

Comments
 (0)