Skip to content

Commit cb64d0b

Browse files
committed
docs: 実装計画を改善 - respond_toと三項演算子を活用
主な改善点: - respond_toメソッドを使用してRailsの標準パターンに準拠 - 三項演算子でformat.json/csvのコードを簡潔に記述 - render_yearly_stats内の重複したパラメータチェックを削除 - より読みやすく保守しやすいコード構造に改善
1 parent 6ab4a24 commit cb64d0b

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

docs/plan_download_yearly_stats.md

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,7 @@ class DojosController < ApplicationController
6161
@selected_year = year
6262
end_of_year = Time.zone.local(@selected_year).end_of_year
6363

64-
# CSV/JSON形式の場合は統計データを返す
65-
if request.format.csv? || request.format.json?
66-
return render_yearly_stats
67-
end
68-
69-
# HTML形式の場合は、その年末時点でアクティブだった道場を表示
64+
# その年末時点でアクティブだった道場を取得
7065
@dojos = []
7166
Dojo.active_at(end_of_year).includes(:prefecture).order(order: :asc).each do |dojo|
7267
@dojos << {
@@ -85,33 +80,33 @@ class DojosController < ApplicationController
8580

8681
@page_title = "#{@selected_year}年末時点のCoderDojo一覧"
8782
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+
}
9298
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-
}
10999
end
110100

101+
# respond_toで形式ごとに処理を分岐
111102
respond_to do |format|
112103
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
115110
end
116111
end
117112

@@ -126,14 +121,7 @@ class DojosController < ApplicationController
126121
@period_end = Date.current.year
127122

128123
# 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アクションで設定済み
137125
period = Time.zone.local(@selected_year).beginning_of_year..Time.zone.local(@selected_year).end_of_year
138126
@stat = Stat.new(period)
139127
@yearly_data = prepare_single_year_data(@stat, @selected_year)
@@ -146,6 +134,7 @@ class DojosController < ApplicationController
146134
filename_suffix = 'all'
147135
end
148136

137+
# CSVまたはJSONとして返す
149138
respond_to do |format|
150139
format.csv do
151140
send_data render_to_string(template: 'dojos/yearly_stats'),

0 commit comments

Comments
 (0)