Skip to content

Commit 37d6f87

Browse files
committed
refactor: is_activeカラムの参照をinactivated_atベースに変更
実装内容: - Dojoモデル: active/inactiveスコープの内部実装を変更 - order_by_active_statusスコープを追加して可読性向上 - sync_active_status関連コードを削除(移行期間終了) - reactivate!メソッドからis_active更新を削除 - コントローラー: ソート条件を新しいスコープに変更 - Rakeタスク: is_active設定行を削除 これにより既存のインターフェースを維持しながら、内部実装を inactivated_atベースに統一。is_activeカラム削除の準備完了。 refs #1734
1 parent a778eeb commit 37d6f87

File tree

3 files changed

+10
-23
lines changed

3 files changed

+10
-23
lines changed

app/controllers/dojos_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ def index
2828
end
2929

3030
@dojos = []
31-
dojos_scope.includes(:prefecture).order(is_active: :desc, order: :asc).each do |dojo|
31+
dojos_scope.includes(:prefecture).order_by_active_status.order(order: :asc).each do |dojo|
3232
# 年が選択されている場合は、その年末時点でのアクティブ状態を判定
3333
# 選択されていない場合は、現在の is_active を使用
3434
is_active_at_selected_time = if @selected_year
3535
# その年末時点でアクティブだったかを判定
3636
# inactivated_at が nil(まだアクティブ)または選択年より後に非アクティブ化
3737
dojo.inactivated_at.nil? || dojo.inactivated_at > Time.zone.local(@selected_year).end_of_year
3838
else
39-
dojo.is_active
39+
dojo.active?
4040
end
4141

4242
@dojos << {

app/models/dojo.rb

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ class Dojo < ApplicationRecord
1414
before_save { self.email = self.email.downcase }
1515

1616
scope :default_order, -> { order(prefecture_id: :asc, order: :asc) }
17-
scope :active, -> { where(is_active: true ) }
18-
scope :inactive, -> { where(is_active: false) }
17+
scope :active, -> { where(inactivated_at: nil) }
18+
scope :inactive, -> { where.not(inactivated_at: nil) }
19+
20+
# ソート用スコープ: アクティブな道場を先に表示
21+
scope :order_by_active_status, -> {
22+
order(Arel.sql('CASE WHEN inactivated_at IS NULL THEN 0 ELSE 1 END'))
23+
}
1924

2025
# 新しいスコープ: 特定の日時点でアクティブだったDojoを取得
2126
scope :active_at, ->(date) {
@@ -103,27 +108,10 @@ def reactivate!
103108
end
104109
end
105110

106-
update!(
107-
is_active: true,
108-
inactivated_at: nil
109-
)
111+
update!(inactivated_at: nil)
110112
end
111113

112-
# is_activeとinactivated_atの同期(移行期間中)
113-
before_save :sync_active_status
114-
115114
private
116-
117-
def sync_active_status
118-
if is_active_changed?
119-
if is_active == false && inactivated_at.nil?
120-
self.inactivated_at = Time.current
121-
elsif is_active == true && inactivated_at.present?
122-
# is_activeがtrueに変更された場合、inactivated_atをnilに
123-
self.inactivated_at = nil
124-
end
125-
end
126-
end
127115

128116
# Now 6+ tags are available since this PR:
129117
# https://github.com/coderdojo-japan/coderdojo.jp/pull/1697

lib/tasks/dojos.rake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ namespace :dojos do
4444
d.url = dojo['url']
4545
d.prefecture_id = dojo['prefecture_id']
4646
d.order = dojo['order'] || search_order_number_by(dojo['name'])
47-
d.is_active = dojo['is_active'].nil? ? true : dojo['is_active']
4847
d.is_private = dojo['is_private'].nil? ? false : dojo['is_private']
4948
d.inactivated_at = dojo['inactivated_at'] ? Time.zone.parse(dojo['inactivated_at']) : nil
5049
d.created_at = d.new_record? ? Time.zone.now : dojo['created_at'] || d.created_at

0 commit comments

Comments
 (0)