Skip to content

Commit 4043516

Browse files
committed
fix: 年フィルタリング時のアクティブ状態を選択年時点の状態に修正
## 問題 - /dojos?year=2023 で2024年に非アクティブ化された道場が灰色表示されていた - 現在のis_activeを使用していたため、選択年時点の状態と異なっていた ## 解決策 - 選択年末時点でのアクティブ状態を計算するロジックを追加 - inactivated_atと選択年を比較して正しい状態を判定 ## テスト - inactive-item CSSクラスの存在をチェックするテストを追加 - TDDアプローチ:先にテストを書いて失敗を確認してから修正 ## 今後の課題(このPRではやらない) - inactive-item → inactivated-item へのCSS名変更 - is_activeカラムの削除(inactivated_atで代替可能) - 変数名・コメントのinactive → inactivatedへの統一
1 parent 4ed4e33 commit 4043516

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

app/controllers/dojos_controller.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,24 @@ def index
3232

3333
@dojos = []
3434
dojos_scope.includes(:prefecture).order(is_active: :desc, order: :asc).each do |dojo|
35+
# 年が選択されている場合は、その年末時点でのアクティブ状態を判定
36+
# 選択されていない場合は、現在の is_active を使用
37+
is_active_at_selected_time = if @selected_year
38+
# その年末時点でアクティブだったかを判定
39+
# inactivated_at が nil(まだアクティブ)または選択年より後に非アクティブ化
40+
dojo.inactivated_at.nil? || dojo.inactivated_at > Time.zone.local(@selected_year).end_of_year
41+
else
42+
dojo.is_active
43+
end
44+
3545
@dojos << {
3646
id: dojo.id,
3747
url: dojo.url,
3848
name: dojo.name,
3949
logo: root_url + dojo.logo[1..],
4050
order: dojo.order,
4151
counter: dojo.counter,
42-
is_active: dojo.is_active,
52+
is_active: is_active_at_selected_time,
4353
prefecture: dojo.prefecture.name,
4454
created_at: dojo.created_at,
4555
description: dojo.description,

spec/requests/dojos_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,27 @@
128128
expect(dojo_ids).not_to include(@dojo_2021_active.id)
129129
end
130130

131+
it "does not show inactivated styling for dojos active in 2020" do
132+
get dojos_path(year: 2020, format: :html)
133+
134+
# HTMLレスポンスを取得
135+
html = response.body
136+
137+
# 2021年に非アクティブ化された道場(Test Dojo 2020 Inactive)が含まれていることを確認
138+
expect(html).to include("Test Dojo 2020 Inactive")
139+
140+
# その道場の行を探す(IDで特定)
141+
dojo_row_match = html.match(/Test Dojo 2020 Inactive.*?<\/tr>/m)
142+
expect(dojo_row_match).not_to be_nil
143+
144+
dojo_row = dojo_row_match[0]
145+
146+
# 重要: この道場は2021年3月に非アクティブ化されたが、
147+
# 2020年末時点ではアクティブだったので、inactive-item クラスを持たないべき
148+
# 現在のコードはここで失敗するはず(現在の is_active: false を使っているため)
149+
expect(dojo_row).not_to include('class="inactive-item"')
150+
end
151+
131152
it "filters correctly in CSV format" do
132153
get dojos_path(year: 2020, format: :csv)
133154

@@ -154,6 +175,25 @@
154175
expect(dojo_ids).not_to include(@dojo_2020_inactive.id)
155176
expect(dojo_ids).not_to include(@dojo_2019_inactive.id)
156177
end
178+
179+
it "does not show any inactivated dojos for year 2021" do
180+
get dojos_path(year: 2021, format: :html)
181+
182+
html = response.body
183+
184+
# 2021年末時点でアクティブな道場のみが含まれる
185+
expect(html).to include("Test Dojo 2020") # アクティブ
186+
expect(html).to include("Test Dojo 2021") # アクティブ
187+
expect(html).to include("Multi Branch Dojo") # アクティブ
188+
189+
# 2021年に非アクティブ化された道場は含まれない
190+
expect(html).not_to include("Test Dojo 2020 Inactive")
191+
expect(html).not_to include("Test Dojo 2019 Inactive")
192+
193+
# すべての表示された道場は inactive-item クラスを持たないべき
194+
# (2021年末時点ではすべてアクティブなので)
195+
expect(html.scan('class="inactive-item"').count).to eq(0)
196+
end
157197
end
158198
end
159199

0 commit comments

Comments
 (0)