Skip to content

Commit ec7f4c6

Browse files
authored
Merge pull request #1784 from coderdojo-japan/improve-activity-page-inactive-dojos
活動状況ページで非アクティブ道場も表示し、降順でソート
2 parents 45c847c + ea8ac4f commit ec7f4c6

3 files changed

Lines changed: 102 additions & 11 deletions

File tree

app/controllers/dojos_controller.rb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ def activity
113113
@inactive_threshold = Dojo::INACTIVE_THRESHOLD_IN_MONTH
114114

115115
@latest_event_by_dojos = []
116-
Dojo.active.each do |dojo|
116+
117+
# アクティブな道場と非アクティブな道場を両方取得
118+
# アクティブな道場を先に、非アクティブな道場を後に表示
119+
dojos = Dojo.active.to_a + Dojo.inactive.to_a
120+
121+
dojos.each do |dojo|
117122
link_in_note = dojo.note.match(URI.regexp)
118123
# YYYY-MM-DD、YYYY/MM/DD、または YYYY年MM月DD日 形式の日付を抽出
119124
date_in_note = dojo.note.match(/(\d{4}-\d{1,2}-\d{1,2}|\d{4}\/\d{1,2}\/\d{1,2}|\d{4}年\d{1,2}月\d{1,2}日)/)
@@ -126,6 +131,7 @@ def activity
126131
note: dojo.note,
127132
url: dojo.url,
128133
created_at: dojo.created_at, # 掲載日(/dojos と同じ)
134+
is_active: dojo.active?, # アクティブ状態を追加
129135

130136
# 直近の開催日(イベント履歴がある場合のみ)
131137
latest_event_at: latest_event&.evented_at,
@@ -137,11 +143,24 @@ def activity
137143
}
138144
end
139145

140-
# Sort by latest event date (or created_at if no events) && Dojo's order if same date
141-
@latest_event_by_dojos.sort_by! do |dojo|
146+
# アクティブな道場と非アクティブな道場を分けてソート
147+
active_dojos = @latest_event_by_dojos.select { |d| d[:is_active] }
148+
inactive_dojos = @latest_event_by_dojos.reject { |d| d[:is_active] }
149+
150+
# それぞれのグループ内でソート
151+
active_dojos.sort_by! do |dojo|
142152
sort_date = dojo[:latest_event_at] || dojo[:note_date] || dojo[:created_at]
143153
[sort_date, dojo[:order]]
144154
end
155+
156+
# 非アクティブな道場は最新の開催日から古い順(降順)にソート
157+
inactive_dojos.sort_by! do |dojo|
158+
sort_date = dojo[:latest_event_at] || dojo[:note_date] || dojo[:created_at]
159+
[-sort_date.to_i, dojo[:order]] # マイナスを付けて降順にする
160+
end
161+
162+
# アクティブな道場を先に、非アクティブな道場を後に
163+
@latest_event_by_dojos = active_dojos + inactive_dojos
145164
end
146165

147166
private

app/views/dojos/activity.html.erb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,18 @@
7474
</tr>
7575
<% @latest_event_by_dojos.each do |dojo| %>
7676
<tr>
77-
<td>
77+
<td class="<%= 'inactive-item' unless dojo[:is_active] %>">
7878
<small>
7979
<%= link_to dojo_path(dojo[:id]) do %>
8080
<%= dojo[:name] %><br>
8181
<small>(ID: <%= dojo[:id] %>)</small>
8282
<% end %>
8383
</small>
8484
</td>
85-
<td>
85+
<td class="<%= 'inactive-item' unless dojo[:is_active] %>">
8686
<small><%= dojo[:created_at].strftime("%Y-%m-%d") %></small>
8787
</td>
88-
<td>
88+
<td class="<%= 'inactive-item' unless dojo[:is_active] %>">
8989
<small>
9090
<% if dojo[:note_date] && dojo[:latest_event_at] %>
9191
<!-- 両方の日付がある場合:より新しい方を表示 -->
@@ -128,7 +128,7 @@
128128
<% end %>
129129
</small>
130130
</td>
131-
<td>
131+
<td class="<%= 'inactive-item' unless dojo[:is_active] %>">
132132
<small><small>
133133
<% search_query = URI.encode_www_form(q: "CoderDojo #{dojo[:name].split('@').first.strip}") %>
134134
<% search_range = URI.encode_www_form(tbs: "qdr:m12") # 過去12ヶ月の検索結果 %>
@@ -138,15 +138,15 @@
138138
<%= link_to '12ヶ月', [search_url, search_range].join('&'), target: "_blank" %>
139139
</small></small>
140140
</td>
141-
<td>
141+
<td class="<%= 'inactive-item' unless dojo[:is_active] %>">
142142
<small><small>
143143
<%= link_to dojo[:url], target: "_blank" do %>
144144
Website
145145
<small><i class="far fa-external-link"></i></small>
146146
<% end %>
147147
</small></small>
148148
</td>
149-
<td class="url-cell">
149+
<td class="url-cell <%= 'inactive-item' unless dojo[:is_active] %>">
150150
<small>
151151
<span title="<%= dojo[:note] %>">
152152
<%= raw Addressable::URI.unescape(truncate_with_preserved_links(dojo[:note], length: 60)) %>

spec/requests/dojos_spec.rb

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,82 @@
346346
expect(response.body).to include("道場別の活動状況まとめ")
347347
end
348348

349-
it "includes only active dojos" do
349+
it "includes both active and inactive dojos" do
350350
get activity_dojos_path
351351
expect(response.body).to include(@active_dojo.name)
352-
expect(response.body).not_to include(@inactive_dojo.name)
352+
expect(response.body).to include(@inactive_dojo.name)
353+
end
354+
355+
it "displays inactive dojos with inactive-item class" do
356+
get activity_dojos_path
357+
doc = Nokogiri::HTML(response.body)
358+
359+
# 非アクティブな道場の行を探す(リンク内にある名前を探す)
360+
inactive_link = doc.xpath("//a[contains(., '#{@inactive_dojo.name}')]").first
361+
expect(inactive_link).not_to be_nil
362+
363+
# そのリンクを含む行(tr)を取得
364+
inactive_row = inactive_link.xpath("ancestor::tr").first
365+
expect(inactive_row).not_to be_nil
366+
367+
# その行のすべてのセルに inactive-item クラスがあることを確認
368+
inactive_cells = inactive_row.css('td')
369+
expect(inactive_cells.all? { |cell| cell['class']&.include?('inactive-item') }).to be true
370+
end
371+
372+
it "sorts inactive dojos by latest event date in descending order" do
373+
# 複数の非アクティブな道場を作成(異なる作成日で)
374+
older_inactive_dojo = create(:dojo,
375+
name: "Older Inactive Dojo",
376+
created_at: 2.years.ago, # より古い作成日
377+
inactivated_at: 6.months.ago
378+
)
379+
380+
newer_inactive_dojo = create(:dojo,
381+
name: "Newer Inactive Dojo",
382+
created_at: 1.year.ago, # より新しい作成日
383+
inactivated_at: 3.months.ago
384+
)
385+
386+
# db/static_event_histories.yml の形式に合わせてEventHistory を作成
387+
# より古いイベント
388+
EventHistory.create!(
389+
dojo_id: older_inactive_dojo.id,
390+
dojo_name: older_inactive_dojo.name,
391+
event_url: 'https://example.com/event1',
392+
evented_at: 8.months.ago,
393+
participants: 10,
394+
service_name: 'static_yml',
395+
service_group_id: 'static',
396+
event_id: 1
397+
)
398+
399+
# より新しいイベント
400+
EventHistory.create!(
401+
dojo_id: newer_inactive_dojo.id,
402+
dojo_name: newer_inactive_dojo.name,
403+
event_url: 'https://example.com/event2',
404+
evented_at: 4.months.ago, # より最近のイベント
405+
participants: 15,
406+
service_name: 'static_yml',
407+
service_group_id: 'static',
408+
event_id: 2
409+
)
410+
411+
get activity_dojos_path
412+
doc = Nokogiri::HTML(response.body)
413+
414+
# 全ての道場の名前を取得(テーブルの最初のセルのリンクから)
415+
dojo_links = doc.css('table tr td:first-child a')
416+
dojo_names = dojo_links.map(&:text).map(&:strip)
417+
418+
# 非アクティブな道場の中で、newer_inactive_dojo が older_inactive_dojo より先に表示されることを確認
419+
newer_index = dojo_names.find_index { |name| name.include?(newer_inactive_dojo.name) }
420+
older_index = dojo_names.find_index { |name| name.include?(older_inactive_dojo.name) }
421+
422+
expect(newer_index).not_to be_nil
423+
expect(older_index).not_to be_nil
424+
expect(newer_index).to be < older_index
353425
end
354426

355427
it "redirects from old URL /events/latest" do

0 commit comments

Comments
 (0)