diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 6d1b23579..650bcba10 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -318,7 +318,7 @@ class StoriesController < ApplicationController end def assign_user_comments - comment_count = params[:view] == "comments" ? 250 : 8 + comment_count = helpers.comment_count(params[:view]) @comments = if @user.comments_count.positive? @user.comments.where(deleted: false) .order(created_at: :desc).includes(:commentable).limit(comment_count) diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 8db8c9852..39663a03f 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -1,4 +1,7 @@ module CommentsHelper + MAX_COMMENTS_TO_RENDER = 250 + MIN_COMMENTS_TO_RENDER = 8 + def comment_class(comment, is_view_root: false) if comment.root? || is_view_root "root" @@ -31,6 +34,22 @@ module CommentsHelper comment.hidden_by_commentable_user && comment != root_comment end + def high_number_of_comments?(comments_number) + comments_number > MAX_COMMENTS_TO_RENDER + end + + def view_all_comments?(comments_number) + comments_number > MIN_COMMENTS_TO_RENDER + end + + def number_of_comments_to_render + MAX_COMMENTS_TO_RENDER + end + + def comment_count(view) + view == "comments" ? MAX_COMMENTS_TO_RENDER : MIN_COMMENTS_TO_RENDER + end + def like_button_text(comment) case comment.public_reactions_count when 0 diff --git a/app/views/users/_comments_section.html.erb b/app/views/users/_comments_section.html.erb index 3a788edab..5ad5a6070 100644 --- a/app/views/users/_comments_section.html.erb +++ b/app/views/users/_comments_section.html.erb @@ -4,8 +4,8 @@ <% if params[:view] == "comments" %>

- <% if @user.comments_count > 250 %> - Last 250 comments + <% if high_number_of_comments?(@user.comments_count) %> + Last <%= number_of_comments_to_render %> comments <% else %> All <%= @user.comments_count %> comments <% end %> @@ -41,13 +41,13 @@ <% end %> <% end %> - <% if params[:view] != "comments" && @user.comments_count > 250 %> + <% if params[:view] != "comments" && high_number_of_comments?(@user.comments_count) %> - <% elsif params[:view] != "comments" && @user.comments_count > 8 %> + <% elsif params[:view] != "comments" && view_all_comments?(@user.comments_count) %>
View all <%= @user.comments_count %> comments diff --git a/spec/system/user/view_user_comments_spec.rb b/spec/system/user/view_user_comments_spec.rb index 16b24a48e..b64205868 100644 --- a/spec/system/user/view_user_comments_spec.rb +++ b/spec/system/user/view_user_comments_spec.rb @@ -23,4 +23,14 @@ RSpec.describe "User comments", type: :system do end end end + + context "when user has too many comments" do + it "show user's last comments ", js: true, stub_elasticsearch: true do + stub_const("CommentsHelper::MAX_COMMENTS_TO_RENDER", 1) + visit "/user3000/comments" + within("#substories div.profile-comment-card") do + expect(page).to have_content("Last 1 comments") + end + end + end end diff --git a/spec/system/user/view_user_index_spec.rb b/spec/system/user/view_user_index_spec.rb index 6bea14b7b..afc149c58 100644 --- a/spec/system/user/view_user_index_spec.rb +++ b/spec/system/user/view_user_index_spec.rb @@ -5,6 +5,7 @@ RSpec.describe "User index", type: :system, stub_elasticsearch: true do let!(:article) { create(:article, user: user) } let!(:other_article) { create(:article) } let!(:comment) { create(:comment, user: user, commentable: other_article) } + let!(:comment2) { create(:comment, user: user, commentable: other_article) } let(:organization) { create(:organization) } context "when user is unauthorized" do @@ -22,6 +23,7 @@ RSpec.describe "User index", type: :system, stub_elasticsearch: true do shows_articles shows_comments shows_comment_timestamp + shows_last_comments end def shows_header @@ -46,20 +48,21 @@ RSpec.describe "User index", type: :system, stub_elasticsearch: true do within("#substories div.profile-comment-card") do expect(page).to have_content("Recent comments") expect(page).to have_link(nil, href: comment.path) + expect(page).to have_link(nil, href: comment2.path) end within("#substories") do expect(page).to have_selector(".profile-comment-card", count: 1) end - within("#substories .profile-comment-card .profile-comment-row") do + within("#substories .profile-comment-card .profile-comment-row:first-of-type") do comment_date = comment.readable_publish_date.gsub(" ", " ") expect(page).to have_selector(".comment-date", text: comment_date) end end def shows_comment_timestamp - within("#substories .profile-comment-card .profile-comment-row") do + within("#substories .profile-comment-card .profile-comment-row:first-of-type") do ts = comment.decorate.published_timestamp timestamp_selector = ".comment-date time[datetime='#{ts}']" expect(page).to have_selector(timestamp_selector) @@ -90,6 +93,7 @@ RSpec.describe "User index", type: :system, stub_elasticsearch: true do shows_header shows_articles shows_comments + shows_last_comments end def shows_header @@ -113,4 +117,12 @@ RSpec.describe "User index", type: :system, stub_elasticsearch: true do end end end + + def shows_last_comments + stub_const("CommentsHelper::MAX_COMMENTS_TO_RENDER", 1) + visit "/#{user.username}" + within("#substories .profile-comment-card .pt-3 .fs-base") do + expect(page).to have_content("View last 1 Comment") + end + end end