Refactor magic numbers for /comments rendering (#12323)
* Refactor magic numbers for `/comments` rendering (#11594) * Update comments_helper adding constants & accessor methods * Refactor stories_controller replacing hard coded values by helper call * Refactor _comments_section view accordingly with helper calls * Add test to specs (view_user(index|comments)) accordingly * Refactor magic numbers for `/comments` rendering Small changes after initial review * Change constante to more explicit names * Change one helper method name * Pluralize in view Co-authored-by: rhymes <rhymes@hey.com>
This commit is contained in:
parent
1f90c63eac
commit
a3e76b6d09
5 changed files with 49 additions and 8 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@
|
|||
<% if params[:view] == "comments" %>
|
||||
<div class="crayons-card__header">
|
||||
<h3 class="crayons-subtitle-2">
|
||||
<% 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 @@
|
|||
</a>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% if params[:view] != "comments" && @user.comments_count > 250 %>
|
||||
<% if params[:view] != "comments" && high_number_of_comments?(@user.comments_count) %>
|
||||
<div class="pt-3 pl-4 pb-3 pr-4">
|
||||
<a href="<%= @user.path %>/comments" class="fs-base">
|
||||
View last 250 Comments
|
||||
View last <%= pluralize(number_of_comments_to_render, "Comment") %>
|
||||
</a>
|
||||
</div>
|
||||
<% elsif params[:view] != "comments" && @user.comments_count > 8 %>
|
||||
<% elsif params[:view] != "comments" && view_all_comments?(@user.comments_count) %>
|
||||
<div class="py-3 px-4">
|
||||
<a href="<%= @user.path %>/comments" class="fs-base">
|
||||
View all <%= @user.comments_count %> comments
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue