Skip to content

Commit 4ed4e33

Browse files
committed
refactor: inline_プレフィックスパターンでflashメッセージの表示位置を制御
## 実装内容 - 'inline_' プレフィックスがついたflashメッセージは、デフォルト位置(ページ上部)では表示しない - 各ビュー内でカスタム位置に表示できるようにヘルパーメソッドを追加 - /dojos ページの年フィルタリングのアラートとインフォメッセージに適用 ## メリット - flashメッセージの2重表示問題を解決 - inline_alert → alert-alert のようにBootstrapのCSSクラスを自動適用 - 将来的に inline_warning, inline_success なども同じパターンで使用可能 - コードがシンプルになり、条件分岐が不要に ## 変更箇所 - flash[:alert] → flash[:inline_alert] に変更(エラー時) - flash.now[:inline_info] を追加(成功時) - render_inline_flash_messages ヘルパーメソッドを追加 - テストを inline_alert に対応
1 parent 697e8af commit 4ed4e33

File tree

5 files changed

+24
-15
lines changed

5 files changed

+24
-15
lines changed

app/controllers/dojos_controller.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def index
88
year = params[:year].to_i
99
# 有効な年の範囲をチェック
1010
unless year.between?(2012, Date.current.year)
11-
flash[:alert] = "指定された年は無効です。2012年から#{Date.current.year}年の間で指定してください。"
11+
flash[:inline_alert] = "指定された年は無効です。2012年から#{Date.current.year}年の間で指定してください。"
1212
return redirect_to dojos_path(anchor: 'table')
1313
end
1414

@@ -18,8 +18,11 @@ def index
1818
# その年末時点でアクティブだった道場を取得
1919
dojos_scope = Dojo.active_at(end_of_year)
2020
@page_title = "#{@selected_year}年末時点のCoderDojo一覧"
21+
22+
# 成功メッセージもinline_で表示
23+
flash.now[:inline_info] = "#{@selected_year}年末時点のアクティブな道場を表示中"
2124
rescue ArgumentError
22-
flash[:alert] = "無効な年が指定されました"
25+
flash[:inline_alert] = "無効な年が指定されました"
2326
return redirect_to dojos_path(anchor: 'table')
2427
end
2528
else

app/helpers/application_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ def page_lang(lang)
4848
lang.empty? ? 'ja' : lang
4949
end
5050

51+
# 'inline_' プレフィックスがついたflashメッセージをビュー内で表示するヘルパー
52+
# inline_alert → alert, inline_warning → warning のように変換してBootstrapのCSSクラスを適用
53+
def render_inline_flash_messages
54+
flash.select { |type, _| type.to_s.start_with?('inline_') }.map do |type, message|
55+
css_class = type.to_s.gsub('inline_', '')
56+
content_tag(:div, message, class: "alert alert-#{css_class}", style: "margin-bottom: 15px;")
57+
end.join.html_safe
58+
end
59+
5160
def kata_description
5261
"道場で役立つ資料やコンテスト情報、立ち上げ方や各種支援をまとめています。"
5362
end

app/views/dojos/index.html.erb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,7 @@
3333
年次データのフィルタリング
3434
</h3>
3535

36-
<% if flash[:alert] %>
37-
<div style="padding: 10px; background: #f8d7da; border: 1px solid #f5c6cb; border-radius: 4px; margin-bottom: 15px; color: #721c24;">
38-
<%= flash[:alert] %>
39-
</div>
40-
<% elsif @selected_year %>
41-
<div style="padding: 10px; background: #d1ecf1; border: 1px solid #bee5eb; border-radius: 4px; margin-bottom: 15px;">
42-
<strong><%= @selected_year %>年末時点</strong>のアクティブな道場を表示中
43-
</div>
44-
<% end %>
36+
<%= render_inline_flash_messages %>
4537

4638
<div style="display: flex; align-items: center; justify-content: center; gap: 10px; margin-top: 15px;">
4739
<%= label_tag :year, '対象期間:', style: 'font-weight: bold;' %>

app/views/layouts/application.html.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@
8888
<body>
8989
<%= render 'shared/header' %>
9090

91+
<%# 'inline_' プレフィックスがついたflashメッセージは、ここでは表示せず、各ビュー内でカスタム表示する %>
9192
<% flash.each do |message_type, message| %>
92-
<div class="alert alert-<%= message_type %>"><%= message %></div>
93+
<% unless message_type.to_s.start_with?('inline_') %>
94+
<div class="alert alert-<%= message_type %>"><%= message %></div>
95+
<% end %>
9396
<% end %>
9497

9598
<%= yield %>

spec/requests/dojos_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@
5555
it "rejects years before 2012" do
5656
get dojos_path(year: 2011, format: :json)
5757
expect(response).to redirect_to(dojos_path(anchor: 'table'))
58-
expect(flash[:alert]).to include("2012年から")
58+
expect(flash[:inline_alert]).to include("2012年から")
5959
end
6060

6161
it "rejects years after current year" do
6262
future_year = Date.current.year + 1
6363
get dojos_path(year: future_year, format: :json)
6464
expect(response).to redirect_to(dojos_path(anchor: 'table'))
65-
expect(flash[:alert]).to include("指定された年は無効です")
65+
expect(flash[:inline_alert]).to include("指定された年は無効です")
6666
end
6767

6868
it "handles invalid year strings" do
6969
get dojos_path(year: "invalid", format: :json)
7070
expect(response).to redirect_to(dojos_path(anchor: 'table'))
71-
expect(flash[:alert]).to include("無効")
71+
expect(flash[:inline_alert]).to include("無効")
7272
end
7373
end
7474

@@ -253,6 +253,8 @@
253253
get dojos_path(year: 2020)
254254
expect(response.body).to include('2020年末時点')
255255
expect(response.body).to include('アクティブな道場を表示中')
256+
# inline_infoメッセージが表示されることを確認
257+
expect(response.body).to include('alert-info')
256258
end
257259

258260
it "includes CSV and JSON download links with year parameter" do

0 commit comments

Comments
 (0)