diff --git a/app/helpers/page_tabs/users_helper.rb b/app/helpers/page_tabs/users_helper.rb index 4705ea28c80..9a9bb9d1cc7 100644 --- a/app/helpers/page_tabs/users_helper.rb +++ b/app/helpers/page_tabs/users_helper.rb @@ -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) } diff --git a/app/models/comment.rb b/app/models/comment.rb index 383074b2d85..d674b4b8897 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -18,6 +18,8 @@ class Comment < ApplicationRecord mentionable_as :description, hook_name: :after_commit + scope :without_talk, -> { where.not(commentable_type: 'Talk') } + class << self def commented_users User.with_attached_avatar diff --git a/app/views/users/_activity_counts.html.slim b/app/views/users/_activity_counts.html.slim index 7f95326824b..61338eacc30 100644 --- a/app/views/users/_activity_counts.html.slim +++ b/app/views/users/_activity_counts.html.slim @@ -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 diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml index 732434e4178..f261378c7aa 100644 --- a/test/fixtures/comments.yml +++ b/test/fixtures/comments.yml @@ -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" diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb index cfabde265ae..b726495eb15 100644 --- a/test/models/comment_test.rb +++ b/test/models/comment_test.rb @@ -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