Fix NoMethodError when a comment's commentable has no user#10281
Conversation
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
📝 WalkthroughWalkthrough
Changesメンション対象の安全な生成
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
test/models/concerns/mentioner_test.rb (1)
34-39: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
assert_nothing_raisedだけでなく戻り値も検証してください。comments(:comment1)はReportなので safe navigation の経路自体は通っていますが、例外が出ないことだけではwhere_mentionの文言崩れを拾えません。userがnilのときの文字列もassert_equalで固定してください。🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/models/concerns/mentioner_test.rb` around lines 34 - 39, Update the `where_mention` test to assert the returned string when `commentable.user` is stubbed to nil, using `assert_equal` with the expected no-user message while retaining the exception-safety check.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@test/models/concerns/mentioner_test.rb`:
- Around line 34-39: Update the `where_mention` test to assert the returned
string when `commentable.user` is stubbed to nil, using `assert_equal` with the
expected no-user message while retaining the exception-safety check.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: fbf54cb6-9953-4022-81f8-5740348715a1
📒 Files selected for processing (2)
app/models/concerns/mentioner.rbtest/models/concerns/mentioner_test.rb
Issue
概要
Mentioner#target_of_commentがハッシュリテラルで全ての項目を先に評価していたため、commentable_classがReportやProduct以外でもcommentable.user.login_nameが実行されていました。commentable.userがnil(Comment#userはcommentableが user を持たない場合にnilを返します)のときにNoMethodError: undefined method 'login_name' for nilが発生します。where_mentionと同じくcaseに変更し、該当するクラスの分岐だけを評価するようにしました。あわせて Report / Product の分岐はuser&.login_nameにして user が無い場合でも落ちないようにしています。変更確認方法
bin/rails test test/models/concerns/mentioner_test.rbを実行し、追加した#where_mention does not raise when the commentable has no userが通ることを確認するScreenshot
変更前
(表示に影響のない内部処理の修正のため、スクリーンショットはありません)
変更後
Summary by CodeRabbit