|
1 | 1 | class DojosController < ApplicationController |
2 | 2 |
|
3 | | - # GET /dojos[.json] |
| 3 | + # GET /dojos[.html|.json|.csv] |
4 | 4 | def index |
| 5 | + # yearパラメータがある場合の処理 |
| 6 | + if params[:year].present? |
| 7 | + begin |
| 8 | + year = params[:year].to_i |
| 9 | + # 有効な年の範囲をチェック |
| 10 | + unless year.between?(2012, Date.current.year) |
| 11 | + flash[:inline_alert] = "指定された年は無効です。2012年から#{Date.current.year}年の間で指定してください。" |
| 12 | + return redirect_to dojos_path(anchor: 'table') |
| 13 | + end |
| 14 | + |
| 15 | + @selected_year = year |
| 16 | + year_end = Time.zone.local(@selected_year).end_of_year |
| 17 | + |
| 18 | + # その年末時点でアクティブだった道場を取得 |
| 19 | + dojos_scope = Dojo.active_at(year_end) |
| 20 | + @page_title = "#{@selected_year}年末時点のCoderDojo一覧" |
| 21 | + rescue ArgumentError |
| 22 | + flash[:inline_alert] = "無効な年が指定されました" |
| 23 | + return redirect_to dojos_path(anchor: 'table') |
| 24 | + end |
| 25 | + else |
| 26 | + # yearパラメータなしの場合(既存の実装そのまま) |
| 27 | + dojos_scope = Dojo.all |
| 28 | + end |
| 29 | + |
5 | 30 | @dojos = [] |
6 | | - Dojo.includes(:prefecture).order(order: :asc).all.each do |dojo| |
| 31 | + dojos_scope.includes(:prefecture).order(is_active: :desc, order: :asc).each do |dojo| |
| 32 | + # 年が選択されている場合は、その年末時点でのアクティブ状態を判定 |
| 33 | + # 選択されていない場合は、現在の is_active を使用 |
| 34 | + is_active_at_selected_time = if @selected_year |
| 35 | + # その年末時点でアクティブだったかを判定 |
| 36 | + # inactivated_at が nil(まだアクティブ)または選択年より後に非アクティブ化 |
| 37 | + dojo.inactivated_at.nil? || dojo.inactivated_at > Time.zone.local(@selected_year).end_of_year |
| 38 | + else |
| 39 | + dojo.is_active |
| 40 | + end |
| 41 | + |
7 | 42 | @dojos << { |
8 | 43 | id: dojo.id, |
9 | 44 | url: dojo.url, |
10 | 45 | name: dojo.name, |
11 | 46 | logo: root_url + dojo.logo[1..], |
12 | 47 | order: dojo.order, |
13 | 48 | counter: dojo.counter, |
14 | | - is_active: dojo.is_active, |
| 49 | + is_active: is_active_at_selected_time, |
15 | 50 | prefecture: dojo.prefecture.name, |
16 | 51 | created_at: dojo.created_at, |
17 | 52 | description: dojo.description, |
| 53 | + inactivated_at: dojo.inactivated_at, # CSV用に追加 |
18 | 54 | } |
19 | 55 | end |
| 56 | + |
| 57 | + # counter合計を計算(/statsとの照合用) |
| 58 | + @counter_sum = @dojos.sum { |d| d[:counter] } |
| 59 | + |
| 60 | + # 情報メッセージを設定 |
| 61 | + if @selected_year |
| 62 | + # /statsページと同じ計算方法を使用 |
| 63 | + # 開設数 = その年に新規開設されたDojoのcounter合計 |
| 64 | + year_begin = Time.zone.local(@selected_year).beginning_of_year |
| 65 | + year_end = Time.zone.local(@selected_year).end_of_year |
| 66 | + new_dojos_count = Dojo.where(created_at: year_begin..year_end).sum(:counter) |
| 67 | + |
| 68 | + # 合計数 = その年末時点でアクティブだったDojoのcounter合計 |
| 69 | + total_dojos_count = Dojo.active_at(year_end).sum(:counter) |
| 70 | + |
| 71 | + # 表示用の日付テキスト |
| 72 | + display_date = "#{@selected_year}年末" |
| 73 | + display_date = Date.current.strftime('%Y年%-m月%-d日') if @selected_year == Date.current.year |
| 74 | + |
| 75 | + flash.now[:inline_info] = "#{display_date}時点のアクティブな道場を表示中<br>(開設数: #{new_dojos_count} / 合計数: #{total_dojos_count})".html_safe |
| 76 | + else |
| 77 | + # 全期間表示時の情報メッセージ |
| 78 | + flash.now[:inline_info] = "全期間の道場を表示中(非アクティブ含む)" |
| 79 | + end |
20 | 80 |
|
21 | 81 | respond_to do |format| |
22 | | - # No corresponding View for now. |
23 | | - # Only for API: GET /dojos.json |
24 | | - format.html # => app/views/dojos/index.html.erb |
| 82 | + format.html { render :index } # => app/views/dojos/index.html.erb |
25 | 83 | format.json { render json: @dojos } |
| 84 | + format.csv do |
| 85 | + # ファイル名を年に応じて設定 |
| 86 | + filename = if @selected_year |
| 87 | + "dojos_#{@selected_year}.csv" |
| 88 | + else |
| 89 | + "dojos_all.csv" |
| 90 | + end |
| 91 | + send_data render_to_string, type: :csv, filename: filename |
| 92 | + end |
26 | 93 | end |
27 | 94 | end |
28 | 95 |
|
|
0 commit comments