Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
34b5ab7
docs: inactivated_at カラム追加の詳細な実装計画を作成
yasulab Aug 5, 2025
fdd31df
docs: `inactivated_at` 実装前の道場統計を記録:#1726 マージ後の比較用
yasulab Aug 6, 2025
3c24c7b
feat: Dojoモデルに inactivated_at カラムを追加
yasulab Aug 6, 2025
92f35c9
docs: YAMLマスターデータを考慮した実装計画に更新
yasulab Aug 6, 2025
d16ae98
docs: Phase 2の進捗を反映(YAMLサポート実装準備)
yasulab Aug 6, 2025
11c3d41
feat: YAMLファイルサポートと統計ロジックを実装
yasulab Aug 7, 2025
03997a9
refactor: Git履歴抽出タスクを1つに統合して冪等性を実装
yasulab Aug 7, 2025
1edc62d
chore: マイグレーション実行後のschema.rbを更新
yasulab Aug 7, 2025
fb7a43f
docs: 実装計画を更新 - Phase 2 完了状況を反映
yasulab Aug 7, 2025
6011ec4
docs: Opus 4.1レビューによる実装計画の改善と統合
yasulab Aug 7, 2025
047db74
feat: Git履歴からinactivated_at日付を抽出するRakeタスク実装
yasulab Aug 7, 2025
884afec
test: 全ての非アクティブDojoがinactivated_atを持つことを検証するテスト追加
yasulab Aug 7, 2025
2832230
fix: Rakeタスクで行番号がずれるバグを修正
yasulab Aug 7, 2025
5a548c7
data: 全124個の非アクティブDojoにinactivated_atを追加
yasulab Aug 7, 2025
38ff377
test: inactivated_atを持つDojoが必ずis_activeカラムを持つことを確認するテストを追加
yasulab Aug 7, 2025
521cce6
feat: 統計ロジックを更新して過去の活動履歴を正確に反映
yasulab Aug 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions app/models/dojo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class Dojo < ApplicationRecord
scope :default_order, -> { order(prefecture_id: :asc, order: :asc) }
scope :active, -> { where(is_active: true ) }
scope :inactive, -> { where(is_active: false) }

# 新しいスコープ: 特定の日時点でアクティブだったDojoを取得
scope :active_at, ->(date) {
where('created_at <= ?', date)
.where('inactivated_at IS NULL OR inactivated_at > ?', date)
}

validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: false
Expand Down Expand Up @@ -74,8 +80,50 @@ def annual_count(period)
]
end
end

# インスタンスメソッド
def active?
inactivated_at.nil?
end

def active_at?(date)
created_at <= date && (inactivated_at.nil? || inactivated_at > date)
end

# 再活性化メソッド
def reactivate!
if inactivated_at.present?
# 非活動期間を note に記録
inactive_period = "#{inactivated_at.strftime('%Y-%m-%d')}#{Date.today}"

if note.present?
self.note += "\n非活動期間: #{inactive_period}"
else
self.note = "非活動期間: #{inactive_period}"
end
end

update!(
is_active: true,
inactivated_at: nil
)
end

# is_activeとinactivated_atの同期(移行期間中)
before_save :sync_active_status

private

def sync_active_status
if is_active_changed?
if is_active == false && inactivated_at.nil?
self.inactivated_at = Time.current
elsif is_active == true && inactivated_at.present?
# is_activeがtrueに変更された場合、inactivated_atをnilに
self.inactivated_at = nil
end
end
end

# Now 6+ tags are available since this PR:
# https://github.com/coderdojo-japan/coderdojo.jp/pull/1697
Expand Down
17 changes: 14 additions & 3 deletions app/models/high_charts_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,20 @@ def build_annual_participants(source, lang = 'ja')
private

def annual_chart_data_from(source)
years = source.map(&:first)
increase_nums = source.map(&:last)
cumulative_sums = increase_nums.size.times.map {|i| increase_nums[0..i].sum }
# sourceがハッシュの場合は配列に変換
source_array = source.is_a?(Hash) ? source.to_a : source

years = source_array.map(&:first)
counts = source_array.map(&:last)

# 増加数を計算(前年との差分)
increase_nums = counts.each_with_index.map do |count, i|
i == 0 ? count : count - counts[i - 1]
end

# annual_dojos_with_historical_dataからの値は既にその時点での総数
# (累積値として扱う)
cumulative_sums = counts

{
years: years,
Expand Down
15 changes: 13 additions & 2 deletions app/models/stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,19 @@ def annual_sum_of_participants
end

def annual_dojos_chart(lang = 'ja')
# MEMO: トップページの道場数と一致するように Active Dojo を集計対象としている
HighChartsBuilder.build_annual_dojos(Dojo.active.annual_count(@period), lang)
# 各年末時点でアクティブだったDojoを集計(過去の非アクティブDojoも含む)
# YAMLマスターデータには既にinactivated_atが含まれているため、常にこの方式を使用
HighChartsBuilder.build_annual_dojos(annual_dojos_with_historical_data, lang)
end

# 各年末時点でアクティブだったDojo数を集計(過去の非アクティブDojoも含む)
def annual_dojos_with_historical_data
(@period.first.year..@period.last.year).each_with_object({}) do |year, hash|
end_of_year = Time.zone.local(year).end_of_year
# その年の終わりにアクティブだったDojoの数を集計
count = Dojo.active_at(end_of_year).sum(:counter)
hash[year.to_s] = count
end
end

def annual_event_histories_chart(lang = 'ja')
Expand Down
Loading