Skip to content

Commit 8d4f516

Browse files
committed
🔗 ニュースセクションの内部リンクから target='_blank' を削除
- News モデルに internal_link? メソッドを追加 - coderdojo.jp ドメインと相対パスのリンクを内部リンクとして判定 - 内部リンクの場合は新しいタブで開かないように変更 - 外部リンクには外部リンクアイコンを表示 - テストケースを追加して動作を保証
1 parent 955dd84 commit 8d4f516

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

app/models/news.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ def link_url
3232
url
3333
end
3434
end
35+
36+
def internal_link?
37+
# Check if the link is internal (coderdojo.jp domain)
38+
url.match?(%r{^https://coderdojo\.jp}) || url.start_with?('/')
39+
end
3540
end

app/views/home/show.html.erb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,16 @@
194194
<ul class="list" style="list-style: none;">
195195
<% @news_items.each do |news| %>
196196
<li>
197-
<%= link_to news.formatted_title, news.link_url, target: '_blank' %>
197+
<% if news.internal_link? %>
198+
<%= link_to news.link_url do %>
199+
<%= news.formatted_title %>
200+
<% end %>
201+
<% else %>
202+
<%= link_to news.link_url, target: '_blank' do %>
203+
<%= news.formatted_title %>
204+
<small><small><i class="far fa-external-link"></i></small></small>
205+
<% end %>
206+
<% end %>
198207
</li>
199208
<% end %>
200209
</ul>

app/views/news/index.html.erb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@
2323
<% if @news_items.any? %>
2424
<div class="news-list">
2525
<% @news_items.each do |news| %>
26+
2627
<article class="news-item" style="margin-bottom: 30px; padding-bottom: 20px; border-bottom: 1px solid #e0e0e0;">
2728
<h3 style="margin-bottom: 10px;">
28-
<%= link_to news.formatted_title, news.link_url, target: '_blank', rel: 'noopener' %>
29+
<% if news.internal_link? %>
30+
<%= link_to news.link_url do %>
31+
<%= news.formatted_title %>
32+
<% end %>
33+
<% else %>
34+
<%= link_to news.link_url, target: '_blank' do %>
35+
<%= news.formatted_title %>
36+
<small><i class="far fa-external-link"></i></small>
37+
<% end %>
38+
<% end %>
2939
</h3>
3040
<p class="news-meta" style="color: #666; font-size: 14px;">
31-
<i class="fa fa-calendar"></i>
41+
<!--<i class="fa fa-calendar"></i>-->
3242
<%= l(news.published_at.to_date, format: :long) if news.published_at %>
3343
</p>
3444
</article>

spec/models/news_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,35 @@
125125
expect(news2.link_url).to eq 'https://prtimes.jp/main/html/rd/p/000000001.000038935.html'
126126
end
127127
end
128+
129+
describe '#internal_link?' do
130+
context '内部リンクの場合' do
131+
it 'coderdojo.jpドメインのURLはtrueを返す' do
132+
news = build(:news, url: 'https://coderdojo.jp/podcasts/33')
133+
expect(news.internal_link?).to be true
134+
end
135+
136+
it '相対パスで始まるURLはtrueを返す' do
137+
news = build(:news, url: '/kata')
138+
expect(news.internal_link?).to be true
139+
end
140+
end
141+
142+
context '外部リンクの場合' do
143+
it 'news.coderdojo.jpサブドメインはfalseを返す' do
144+
news = build(:news, url: 'https://news.coderdojo.jp/2025/12/06/dojoletter')
145+
expect(news.internal_link?).to be false
146+
end
147+
148+
it 'prtimes.jpドメインはfalseを返す' do
149+
news = build(:news, url: 'https://prtimes.jp/main/html/rd/p/000000001.000038935.html')
150+
expect(news.internal_link?).to be false
151+
end
152+
153+
it '他の外部ドメインはfalseを返す' do
154+
news = build(:news, url: 'https://example.com/article')
155+
expect(news.internal_link?).to be false
156+
end
157+
end
158+
end
128159
end

0 commit comments

Comments
 (0)