@@ -61,12 +61,7 @@ class DojosController < ApplicationController
61
61
@selected_year = year
62
62
end_of_year = Time .zone.local(@selected_year ).end_of_year
63
63
64
- # CSV/JSON形式の場合は統計データを返す
65
- if request.format .csv? || request.format .json?
66
- return render_yearly_stats
67
- end
68
-
69
- # HTML形式の場合は、その年末時点でアクティブだった道場を表示
64
+ # その年末時点でアクティブだった道場を取得
70
65
@dojos = []
71
66
Dojo .active_at(end_of_year).includes(:prefecture ).order(order: :asc ).each do |dojo |
72
67
@dojos << {
@@ -85,33 +80,33 @@ class DojosController < ApplicationController
85
80
86
81
@page_title = " #{ @selected_year } 年末時点のCoderDojo一覧"
87
82
else
88
- # yearパラメータなしの場合
89
- if request.format .csv? || request.format .json?
90
- # CSV/JSON: 全年次統計データを返す
91
- return render_yearly_stats
83
+ # yearパラメータなしの場合、現在のアクティブな道場リスト(既存の実装)
84
+ @dojos = []
85
+ Dojo .includes(:prefecture ).order(order: :asc ).all.each do |dojo |
86
+ @dojos << {
87
+ id: dojo.id,
88
+ url: dojo.url,
89
+ name: dojo.name,
90
+ logo: root_url + dojo.logo[1 ..],
91
+ order: dojo.order,
92
+ counter: dojo.counter,
93
+ is_active: dojo.is_active,
94
+ prefecture: dojo.prefecture.name,
95
+ created_at: dojo.created_at,
96
+ description: dojo.description,
97
+ }
92
98
end
93
-
94
- # HTML: 現在のアクティブな道場リスト(既存の実装)
95
- @dojos = []
96
- Dojo .includes(:prefecture ).order(order: :asc ).all.each do |dojo |
97
- @dojos << {
98
- id: dojo.id,
99
- url: dojo.url,
100
- name: dojo.name,
101
- logo: root_url + dojo.logo[1 ..],
102
- order: dojo.order,
103
- counter: dojo.counter,
104
- is_active: dojo.is_active,
105
- prefecture: dojo.prefecture.name,
106
- created_at: dojo.created_at,
107
- description: dojo.description,
108
- }
109
99
end
110
100
101
+ # respond_toで形式ごとに処理を分岐
111
102
respond_to do |format |
112
103
format .html # => app/views/dojos/index.html.erb
113
- format .json { render json: @dojos }
114
- format .csv { send_data render_to_string, type: :csv } # 既存の道場リスト
104
+ format .json do
105
+ params[:year ].present? ? render_yearly_stats : render(json: @dojos )
106
+ end
107
+ format .csv do
108
+ params[:year ].present? ? render_yearly_stats : send_data(render_to_string, type: :csv )
109
+ end
115
110
end
116
111
end
117
112
@@ -126,14 +121,7 @@ class DojosController < ApplicationController
126
121
@period_end = Date .current.year
127
122
128
123
# yearパラメータが指定されている場合(整数のみ許可)
129
- if params[:year ].present?
130
- year = params[:year ].to_i
131
- # 有効な年の範囲をチェック
132
- unless year.between?(@period_start , @period_end )
133
- return render json: { error: " Year must be between #{ @period_start } and #{ @period_end } " }, status: :bad_request
134
- end
135
-
136
- @selected_year = year
124
+ if @selected_year # 既にindexアクションで設定済み
137
125
period = Time .zone.local(@selected_year ).beginning_of_year..Time .zone.local(@selected_year ).end_of_year
138
126
@stat = Stat .new (period)
139
127
@yearly_data = prepare_single_year_data(@stat , @selected_year )
@@ -146,6 +134,7 @@ class DojosController < ApplicationController
146
134
filename_suffix = ' all'
147
135
end
148
136
137
+ # CSVまたはJSONとして返す
149
138
respond_to do |format |
150
139
format .csv do
151
140
send_data render_to_string(template: ' dojos/yearly_stats' ),
0 commit comments