Skip to content

同僚研修生の最新日報取得ロジックのリファクタリングをした#8980

Merged
komagata merged 4 commits intomainfrom
refactor-colleague-trainees-recent-reports-query
Aug 25, 2025
Merged

同僚研修生の最新日報取得ロジックのリファクタリングをした#8980
komagata merged 4 commits intomainfrom
refactor-colleague-trainees-recent-reports-query

Conversation

@hirokiej
Copy link
Copy Markdown
Contributor

@hirokiej hirokiej commented Jul 23, 2025

Issue

概要

HomeControllerにあった同僚研修生の最新日報取得ロジックをrails-patterns gemのQueryオブジェクトを使ってリファクタリングしました。
参考: QueryObject · fjordllc/bootcamp Wiki

変更確認方法

  1. refactor-colleague-trainees-recent-reports-query をローカルに取り込む
git fetch origin refactor-colleague-trainees-recent-reports-query 
git checkout refactor-colleague-trainees-recent-reports-query 
  1. アドバイザーsenpaiでログイン
    ID: senpai
    PASS: testtest
    3.ダッシュボードの下にスクロールをし、「研修生の最新の日報」があることを確認
    4.kensyusenpaiの同僚なので、その日報が降順(新→旧)になっていることを確認。

Screenshot

※内部的なリファクタリングであり、見た目の変更はないためスクリーンショットはありません。

Summary by CodeRabbit

  • リファクタリング

    • ダッシュボードの同僚研修生の日報取得処理をクエリサービスへ切り出しました。
  • 新機能

    • 同僚研修生の最近の日報を取得する専用クエリを追加しました。
  • テスト

    • クエリの振る舞い(対象絞り込み、WIP除外、並び順)を検証するテストを追加しました。
    • テスト用の日報データ(WIP含む)を追加しました。
    • システムテストの検証フローを整理しました。

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jul 23, 2025

Walkthrough

HomeControllerのダッシュボードでの同僚研修生の日報取得ロジックをインラインクエリからクエリオブジェクトColleagueTraineesRecentReportsQueryへ抽出し、関連するテストとフィクスチャを追加・システムテストを一部修正しました。

Changes

Cohort / File(s) Change Summary
HomeControllerのクエリ呼び出しリファクタリング
app/controllers/home_controller.rb
同僚研修生の日報取得処理をインラインクエリからColleagueTraineesRecentReportsQuery.new(current_user).call.limit(10)へ置換
クエリオブジェクト新規追加
app/queries/colleague_trainees_recent_reports_query.rb
ColleagueTraineesRecentReportsQueryクラスを追加。current_userの同僚研修生IDでReportを絞り、WIP除外・降順・アバター有りで取得する
クエリオブジェクトのテスト新規追加
test/queries/colleague_trainees_recent_reports_query_test.rb
同僚研修生の最近の日報取得、WIP除外、報告日の降順を検証するテストを追加
テスト用フィクスチャ追加
test/fixtures/reports.yml
report75(WIPの日報)エントリを追加
システムテスト修正
test/system/training_completion_test.rb
確認ダイアログ処理とアサーションの配置を変更(assert_difference削除、confirm内外の見直し)

Sequence Diagram(s)

sequenceDiagram
    participant Controller as HomeController
    participant Query as ColleagueTraineesRecentReportsQuery
    participant DB as Database

    Controller->>Query: new(current_user).call.limit(10)
    Query->>DB: JOIN users\nWHERE user_id IN (colleague_trainee_ids)\nAND NOT wip\nAND with_avatar\nORDER BY reported_on DESC
    DB-->>Query: Report records
    Query-->>Controller: 結果を返す
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related issues

Suggested reviewers

  • komagata

Poem

うさぎがぴょんとクエリ整理、
森の報告は箱にまとまる。
テストも添えて安心だよ、
ダッシュボードはすっきり晴れ、
🐰✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 RuboCop (1.76.1)
app/controllers/home_controller.rb

rubocop-minitest extension supports plugin, specify plugins: rubocop-minitest instead of require: rubocop-minitest in /.rubocop.yml.
For more information, see https://docs.rubocop.org/rubocop/plugin_migration_guide.html.
rubocop-capybara extension supports plugin, specify plugins: rubocop-capybara instead of require: rubocop-capybara in /.rubocop.yml.
For more information, see https://docs.rubocop.org/rubocop/plugin_migration_guide.html.
Unable to find gem rubocop-fjord; is the gem installed? Gem::MissingSpecError
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:309:in rescue in gem_config_path' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:293:in gem_config_path'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:84:in block (2 levels) in resolve_inheritance_from_gems' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:82:in reverse_each'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:82:in block in resolve_inheritance_from_gems' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:76:in each_pair'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:76:in resolve_inheritance_from_gems' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader.rb:58:in load_file'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_store.rb:29:in options_config=' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:162:in act_on_options'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:47:in block in run' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:82:in profile_if_needed'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:43:in run' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/exe/rubocop:19:in <top (required)>'
/usr/local/bin/rubocop:25:in load' /usr/local/bin/rubocop:25:in

'

app/queries/colleague_trainees_recent_reports_query.rb

rubocop-minitest extension supports plugin, specify plugins: rubocop-minitest instead of require: rubocop-minitest in /.rubocop.yml.
For more information, see https://docs.rubocop.org/rubocop/plugin_migration_guide.html.
rubocop-capybara extension supports plugin, specify plugins: rubocop-capybara instead of require: rubocop-capybara in /.rubocop.yml.
For more information, see https://docs.rubocop.org/rubocop/plugin_migration_guide.html.
Unable to find gem rubocop-fjord; is the gem installed? Gem::MissingSpecError
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:309:in rescue in gem_config_path' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:293:in gem_config_path'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:84:in block (2 levels) in resolve_inheritance_from_gems' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:82:in reverse_each'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:82:in block in resolve_inheritance_from_gems' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:76:in each_pair'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader_resolver.rb:76:in resolve_inheritance_from_gems' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_loader.rb:58:in load_file'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/config_store.rb:29:in options_config=' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:162:in act_on_options'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:47:in block in run' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:82:in profile_if_needed'
/var/lib/gems/3.1.0/gems/rubocop-1.79.2/lib/rubocop/cli.rb:43:in run' /var/lib/gems/3.1.0/gems/rubocop-1.79.2/exe/rubocop:19:in <top (required)>'
/usr/local/bin/rubocop:25:in load' /usr/local/bin/rubocop:25:in

'

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c2fe65d and e2478c2.

📒 Files selected for processing (2)
  • app/controllers/home_controller.rb (1 hunks)
  • app/queries/colleague_trainees_recent_reports_query.rb (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • app/queries/colleague_trainees_recent_reports_query.rb
  • app/controllers/home_controller.rb
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build_and_test
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor-colleague-trainees-recent-reports-query

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@hirokiej hirokiej changed the title 僚研修生の最新日報取得ロジックのリファクタリングをした 同僚研修生の最新日報取得ロジックのリファクタリングをした Jul 23, 2025
@hirokiej hirokiej force-pushed the refactor-colleague-trainees-recent-reports-query branch 3 times, most recently from 0fa74b5 to 6fbe791 Compare July 29, 2025 12:48
@hirokiej hirokiej marked this pull request as ready for review July 29, 2025 12:48
@github-actions github-actions bot requested a review from komagata July 29, 2025 12:48
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
app/controllers/home_controller.rb (1)

49-50: Query objectの使用は適切ですが、冗長なorderingがあります。

複雑なActiveRecordクエリをQuery objectに抽出したのは、コーディングガイドラインに従った良いリファクタリングです。ただし、ColleagueTraineesRecentReportsQuery内で既にrecentスコープを使用してreported_on: :descでソートしているため、50行目の.order(reported_on: :desc)は冗長です。

冗長なorderingを削除することを検討してください:

-    colleague_trainees_reports = ColleagueTraineesRecentReportsQuery.new(current_user:).call
-    @colleague_trainees_recent_reports = colleague_trainees_reports.order(reported_on: :desc).limit(10)
+    @colleague_trainees_recent_reports = ColleagueTraineesRecentReportsQuery.new(current_user:).call.limit(10)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 93c3adb and 6fbe791.

📒 Files selected for processing (5)
  • app/controllers/home_controller.rb (1 hunks)
  • app/models/report.rb (1 hunks)
  • app/queries/colleague_trainees_recent_reports_query.rb (1 hunks)
  • test/fixtures/reports.yml (1 hunks)
  • test/queries/colleague_trainees_recent_reports_query_test.rb (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.rb

⚙️ CodeRabbit Configuration File

**/*.rb: # refactoring

  • まずFat Controllerを避け、次にFat Modelを避ける。
  • Serviceクラスの乱用を避ける。
  • controller concernを作ろうとしたらPORO(Plain Old Ruby Object)やActiveRecordモデルでの実装で代替できないか検討する。

Rails Patterns

  • ViewHelperにメソッドを実装する時にはまずDecoratorパターンを使うことを検討する。(active_decorator gemを導入しているのでそれを使う)
  • 複雑なActiveRecordクエリがあり、再利用できそうな場合はQueryObjectパターンを検討する。(rails-patterns gemを導入しているのでそれのQuery機能を使う)
  • Viewにpartialを作る場合はViewComponentを使うことを検討する。
  • 複数のActiveRecordモデルを操作する1つの責務がある時や外部APIとやりとりする処理がある場合にはInteractorオブジェクトパターンを検討する。(interactor gemを導入しているのでそれを使う)
  • 複数のInteractorを実行するような処理がある場合Organizerオブジェクトパターンを検討する。(interactor gemを導入しており、その中にOrganizerの機能があるので使う)

Files:

  • app/models/report.rb
  • app/controllers/home_controller.rb
  • test/queries/colleague_trainees_recent_reports_query_test.rb
  • app/queries/colleague_trainees_recent_reports_query.rb
test/**/*

⚙️ CodeRabbit Configuration File

test/**/*: # Test

  • TestCase名は英語で書く。
  • どうしても避けられない時以外にsystem testでsleepは使わない。

Unit Test

model, helper, decorator, view_componentについてはメソッドを追加した場合は必ず対応したUnit TestのTestCaseを1つは書く。

Files:

  • test/fixtures/reports.yml
  • test/queries/colleague_trainees_recent_reports_query_test.rb
🧠 Learnings (6)
📓 Common learnings
Learnt from: jun-kondo
PR: fjordllc/bootcamp#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'`イベントを発行する。これは両方とも本質的に「レポートの保存」操作であり、作成と更新を区別する必要がないためである。
app/models/report.rb (1)

Learnt from: jun-kondo
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において、createupdateアクションは両方とも@report.save_uniquelyを使用し、同じ:report_saveイベントと'report.save'イベントを発行する。これは両方とも本質的に「レポートの保存」操作であり、作成と更新を区別する必要がないためである。

test/fixtures/reports.yml (1)

Learnt from: jun-kondo
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において、createupdateアクションは両方とも@report.save_uniquelyを使用し、同じ:report_saveイベントと'report.save'イベントを発行する。これは両方とも本質的に「レポートの保存」操作であり、作成と更新を区別する必要がないためである。

app/controllers/home_controller.rb (1)

Learnt from: jun-kondo
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において、createupdateアクションは両方とも@report.save_uniquelyを使用し、同じ:report_saveイベントと'report.save'イベントを発行する。これは両方とも本質的に「レポートの保存」操作であり、作成と更新を区別する必要がないためである。

test/queries/colleague_trainees_recent_reports_query_test.rb (2)

Learnt from: kitarou888
PR: #8277
File: test/models/searcher_test.rb:326-331
Timestamp: 2025-07-10T12:59:27.807Z
Learning: モデルテストにおいて、1つのクラスメソッドの複数の挙動を検証する場合、機能の異なる側面を同じテストメソッドでテストすることは、包括的なテストとして適切である。特にonly_meのような機能では、異なる検索条件(空文字と具体的な検索語)を使い分けることで、より広範囲な動作保証が可能となる。

Learnt from: reckyy
PR: #8356
File: db/fixtures/users.yml:823-826
Timestamp: 2025-07-15T12:33:04.885Z
Learning: fjordllc/bootcampプロジェクトでは、trainee: trueは「研修に所属していたユーザー」全体を示すフラグであり、研修中のユーザーと研修を終了したユーザーの両方を含む。そのため、trainee: truetraining_completed_atの併存は意図された仕様であり、排他的な関係ではない。

app/queries/colleague_trainees_recent_reports_query.rb (1)

Learnt from: reckyy
PR: #8356
File: db/fixtures/users.yml:823-826
Timestamp: 2025-07-15T12:33:04.885Z
Learning: fjordllc/bootcampプロジェクトでは、trainee: trueは「研修に所属していたユーザー」全体を示すフラグであり、研修中のユーザーと研修を終了したユーザーの両方を含む。そのため、trainee: truetraining_completed_atの併存は意図された仕様であり、排他的な関係ではない。

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build_and_test
🔇 Additional comments (9)
app/models/report.rb (1)

63-63: 新しいscopeの追加が適切です。

recentスコープはreported_onの降順でソートする明確な責務を持ち、新しいQuery objectで適切に使用されています。既存のdefault_orderスコープと一貫した実装になっています。

test/fixtures/reports.yml (1)

354-360: テスト用フィクスチャの追加が適切です。

WIPの日報を除外するロジックをテストするためのフィクスチャとして適切に設計されています。既存のパターンに従った実装です。

test/queries/colleague_trainees_recent_reports_query_test.rb (3)

6-15: 基本的な機能テストが適切に実装されています。

同僚研修生の日報が含まれ、関係のない日報が除外されることを適切にテストしています。フィクスチャの使用も適切です。


17-24: WIP除外のテストが適切です。

新しく追加されたreport75フィクスチャを使用してWIPの日報が除外されることを確認しています。


26-33: ソート順のテストが適切に実装されています。

recentスコープによる降順ソートが正しく機能することを検証しています。日付の配列を比較する方法は明確で理解しやすいです。

app/queries/colleague_trainees_recent_reports_query.rb (4)

3-4: Query objectの基本構造が適切です。

rails-patterns gemのQuery patternを正しく使用し、Reportモデルを対象としています。コーディングガイドラインに従った良い実装です。


8-15: クエリロジックが適切に実装されています。

  • joins(:user)でユーザーテーブルとの結合
  • 同僚研修生IDでのフィルタリング
  • WIPの除外
  • recentスコープの使用
  • パフォーマンスを考慮したeager loading

すべて適切に実装されており、Fat Controllerを避ける良いリファクタリングです。


17-20: カスタムイニシャライザが適切です。

current_userパラメータを必須にし、親クラスの初期化も適切に呼び出しています。


22-24: ヘルパーメソッドが適切に実装されています。

同僚研修生のIDを取得するロジックが明確に分離されており、可読性が向上しています。

@hirokiej hirokiej force-pushed the refactor-colleague-trainees-recent-reports-query branch from 6fbe791 to b857057 Compare July 29, 2025 14:06
@hirokiej
Copy link
Copy Markdown
Contributor Author

パフォーマンステストは #8930 のやり取りでなしという方針となりました。

パフォーマンスはテストしなくて大丈夫です。想定される内容が返ってきてるかだけで良いので assert_equalとかで大丈夫です。
開発環境ではbulletを使ってますし、本番で起きる可能性を残したくないのでstrict_loadingはない方が良いかなと思いました。

@hirokiej hirokiej self-assigned this Jul 29, 2025
@hirokiej hirokiej requested a review from jun-kondo July 29, 2025 14:42
@hirokiej
Copy link
Copy Markdown
Contributor Author

@jun-kondo さん
お疲れ様です!
レビュー依頼です!
こちらお願いしても良いでしょうか🙇?

@jun-kondo
Copy link
Copy Markdown
Contributor

@hirokiej
お疲れ様です。承知しましたー
確認致しますのでお時間ください🙏

Copy link
Copy Markdown
Contributor

@jun-kondo jun-kondo left a comment

Choose a reason for hiding this comment

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

@hirokiej
お疲れ様ですー
コメントしましたー

@hirokiej hirokiej force-pushed the refactor-colleague-trainees-recent-reports-query branch from b857057 to dd747a5 Compare August 4, 2025 15:46
@jun-kondo
Copy link
Copy Markdown
Contributor

@hirokiej
お疲れ様ですー
変更確認しました、LGTMですー🙆‍♂️
ただ、昨日の夜遅くからテスト通らなくなっちゃいましたね。。。私も同じ箇所で失敗してます

@jun-kondo jun-kondo self-requested a review August 16, 2025 00:09
Copy link
Copy Markdown
Contributor

@jun-kondo jun-kondo left a comment

Choose a reason for hiding this comment

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

お疲れ様ですー
すみません。Approveになっていませんでした🙏
おそらくCIが通るようになってると思うので進められると思います👀

@hirokiej hirokiej force-pushed the refactor-colleague-trainees-recent-reports-query branch from dd747a5 to c2fe65d Compare August 16, 2025 16:29
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
test/system/training_completion_test.rb (1)

87-96: 明示的なログインヘルパに統一すると意図がさらに明確に

このテスト内では直前にvisit_with_auth '/reports/new', 'kensyu'を実行しているためセッションは維持されますが、visit new_training_completion_pathvisit_with_authに揃えると、将来ヘルパの挙動やセッション初期化が変わった場合でも意図が明確で壊れにくくなります。(他の同趣旨テストもvisit_with_authで統一されています)

提案差分:

-    visit new_training_completion_path
+    visit_with_auth new_training_completion_path, 'kensyu'
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between dd747a5 and c2fe65d.

📒 Files selected for processing (5)
  • app/controllers/home_controller.rb (1 hunks)
  • app/queries/colleague_trainees_recent_reports_query.rb (1 hunks)
  • test/fixtures/reports.yml (1 hunks)
  • test/queries/colleague_trainees_recent_reports_query_test.rb (1 hunks)
  • test/system/training_completion_test.rb (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • test/queries/colleague_trainees_recent_reports_query_test.rb
  • test/fixtures/reports.yml
  • app/queries/colleague_trainees_recent_reports_query.rb
  • app/controllers/home_controller.rb
🧰 Additional context used
📓 Path-based instructions (2)
**/*.rb

⚙️ CodeRabbit Configuration File

**/*.rb: # refactoring

  • まずFat Controllerを避け、次にFat Modelを避ける。
  • Serviceクラスの乱用を避ける。
  • controller concernを作ろうとしたらPORO(Plain Old Ruby Object)やActiveRecordモデルでの実装で代替できないか検討する。

Rails Patterns

  • ViewHelperにメソッドを実装する時にはまずDecoratorパターンを使うことを検討する。(active_decorator gemを導入しているのでそれを使う)
  • 複雑なActiveRecordクエリがあり、再利用できそうな場合はQueryObjectパターンを検討する。(rails-patterns gemを導入しているのでそれのQuery機能を使う)
  • Viewにpartialを作る場合はViewComponentを使うことを検討する。
  • 複数のActiveRecordモデルを操作する1つの責務がある時や外部APIとやりとりする処理がある場合にはInteractorオブジェクトパターンを検討する。(interactor gemを導入しているのでそれを使う)
  • 複数のInteractorを実行するような処理がある場合Organizerオブジェクトパターンを検討する。(interactor gemを導入しており、その中にOrganizerの機能があるので使う)

Files:

  • test/system/training_completion_test.rb
test/**/*

⚙️ CodeRabbit Configuration File

test/**/*: # Test

  • TestCase名は英語で書く。
  • どうしても避けられない時以外にsystem testでsleepは使わない。

Unit Test

model, helper, decorator, view_componentについてはメソッドを追加した場合は必ず対応したUnit TestのTestCaseを1つは書く。

Files:

  • test/system/training_completion_test.rb
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build_and_test
🔇 Additional comments (1)
test/system/training_completion_test.rb (1)

90-96: confirmの扱いと検証ポイントの切り出しが適切(堅牢性が向上)

  • confirmダイアログはpage.accept_confirmで包み、完了メッセージのassert_textをブロック外へ出したのは正しいです。クリック後の画面更新を待ってから検証でき、フレークを避けられます。
  • assert_not @user.reports.wip.exists?への置き換えも、件数差分ではなく期待状態(WIPが1件も残っていない)を直接担保できるので、仕様に対して強いテストになっています。
  • travel_toを使っているため、assert_equal Time.current, @user.reload.training_completed_atも安定して比較できています。

この変更は、昨夜以降のテスト不安定化(WIP件数差分の前提崩れ)に対して良い方向の修正だと思います。

@hirokiej
Copy link
Copy Markdown
Contributor Author

@jun-kondo さん
返信が遅くなってしまいすみません💦
ちょっと学習を休んでいました🙏

fixtureを増やした影響で、一部テストコードを修正しました。そちらのご確認をよろしくお願いいたします🙇

Copy link
Copy Markdown
Contributor

@jun-kondo jun-kondo left a comment

Choose a reason for hiding this comment

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

お疲れ様ですー🍵

確認しました🙌
追加の変更箇所について、問題ないと思います。
1点コメントさせて頂きました。お手数ですがご確認頂ければ幸いです🙏

end

assert_text '研修終了手続きが完了しました'
assert_not @user.reports.wip.exists?
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.

変更の理由について下記の理解であっていますでしょうか👀

変更前: テスト内でWIP状態の日報を一つ作成し、研修修了手続きの前後でのWIP日報の差分が-1になるアサーションだった。

機能としては研修終了手続きを完了するとすべてのWIP状態の日報が削除されるが、fixtureでユーザーkensyuに紐づくWIPの日報を一つ追加したので差分が-2になりテストが落ちるようになったのでassert_not @user.reports.wip.exists?存在確認に変更。

すべてのWIP状態の日報が削除されるなら、差分の確認より存在確認の方が良さそうですね👀

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.

その理解であってます!
あとは今後、fixtureでWIPの日報をn個作成する度にテストが失敗し、-nで差分を調整しなければならず、保守性の観点からもexistsにすべきだと思いました💡

@hirokiej
Copy link
Copy Markdown
Contributor Author

@jun-kondo さん
ご確認ありがとうございます🙇
レビューもありがとうございました!

@komagata さん
お疲れ様です。
チームメンバーのレビューが終了しましたので、お手隙でレビューをよろしくお願いいたします🙇

@hirokiej hirokiej force-pushed the refactor-colleague-trainees-recent-reports-query branch from c2fe65d to e2478c2 Compare August 21, 2025 08:48
Copy link
Copy Markdown
Member

@komagata komagata left a comment

Choose a reason for hiding this comment

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

確認させて頂きました。OKです〜🙆‍♂️

@komagata komagata merged commit 5a27f26 into main Aug 25, 2025
5 checks passed
@komagata komagata deleted the refactor-colleague-trainees-recent-reports-query branch August 25, 2025 19:52
@github-actions github-actions bot mentioned this pull request Aug 25, 2025
24 tasks
@hirokiej
Copy link
Copy Markdown
Contributor Author

hirokiej commented Sep 1, 2025

@komagata さん
ご確認ありがとうございます!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants