Skip to content

Commit 86e6f5e

Browse files
authored
Merge pull request #1726 from coderdojo-japan/add-inactivated-at-to-dojo
Add `inactivated_at` to `Dojo` model to replace `is_active` boolean column
2 parents cd40f76 + 521cce6 commit 86e6f5e

13 files changed

+1590
-21
lines changed

app/models/dojo.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class Dojo < ApplicationRecord
1616
scope :default_order, -> { order(prefecture_id: :asc, order: :asc) }
1717
scope :active, -> { where(is_active: true ) }
1818
scope :inactive, -> { where(is_active: false) }
19+
20+
# 新しいスコープ: 特定の日時点でアクティブだったDojoを取得
21+
scope :active_at, ->(date) {
22+
where('created_at <= ?', date)
23+
.where('inactivated_at IS NULL OR inactivated_at > ?', date)
24+
}
1925

2026
validates :name, presence: true, length: { maximum: 50 }
2127
validates :email, presence: false
@@ -74,8 +80,50 @@ def annual_count(period)
7480
]
7581
end
7682
end
83+
84+
# インスタンスメソッド
85+
def active?
86+
inactivated_at.nil?
87+
end
88+
89+
def active_at?(date)
90+
created_at <= date && (inactivated_at.nil? || inactivated_at > date)
91+
end
92+
93+
# 再活性化メソッド
94+
def reactivate!
95+
if inactivated_at.present?
96+
# 非活動期間を note に記録
97+
inactive_period = "#{inactivated_at.strftime('%Y-%m-%d')}#{Date.today}"
98+
99+
if note.present?
100+
self.note += "\n非活動期間: #{inactive_period}"
101+
else
102+
self.note = "非活動期間: #{inactive_period}"
103+
end
104+
end
105+
106+
update!(
107+
is_active: true,
108+
inactivated_at: nil
109+
)
110+
end
111+
112+
# is_activeとinactivated_atの同期(移行期間中)
113+
before_save :sync_active_status
77114

78115
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
79127

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

app/models/high_charts_builder.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,20 @@ def build_annual_participants(source, lang = 'ja')
6565
private
6666

6767
def annual_chart_data_from(source)
68-
years = source.map(&:first)
69-
increase_nums = source.map(&:last)
70-
cumulative_sums = increase_nums.size.times.map {|i| increase_nums[0..i].sum }
68+
# sourceがハッシュの場合は配列に変換
69+
source_array = source.is_a?(Hash) ? source.to_a : source
70+
71+
years = source_array.map(&:first)
72+
counts = source_array.map(&:last)
73+
74+
# 増加数を計算(前年との差分)
75+
increase_nums = counts.each_with_index.map do |count, i|
76+
i == 0 ? count : count - counts[i - 1]
77+
end
78+
79+
# annual_dojos_with_historical_dataからの値は既にその時点での総数
80+
# (累積値として扱う)
81+
cumulative_sums = counts
7182

7283
{
7384
years: years,

app/models/stat.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,19 @@ def annual_sum_of_participants
3232
end
3333

3434
def annual_dojos_chart(lang = 'ja')
35-
# MEMO: トップページの道場数と一致するように Active Dojo を集計対象としている
36-
HighChartsBuilder.build_annual_dojos(Dojo.active.annual_count(@period), lang)
35+
# 各年末時点でアクティブだったDojoを集計(過去の非アクティブDojoも含む)
36+
# YAMLマスターデータには既にinactivated_atが含まれているため、常にこの方式を使用
37+
HighChartsBuilder.build_annual_dojos(annual_dojos_with_historical_data, lang)
38+
end
39+
40+
# 各年末時点でアクティブだったDojo数を集計(過去の非アクティブDojoも含む)
41+
def annual_dojos_with_historical_data
42+
(@period.first.year..@period.last.year).each_with_object({}) do |year, hash|
43+
end_of_year = Time.zone.local(year).end_of_year
44+
# その年の終わりにアクティブだったDojoの数を集計
45+
count = Dojo.active_at(end_of_year).sum(:counter)
46+
hash[year.to_s] = count
47+
end
3748
end
3849

3950
def annual_event_histories_chart(lang = 'ja')

0 commit comments

Comments
 (0)