Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion app/controllers/regular_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def create
set_wip
if @regular_event.save
update_published_at
Organizer.create(user_id: current_user.id, regular_event_id: @regular_event.id)
RegularEventOrganizer.create(user_id: current_user.id, regular_event_id: @regular_event.id)
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

git ls-files app/controllers/regular_events_controller.rb app/models/regular_event*.rb

Repository: fjordllc/bootcamp

Length of output: 343


🏁 Script executed:

head -50 app/controllers/regular_events_controller.rb

Repository: fjordllc/bootcamp

Length of output: 1544


🏁 Script executed:

cat -n app/controllers/regular_events_controller.rb | sed -n '20,45p'

Repository: fjordllc/bootcamp

Length of output: 903


🏁 Script executed:

fd -t f "regular_event" app/models/ | head -20

Repository: fjordllc/bootcamp

Length of output: 298


🏁 Script executed:

find app/models -name "*regular_event*" -o -name "*organizer*" | head -20

Repository: fjordllc/bootcamp

Length of output: 361


🏁 Script executed:

cat -n app/models/regular_event_organizer.rb

Repository: fjordllc/bootcamp

Length of output: 516


🏁 Script executed:

cat -n app/models/regular_event.rb | head -80

Repository: fjordllc/bootcamp

Length of output: 3139


🏁 Script executed:

rg -A 3 "class RegularEventOrganizer" app/models/

Repository: fjordllc/bootcamp

Length of output: 709


🏁 Script executed:

rg "EventOrganizer\." app/controllers/ -A 2 -B 2

Repository: fjordllc/bootcamp

Length of output: 582


🏁 Script executed:

rg "\.create\(" app/controllers/regular_events_controller.rb

Repository: fjordllc/bootcamp

Length of output: 158


🏁 Script executed:

rg "find_or_create_by" app/ --include="*.rb" | head -20

Repository: fjordllc/bootcamp

Length of output: 502


🏁 Script executed:

rg "create!" app/controllers/ -A 1 -B 1 | head -30

Repository: fjordllc/bootcamp

Length of output: 761


🏁 Script executed:

rg -B 5 -A 5 "RegularEventOrganizer" app/controllers/

Repository: fjordllc/bootcamp

Length of output: 1017


🏁 Script executed:

rg "find_or_create_by!" app/controllers/ -B 2 -A 2 | head -40

Repository: fjordllc/bootcamp

Length of output: 43


🏁 Script executed:

rg "\.create\!" app/models/regular_event.rb -B 2 -A 2

Repository: fjordllc/bootcamp

Length of output: 145


🏁 Script executed:

rg "create!" app/models/regular_event.rb -B 2 -A 2

Repository: fjordllc/bootcamp

Length of output: 43


🏁 Script executed:

rg "transaction" app/controllers/regular_events_controller.rb

Repository: fjordllc/bootcamp

Length of output: 43


🏁 Script executed:

cat -n app/models/regular_event.rb | sed -n '140,200p'

Repository: fjordllc/bootcamp

Length of output: 2223


🏁 Script executed:

rg "regular_event_organizers" app/models/regular_event.rb -B 2 -A 2

Repository: fjordllc/bootcamp

Length of output: 999


🏁 Script executed:

rg "find_or_create_by" app/ -B 1 -A 1 | head -40

Repository: fjordllc/bootcamp

Length of output: 569


🏁 Script executed:

cat -n app/controllers/regular_events_controller.rb | sed -n '26,40p'

Repository: fjordllc/bootcamp

Length of output: 655


🏁 Script executed:

rg "set_all_user_participants_and_watchers" app/controllers/regular_events_controller.rb -A 10

Repository: fjordllc/bootcamp

Length of output: 865


🏁 Script executed:

rg "find_or_create_by" app/models/ -B 1 -A 3 | head -50

Repository: fjordllc/bootcamp

Length of output: 818


🏁 Script executed:

rg "belongs_to :user" app/models/regular_event_organizer.rb -B 1 -A 5

Repository: fjordllc/bootcamp

Length of output: 201


主催者レコード作成失敗が握りつぶされます。

create メソッドは検証失敗時に例外を発生させず、戻り値も確認していないため、作成失敗時でも成功フローが続行されます。主催者関係が紐付けられず、イベントが不完全な状態になるリスクがあります。

修正案
-      RegularEventOrganizer.create(user_id: current_user.id, regular_event_id: `@regular_event.id`)
+      RegularEventOrganizer.find_or_create_by!(user: current_user, regular_event: `@regular_event`)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
RegularEventOrganizer.create(user_id: current_user.id, regular_event_id: @regular_event.id)
RegularEventOrganizer.find_or_create_by!(user: current_user, regular_event: `@regular_event`)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/controllers/regular_events_controller.rb` at line 32,
RegularEventOrganizer.create is called without checking its result so
organizer-creation failures are swallowed; replace or guard this call
(RegularEventOrganizer.create) so failures are handled: either use
RegularEventOrganizer.create! inside the surrounding transaction/rescue block to
raise on validation errors, or check the returned record (e.g. created =
RegularEventOrganizer.create(...); unless created.persisted? then
handle/rollback and render or raise) referencing current_user and `@regular_event`
to locate the call, and ensure the controller responds with an error (or
rollbacks the transaction) when organizer creation fails.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既存の定期イベントに自動で作成者を主催者を作成する機能のバグなので別Issueとして対応いたします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yokomaru さん、承知いたしました。PRのスコープを維持し、別Issueで対応されるのは適切な判断だと思います。

この問題(RegularEventOrganizerの作成失敗時のエラーハンドリング)について、GitHubのIssueを作成しますか?必要であれば、適切なタイトルと説明文を付けてIssueを作成させていただきます。


🧠 Learnings used
Learnt from: yokomaru
Repo: fjordllc/bootcamp PR: 9505
File: test/fixtures/regular_events.yml:253-262
Timestamp: 2026-01-15T06:30:59.321Z
Learning: fjordllc/bootcamp の RegularEvent モデルにおいて、`user` フィールドは「作成者」を意味し、主催者は別途 Organizer モデルで管理されている。通常は作成者 = 主催者のパターンが多いが、システム上は分離されている設計である。

Learnt from: jun-kondo
Repo: fjordllc/bootcamp PR: 0
File: :0-0
Timestamp: 2025-07-23T21:11:21.826Z
Learning: fjordllc/bootcampプロジェクトの`app/models/inquiry_notifier.rb`において、`NOTIFICATION_SENDER_NAME`で指定される管理者ユーザーは必ず存在することがシステムの前提条件として保証されているため、senderがnilになることは想定されておらず、エラーハンドリングは不要である。

Learnt from: jun-kondo
Repo: fjordllc/bootcamp PR: 8977
File: app/controllers/reports_controller.rb:63-63
Timestamp: 2025-07-23T20:31:13.856Z
Learning: fjordllc/bootcampプロジェクトの`app/controllers/reports_controller.rb`において、`create`と`update`アクションは両方とも`report.save_uniquely`を使用し、同じ`:report_save`イベントと`'report.save'`イベントを発行する。これは両方とも本質的に「レポートの保存」操作であり、作成と更新を区別する必要がないためである。

Learnt from: komagata
Repo: fjordllc/bootcamp PR: 9101
File: app/models/graduation_notifier.rb:23-28
Timestamp: 2025-09-12T21:16:47.639Z
Learning: Rails upgrade PRにおいて、configuration systemの変更やwebhook設定の改善提案も、アップグレードに直接関連しない場合は別PRで対応すべきである。PRのスコープ維持が重要。

Learnt from: komagata
Repo: fjordllc/bootcamp PR: 9101
File: app/notifiers/discord_notifier.rb:131-135
Timestamp: 2025-09-12T21:18:00.834Z
Learning: Rails アップグレードPRにおいて、product_review_not_completed メソッドの webhook URL 設定の shared フォールバック追加も、設定システム全体の変更として別PRで対応すべきである。

Learnt from: mousu-a
Repo: fjordllc/bootcamp PR: 8566
File: app/controllers/pair_works_controller.rb:10-18
Timestamp: 2026-01-08T21:12:18.350Z
Learning: In fjordllc/bootcamp, when using ActiveRecord.order to specify ascending order, omit the explicit :asc and rely on Rails' default ASC behavior. Prefer .order(:published_at) over .order(published_at: :asc). If you need a non-default order (e.g., DESC), specify it explicitly. This guideline applies to Ruby/Rails controller and model files, especially in app/controllers and similar ActiveRecord usage sites.

Learnt from: mousu-a
Repo: fjordllc/bootcamp PR: 8566
File: app/helpers/pair_work_helper.rb:31-33
Timestamp: 2026-01-15T03:20:24.643Z
Learning: In this Ruby project, prefer Numeric#hours (plural) for adding time durations. Use the plural hours method when converting integers to durations (e.g., hour_count.hours). Apply this consistently across the codebase to ensure uniform time calculations and Rails-style duration handling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このリポジトリに登録してあるバグレポートのフォーマットに沿って作成お願いします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

@yokomaru さん、承知いたしました。リポジトリのバグレポートフォーマットを確認して、Issue を作成させていただきます。


🏁 Script executed:

#!/bin/bash
# Find bug report template in the repository
fd -t f "bug" .github/ 2>/dev/null || find .github -name "*bug*" -o -name "*issue*" 2>/dev/null | head -10

Length of output: 0


🏁 Script executed:

#!/bin/bash
# Check common locations for issue templates
cat .github/ISSUE_TEMPLATE/bug_report.md 2>/dev/null || \
cat .github/ISSUE_TEMPLATE/bug.md 2>/dev/null || \
cat .github/ISSUE_TEMPLATE.md 2>/dev/null || \
ls -la .github/ISSUE_TEMPLATE/ 2>/dev/null || \
echo "Template not found, checking for any markdown files in .github"
find .github -name "*.md" -o -name "*.yml" -o -name "*.yaml" | head -20

Length of output: 675


🏁 Script executed:

#!/bin/bash
# Read all markdown files in ISSUE_TEMPLATE directory
echo "=== First template file ==="
cat .github/ISSUE_TEMPLATE/*.md | head -100

Length of output: 541


🏁 Script executed:

#!/bin/bash
# List files with full names
ls -1 .github/ISSUE_TEMPLATE/

Length of output: 16


🏁 Script executed:

#!/bin/bash
# Read each file separately
for file in .github/ISSUE_TEMPLATE/*.md; do
  echo "=== File: $file ==="
  cat "$file"
  echo ""
  echo "---"
  echo ""
done

Length of output: 616


🏁 Script executed:

#!/bin/bash
# Create GitHub issue using the bug report template format
gh issue create \
  --title "RegularEventOrganizer作成失敗時のエラーハンドリング不足" \
  --label "バグ" \
  --assignee "yokomaru" \
  --body "## 概要

\`app/controllers/regular_events_controller.rb\` の \`create\` アクション内で、\`RegularEventOrganizer.create\` が呼ばれていますが、作成失敗時のエラーハンドリングがないため、主催者レコードの作成に失敗しても処理が継続され、不完全な状態のイベントが作成される可能性があります。

## 再現手順

1. 定期イベントの新規作成画面にアクセス。
2. 必要な情報を入力して「作成」をクリック。
3. \`RegularEventOrganizer\` のバリデーションが失敗する条件(例: データベース制約違反など)が発生した場合、エラーが握りつぶされ、主催者が紐付けられていないイベントが作成される。

## 期待される振る舞い

\`RegularEventOrganizer\` の作成に失敗した場合は、適切なエラーハンドリング(例外の発生、トランザクションのロールバック、エラーメッセージの表示など)が行われるべきです。

## スクリーンショット

該当なし

## 環境

該当なし(コードレベルの問題)

## 関連Issue

- 関連PR: `#9715`
- コメント: https://github.com/fjordllc/bootcamp/pull/9715#discussion_r2876736798
- 報告者: `@yokomaru`"

Length of output: 88


🏁 Script executed:

#!/bin/bash
# Create GitHub issue using the bug report template format
gh issue create \
  --title "RegularEventOrganizer作成失敗時のエラーハンドリング不足" \
  --label "バグ" \
  --assignee "yokomaru" \
  --body "## 概要

\`app/controllers/regular_events_controller.rb\` の \`create\` アクション内で、\`RegularEventOrganizer.create\` が呼ばれていますが、作成失敗時のエラーハンドリングがないため、主催者レコードの作成に失敗しても処理が継続され、不完全な状態のイベントが作成される可能性があります。

## 再現手順

1. 定期イベントの新規作成画面にアクセス。
2. 必要な情報を入力して「作成」をクリック。
3. \`RegularEventOrganizer\` のバリデーションが失敗する条件(例: データベース制約違反など)が発生した場合、エラーが握りつぶされ、主催者が紐付けられていないイベントが作成される。

## 期待される振る舞い

\`RegularEventOrganizer\` の作成に失敗した場合は、適切なエラーハンドリング(例外の発生、トランザクションのロールバック、エラーメッセージの表示など)が行われるべきです。

## スクリーンショット

該当なし

## 環境

該当なし(コードレベルの問題)

## 関連Issue

- 関連PR: `#9715`
- コメント: https://github.com/fjordllc/bootcamp/pull/9715#discussion_r2876736798
- 報告者: `@yokomaru`"

Length of output: 48


@yokomaru さん、バグレポートのフォーマットに沿って Issue を作成いたしました。

作成された Issue:
#9716

「RegularEventOrganizer作成失敗時のエラーハンドリング不足」というタイトルで、以下の内容を含めています:

  • 概要: RegularEventOrganizer.create のエラーハンドリング不足の説明
  • 再現手順
  • 期待される振る舞い
  • 関連Issue欄にPR #9715とコメントURLを記載
  • ラベル: バグ
  • 担当者: yokomaru さん

ご確認よろしくお願いいたします。


🧠 Learnings used
Learnt from: yokomaru
Repo: fjordllc/bootcamp PR: 9505
File: test/fixtures/regular_events.yml:253-262
Timestamp: 2026-01-15T06:30:59.321Z
Learning: fjordllc/bootcamp の RegularEvent モデルにおいて、`user` フィールドは「作成者」を意味し、主催者は別途 Organizer モデルで管理されている。通常は作成者 = 主催者のパターンが多いが、システム上は分離されている設計である。

Learnt from: jun-kondo
Repo: fjordllc/bootcamp PR: 8977
File: app/controllers/reports_controller.rb:63-63
Timestamp: 2025-07-23T20:31:13.856Z
Learning: fjordllc/bootcampプロジェクトの`app/controllers/reports_controller.rb`において、`create`と`update`アクションは両方とも`report.save_uniquely`を使用し、同じ`:report_save`イベントと`'report.save'`イベントを発行する。これは両方とも本質的に「レポートの保存」操作であり、作成と更新を区別する必要がないためである。

Learnt from: komagata
Repo: fjordllc/bootcamp PR: 9101
File: app/notifiers/discord_notifier.rb:131-135
Timestamp: 2025-09-12T21:18:00.834Z
Learning: Rails アップグレードPRにおいて、product_review_not_completed メソッドの webhook URL 設定の shared フォールバック追加も、設定システム全体の変更として別PRで対応すべきである。

Learnt from: jun-kondo
Repo: fjordllc/bootcamp PR: 0
File: :0-0
Timestamp: 2025-07-23T21:11:21.826Z
Learning: fjordllc/bootcampプロジェクトの`app/models/inquiry_notifier.rb`において、`NOTIFICATION_SENDER_NAME`で指定される管理者ユーザーは必ず存在することがシステムの前提条件として保証されているため、senderがnilになることは想定されておらず、エラーハンドリングは不要である。

Learnt from: mousu-a
Repo: fjordllc/bootcamp PR: 8566
File: db/fixtures/pair_works.yml:19-20
Timestamp: 2026-01-15T03:21:32.929Z
Learning: fjordllc/bootcampの db/fixtures/pair_works.yml において、pair_work1とpair_work2は特定の状態(解決済み・募集中)の動作確認用フィクスチャであり、意図的に過去の静的な日付(例: Time.zone.parse('2025-01-01 00:00:00'))を使用している。これはテストの再現性を保証するための設計であり、問題ではない。

Learnt from: matuaya
Repo: fjordllc/bootcamp PR: 9108
File: app/models/after_user_retirement.rb:0-0
Timestamp: 2025-09-01T22:31:57.345Z
Learning: fjordllc/bootcampプロジェクトにおいて、`Newspaper.publish(:retirement_create, { user: user })`はUnfinishedDataDestroyer(未チェックProduct・WIP Report削除、career_pathリセット)とTimesChannelDestroyer(Discord分報チャンネル削除)による重要なデータクリーンアップ処理を実行するため、退会のトリガーが何であろうと(admin、hibernation、user)必ず実行する必要がある。

Learnt from: komagata
Repo: fjordllc/bootcamp PR: 9101
File: app/models/graduation_notifier.rb:23-28
Timestamp: 2025-09-12T21:16:47.639Z
Learning: Rails upgrade PRにおいて、configuration systemの変更やwebhook設定の改善提案も、アップグレードに直接関連しない場合は別PRで対応すべきである。PRのスコープ維持が重要。

Learnt from: matuaya
Repo: fjordllc/bootcamp PR: 9108
File: app/models/after_user_retirement.rb:0-0
Timestamp: 2025-09-01T22:31:57.345Z
Learning: fjordllc/bootcampプロジェクトにおいて、`Newspaper.publish(:retirement_create, { user: user })`は通知処理ではなく、UnfinishedDataDestroyerやTimesChannelDestroyer等による重要なデータクリーンアップ処理を実行するため、管理者による退会(triggered_by: 'admin')の場合でも必ず実行する必要がある。

Learnt from: mousu-a
Repo: fjordllc/bootcamp PR: 8566
File: app/controllers/pair_works_controller.rb:10-18
Timestamp: 2026-01-08T21:12:18.350Z
Learning: In fjordllc/bootcamp, when using ActiveRecord.order to specify ascending order, omit the explicit :asc and rely on Rails' default ASC behavior. Prefer .order(:published_at) over .order(published_at: :asc). If you need a non-default order (e.g., DESC), specify it explicitly. This guideline applies to Ruby/Rails controller and model files, especially in app/controllers and similar ActiveRecord usage sites.

Learnt from: mousu-a
Repo: fjordllc/bootcamp PR: 8566
File: app/helpers/pair_work_helper.rb:31-33
Timestamp: 2026-01-15T03:20:24.643Z
Learning: In this Ruby project, prefer Numeric#hours (plural) for adding time durations. Use the plural hours method when converting integers to durations (e.g., hour_count.hours). Apply this consistently across the codebase to ensure uniform time calculations and Rails-style duration handling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!問題ありませんでした。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yokomaru さん、どういたしまして!今後の対応、よろしくお願いいたします。


🧠 Learnings used
Learnt from: yokomaru
Repo: fjordllc/bootcamp PR: 9505
File: test/fixtures/regular_events.yml:253-262
Timestamp: 2026-01-15T06:30:59.321Z
Learning: fjordllc/bootcamp の RegularEvent モデルにおいて、`user` フィールドは「作成者」を意味し、主催者は別途 Organizer モデルで管理されている。通常は作成者 = 主催者のパターンが多いが、システム上は分離されている設計である。

Learnt from: komagata
Repo: fjordllc/bootcamp PR: 9101
File: app/models/graduation_notifier.rb:23-28
Timestamp: 2025-09-12T21:16:47.639Z
Learning: Rails upgrade PRにおいて、configuration systemの変更やwebhook設定の改善提案も、アップグレードに直接関連しない場合は別PRで対応すべきである。PRのスコープ維持が重要。

Learnt from: komagata
Repo: fjordllc/bootcamp PR: 9101
File: app/notifiers/discord_notifier.rb:131-135
Timestamp: 2025-09-12T21:18:00.834Z
Learning: Rails アップグレードPRにおいて、product_review_not_completed メソッドの webhook URL 設定の shared フォールバック追加も、設定システム全体の変更として別PRで対応すべきである。

Learnt from: mousu-a
Repo: fjordllc/bootcamp PR: 8566
File: app/controllers/pair_works_controller.rb:10-18
Timestamp: 2026-01-08T21:12:18.350Z
Learning: In fjordllc/bootcamp, when using ActiveRecord.order to specify ascending order, omit the explicit :asc and rely on Rails' default ASC behavior. Prefer .order(:published_at) over .order(published_at: :asc). If you need a non-default order (e.g., DESC), specify it explicitly. This guideline applies to Ruby/Rails controller and model files, especially in app/controllers and similar ActiveRecord usage sites.

Learnt from: mousu-a
Repo: fjordllc/bootcamp PR: 8566
File: app/helpers/pair_work_helper.rb:31-33
Timestamp: 2026-01-15T03:20:24.643Z
Learning: In this Ruby project, prefer Numeric#hours (plural) for adding time durations. Use the plural hours method when converting integers to durations (e.g., hour_count.hours). Apply this consistently across the codebase to ensure uniform time calculations and Rails-style duration handling.

If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

Copy link
Copy Markdown
Contributor

@torinoko torinoko Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NITS]
@yokomaru も触れてた気がするけど、修正時にここの記述も直してよいかもしれませんね。
どっちでもいいけどその方がやっぱり Ruby on Rails っぽいよね。

Suggested change
RegularEventOrganizer.create(user_id: current_user.id, regular_event_id: @regular_event.id)
RegularEventOrganizer.create(user: current_user, regular_event: @regular_event)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

ActiveSupport::Notifications.instrument('regular_event.create', regular_event: @regular_event)
set_all_user_participants_and_watchers
select_redirect_path
Expand Down
9 changes: 5 additions & 4 deletions app/models/regular_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ class RegularEvent < ApplicationRecord # rubocop:disable Metrics/ClassLength

scope :holding, -> { where(finished: false) }
scope :participated_by, ->(user) { where(id: all.filter { |e| e.participated_by?(user) }.map(&:id)) }
scope :organizer_event, ->(user) { joins(:organizers).where(organizers: { user_id: user.id }) }
scope :organizer_event, ->(user) { joins(:regular_event_organizers).where(regular_event_organizers: { user_id: user.id }) }
Copy link
Copy Markdown
Contributor

@torinoko torinoko Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NITS]
ここもかな?(同上)

Suggested change
scope :organizer_event, ->(user) { joins(:regular_event_organizers).where(regular_event_organizers: { user_id: user.id }) }
scope :organizer_event, ->(user) { joins(:regular_event_organizers).where(regular_event_organizers: { user: user }) }

scope :scheduled_on, ->(date) { holding.filter { |event| event.scheduled_on?(date) } }
scope :scheduled_on_without_ended, ->(date) { holding.filter { |event| event.scheduled_on?(date) && !event.ended?(date) } }

belongs_to :user
has_many :organizers, dependent: :destroy
has_many :users, through: :organizers
has_many :regular_event_organizers, dependent: :destroy
has_many :users, through: :regular_event_organizers
has_many :regular_event_repeat_rules, dependent: :destroy
accepts_nested_attributes_for :regular_event_repeat_rules, allow_destroy: true
has_many :regular_event_participations, dependent: :destroy
Expand Down Expand Up @@ -102,6 +102,7 @@ def next_event_date
end

def organizers
# TODO: order内のorganizersをテーブル名を変えた時にregular_event_organizersに変更する
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[LGTM]
TODO 書いているのよいですね ✨

users.preload(avatar_attachment: :blob).order('organizers.created_at')
end

Expand All @@ -122,7 +123,7 @@ def assign_admin_as_organizer_if_none
return if organizers.exists?

admin_user = User.find_by(login_name: User::DEFAULT_REGULAR_EVENT_ORGANIZER)
Organizer.new(user: admin_user, regular_event: self).save if admin_user
RegularEventOrganizer.new(user: admin_user, regular_event: self).save if admin_user
end

def all_scheduled_dates(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

class Organizer < ApplicationRecord
class RegularEventOrganizer < ApplicationRecord
# TODO: テーブル名変えたら消す
self.table_name = 'organizers'

belongs_to :user
belongs_to :regular_event

Expand Down
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class User < ApplicationRecord # rubocop:todo Metrics/ClassLength
has_many :articles, dependent: :destroy
has_many :bookmarks, dependent: :destroy
has_many :regular_events, dependent: :destroy
has_many :organizers, dependent: :destroy
has_many :regular_event_organizers, dependent: :destroy
has_many :hibernations, dependent: :destroy
has_many :authored_books, dependent: :destroy
accepts_nested_attributes_for :authored_books, allow_destroy: true
Expand Down Expand Up @@ -193,7 +193,7 @@ class User < ApplicationRecord # rubocop:todo Metrics/ClassLength
source: :follower

has_many :organize_regular_events,
through: :organizers,
through: :regular_event_organizers,
source: :regular_event

has_many :participate_regular_events,
Expand Down Expand Up @@ -890,7 +890,7 @@ def cancel_participation_from_regular_events
end

def delete_and_assign_new_organizer
organizers.each(&:delete_and_assign_new)
regular_event_organizers.each(&:delete_and_assign_new)
end

def scheduled_retire_at
Expand Down
4 changes: 4 additions & 0 deletions db/fixtures/organizers.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# TODO: テーブル名変えたら消す
_fixture:
model_class: RegularEventOrganizer

organizer1:
user: komagata
regular_event: regular_event1
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/organizers.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# TODO: テーブル名変えたら消す
_fixture:
model_class: RegularEventOrganizer

organizer1:
user: komagata
regular_event: regular_event1
Expand Down
16 changes: 0 additions & 16 deletions test/models/organizer_test.rb

This file was deleted.

16 changes: 16 additions & 0 deletions test/models/regular_event_organizers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'test_helper'

class RegularEventOrganizerTest < ActiveSupport::TestCase
test '#delete_and_assign_new' do
organizer = organizers(:organizer12)
event = organizer.regular_event

assert_changes -> { RegularEventOrganizer.where(regular_event: event, user: organizer.user).exists? }, from: true, to: false do
organizer.delete_and_assign_new
end

assert RegularEventOrganizer.where(regular_event: event).exists?
end
end
2 changes: 1 addition & 1 deletion test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ class UserTest < ActiveSupport::TestCase
test '#delete_and_assign_new_organizer' do
user = users(:hajime)

assert_changes -> { Organizer.where(user:).exists? }, from: true, to: false do
assert_changes -> { RegularEventOrganizer.where(user:).exists? }, from: true, to: false do
user.delete_and_assign_new_organizer
end
end
Expand Down