-
Notifications
You must be signed in to change notification settings - Fork 75
同僚研修生の最新日報取得ロジックのリファクタリングをした #8980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
komagata
merged 4 commits into
main
from
refactor-colleague-trainees-recent-reports-query
Aug 25, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4c9b8f8
ColleagueTraineesRecentReportsQueryに同僚研修生の最新日報を降順で取得するロジックを作成
hirokiej 0c9ca07
ColleagueTraineesRecentReportsQueryを使ってhome_controllerのロジックをリファクタリング
hirokiej cf48ed4
ColleagueTraineesRecentReportsQueryのテストを追加
hirokiej e2478c2
fixture追加で落ちたWIP日報削除確認を件数差分から存在確認に変更
hirokiej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class ColleagueTraineesRecentReportsQuery < Patterns::Query | ||
| queries Report | ||
|
|
||
| private | ||
|
|
||
| def query | ||
| relation | ||
| .joins(:user) | ||
| .where(users: { id: colleague_trainee_ids }) | ||
| .not_wip | ||
| .default_order | ||
| .with_avatar | ||
| end | ||
|
|
||
| def initialize(relation = Report.all, current_user:) | ||
| super(relation) | ||
| @current_user = current_user | ||
| end | ||
|
|
||
| def colleague_trainee_ids | ||
| @current_user.colleague_trainees.pluck(:id) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test/queries/colleague_trainees_recent_reports_query_test.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_helper' | ||
|
|
||
| class ColleagueTraineesRecentReportsQueryTest < ActiveSupport::TestCase | ||
| test 'should return colleague trainees recent reports' do | ||
| user = users(:senpai) | ||
| trainee_report = reports(:report11) | ||
| other_report = reports(:report10) | ||
|
|
||
| result = ColleagueTraineesRecentReportsQuery.new(current_user: user).call | ||
|
|
||
| assert_includes result, trainee_report | ||
| assert_not_includes result, other_report | ||
| end | ||
|
|
||
| test 'should exclude report with wip' do | ||
| user = users(:senpai) | ||
| wip_report = reports(:report75) | ||
|
|
||
| result = ColleagueTraineesRecentReportsQuery.new(current_user: user).call | ||
|
|
||
| assert_not_includes result, wip_report | ||
| end | ||
|
|
||
| test 'should be ordered by recent' do | ||
| user = users(:senpai) | ||
|
|
||
| result = ColleagueTraineesRecentReportsQuery.new(current_user: user).call | ||
|
|
||
| dates = result.map(&:reported_on) | ||
| assert_equal dates, dates.sort.reverse | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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状態の日報が削除されるなら、差分の確認より存在確認の方が良さそうですね👀
There was a problem hiding this comment.
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にすべきだと思いました💡