Skip to content
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

相談部屋のコメントを省いたコメント数を出力するようにした #8223

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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/helpers/page_tabs/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module PageTabs
module UsersHelper
def user_page_tabs(user, active_tab:)
comment_count = user.comments.where.not(commentable_type: 'Talk').length
comment_count = user.comments.without_talk.length
tabs = []
tabs << { name: 'プロフィール', link: user_path(user) }
tabs << { name: 'ポートフォリオ', link: user_portfolio_path(user) }
Expand Down
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Comment < ApplicationRecord

mentionable_as :description, hook_name: :after_commit

scope :without_talk, -> { where.not(commentable_type: 'Talk') }
Copy link
Contributor

Choose a reason for hiding this comment

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

新しくスコープを定義して「相談部屋以外のコメント」を抽出しやすくしたのが分かりやすくて良いなと思いました!

一方、今回の PR で表示数を揃えようとしているもう片側の「ユーザーページの上部タブにあるコメント(既に相談部屋のコメントは省かれている)」の数値取得の方( app/helpers/page_tabs/users_helper.rb 内)でも where.not(commentable_type: 'Talk') の表記がこのまま使われているようでしたので、 users_helper.rb 内の方も without_talk スコープに置き換えて良さそうに思ったのですが、いかがでしょうか?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

確かにおっしゃる通りです!ご指摘ありがとうございます!
スコープに置き換えます👍


class << self
def commented_users
User.with_attached_avatar
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/_activity_counts.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ dl.card-counts__items
dt.card-counts__item-label
| コメント
dd.card-counts__item-value
= link_to_if !user.comments.empty?,
user.comments.size, user_comments_path(user)
= link_to_if !user.comments.without_talk.empty?,
user.comments.without_talk.size, user_comments_path(user)
.card-counts__item
.card-counts__item-inner
dt.card-counts__item-label
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ comment42:
commentOfTalk:
user: komagata
commentable: talk1 (Talk)
commentable_type: Talk
description: "これは相談部屋の会話です。"
updated_at: "2019-01-02 00:00:00 JST"

commentOfTalk2:
user: hajime
commentable: talk9 (Talk)
commentable_type: Talk
description: "これは相談部屋の会話です。"
updated_at: "2019-01-02 00:00:00 JST"
7 changes: 7 additions & 0 deletions test/models/comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class CommentTest < ActiveSupport::TestCase
AbstractNotifier.delivery_mode = @delivery_mode
end

test '.without_talk' do
non_talk_comment_count = Comment.without_talk.count
all_comment_count = Comment.count
only_talk_comment_count = Comment.where(commentable_type: 'Talk').count
assert_equal non_talk_comment_count, all_comment_count - only_talk_comment_count
end

test '.commented_users' do
report = reports(:report4)
users = report.comments.commented_users
Expand Down