comments: put sticky option in popup menu instead of in edit form.

Put the option to sticky a comment in the "..." popup menu instead of
in the comment edit form. This makes it more consistent with deleting or
undeleting a comment.

Also fix a bug where the comment undelete icon didn't show up due to a
typo.
This commit is contained in:
evazion
2021-03-07 20:01:43 -06:00
parent e6a501393a
commit fee7ed506b
10 changed files with 43 additions and 26 deletions

View File

@@ -1,6 +1,7 @@
class ApplicationComponent < ViewComponent::Base
delegate :link_to_user, :time_ago_in_words_tagged, :format_text, :external_link_to, :tag_class, to: :helpers
delegate :edit_icon, :delete_icon, :undelete_icon, :flag_icon, :upvote_icon, :downvote_icon, :link_icon, to: :helpers
delegate :edit_icon, :delete_icon, :undelete_icon, :flag_icon, :upvote_icon,
:downvote_icon, :link_icon, :sticky_icon, :unsticky_icon, to: :helpers
def policy(subject)
Pundit.policy!(current_user, subject)

View File

@@ -2,7 +2,6 @@
class CommentComponent < ApplicationComponent
attr_reader :comment, :context, :dtext_data, :current_user
delegate :link_to_user, :time_ago_in_words_tagged, :format_text, :edit_icon, :delete_icon, :undelete_icon, :flag_icon, :upvote_icon, :downvote_icon, :link_icon, to: :helpers
def initialize(comment:, current_user:, context: nil, dtext_data: nil)
@comment = comment

View File

@@ -99,17 +99,31 @@
<%= menu.item do %>
<% if comment.is_deleted? %>
<%= link_to undelete_comment_path(comment.id), method: :post, remote: true do %>
<%= link_to comment_path(comment.id), "data-params": "comment[is_deleted]=false", method: :put, remote: true do %>
<%= undelete_icon %> Undelete
<% end %>
<% else %>
<%= link_to comment_path(comment.id), "data-confirm": "Are you sure you want to delete this comment?", method: :delete, remote: true do %>
<%= link_to comment_path(comment.id), "data-params": "comment[is_deleted]=true", "data-confirm": "Are you sure you want to delete this comment?", method: :put, remote: true do %>
<%= delete_icon %> Delete
<% end %>
<% end %>
<% end %>
<% end %>
<% if policy(comment).can_sticky_comment? %>
<%= menu.item do %>
<% if comment.is_sticky? %>
<%= link_to comment_path(comment.id), "data-params": "comment[is_sticky]=false", method: :put, remote: true do %>
<%= unsticky_icon %> Unsticky
<% end %>
<% else %>
<%= link_to comment_path(comment.id), "data-params": "comment[is_sticky]=true", method: :put, remote: true do %>
<%= sticky_icon %> Sticky
<% end %>
<% end %>
<% end %>
<% end %>
<% if policy(comment).reportable? %>
<%= menu.item do %>
<%= link_to new_moderation_report_path(moderation_report: { model_type: "Comment", model_id: comment.id }), remote: true do %>

View File

@@ -30,8 +30,9 @@ div.popup-menu {
display: block;
padding: 0.125em 2em 0.125em 0;
i.icon {
width: 1.5em;
.icon {
width: 1rem;
margin-right: 0.25rem;
}
}
}

View File

@@ -1,6 +1,6 @@
class CommentsController < ApplicationController
respond_to :html, :xml, :json, :atom
respond_to :js, only: [:new, :destroy, :undelete]
respond_to :js, only: [:new, :update, :destroy, :undelete]
def index
params[:group_by] ||= "comment" if params[:search].present?
@@ -31,7 +31,7 @@ class CommentsController < ApplicationController
def update
@comment = authorize Comment.find(params[:id])
@comment.update(permitted_attributes(@comment))
respond_with(@comment, :location => post_path(@comment.post_id))
respond_with(@comment)
end
def create

View File

@@ -7,8 +7,8 @@ module ComponentsHelper
render PostPreviewComponent.with_collection(posts, **options)
end
def render_comment(comment, **options)
render CommentComponent.new(comment: comment, **options)
def render_comment(comment, current_user:, **options)
render CommentComponent.new(comment: comment, current_user: current_user, **options)
end
def render_comment_section(post, **options)

View File

@@ -30,6 +30,10 @@ module IconHelper
icon_tag("fas fa-thumbtack", **options)
end
def unsticky_icon(**options)
svg_icon_tag("unsticky-icon", "M306.5 186.6l-5.7-42.6H328c13.2 0 24-10.8 24-24V24c0-13.2-10.8-24-24-24H56C42.8 0 32 10.8 32 24v96c0 13.2 10.8 24 24 24h27.2l-5.7 42.6C29.6 219.4 0 270.7 0 328c0 13.2 10.8 24 24 24h144v104c0 .9.1 1.7.4 2.5l16 48c2.4 7.3 12.8 7.3 15.2 0l16-48c.3-.8.4-1.7.4-2.5V352h144c13.2 0 24-10.8 24-24 0-57.3-29.6-108.6-77.5-141.4zM50.5 304c8.3-38.5 35.6-70 71.5-87.8L138 96H80V48h224v48h-58l16 120.2c35.8 17.8 63.2 49.4 71.5 87.8z", **options)
end
def lock_icon(**options)
icon_tag("fas fa-lock", **options)
end
@@ -39,7 +43,7 @@ module IconHelper
end
def undelete_icon(**options)
icon_tag("fas fa-trash-restore_alt", **options)
icon_tag("fas fa-trash-restore-alt", **options)
end
def private_icon(**options)

View File

@@ -6,9 +6,6 @@
<%= f.button :submit, "Submit" %>
<%= dtext_preview_button "comment_body" %>
<% if comment.new_record? %>
<%= f.input :do_not_bump_post, :label => "No bump" %>
<% end %>
<% if policy(comment).can_sticky_comment? %>
<%= f.input :is_sticky, label: "Sticky", for: "comment_is_sticky" %>
<%= f.input :do_not_bump_post, label: "No bump" %>
<% end %>
<% end %>

View File

@@ -0,0 +1 @@
$("#comment_<%= @comment.id %>").replaceWith("<%= j render_comment(@comment, current_user: CurrentUser.user) %>");

View File

@@ -142,14 +142,14 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest
context "when updating another user's comment" do
should "succeed if updater is a moderator" do
put_auth comment_path(@comment.id), @user, params: {comment: {body: "abc"}}
put_auth comment_path(@comment.id), @user, params: {comment: {body: "abc"}}, xhr: true
assert_equal("abc", @comment.reload.body)
assert_redirected_to post_path(@comment.post)
assert_response :success
end
should "fail if updater is not a moderator" do
@mod_comment = as(@mod) { create(:comment, post: @post) }
put_auth comment_path(@mod_comment.id), @user, params: {comment: {body: "abc"}}
put_auth comment_path(@mod_comment.id), @user, params: {comment: {body: "abc"}}, xhr: true
assert_not_equal("abc", @mod_comment.reload.body)
assert_response 403
end
@@ -157,13 +157,13 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest
context "when stickying a comment" do
should "succeed if updater is a moderator" do
put_auth comment_path(@comment.id), @mod, params: {comment: {is_sticky: true}}
put_auth comment_path(@comment.id), @mod, params: {comment: {is_sticky: true}}, xhr: true
assert_equal(true, @comment.reload.is_sticky)
assert_redirected_to @comment.post
assert_response :success
end
should "fail if updater is not a moderator" do
put_auth comment_path(@comment.id), @user, params: {comment: {is_sticky: true}}
put_auth comment_path(@comment.id), @user, params: {comment: {is_sticky: true}}, xhr: true
assert_response 403
assert_equal(false, @comment.reload.is_sticky)
end
@@ -172,7 +172,7 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest
context "for a deleted comment" do
should "not allow the creator to edit the comment" do
@comment.update!(is_deleted: true)
put_auth comment_path(@comment.id), @user, params: { comment: { body: "blah" }}
put_auth comment_path(@comment.id), @user, params: { comment: { body: "blah" }}, xhr: true
assert_response 403
assert_not_equal("blah", @comment.reload.body)
@@ -180,16 +180,16 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest
end
should "update the body" do
put_auth comment_path(@comment.id), @user, params: {comment: {body: "abc"}}
put_auth comment_path(@comment.id), @user, params: {comment: {body: "abc"}}, xhr: true
assert_equal("abc", @comment.reload.body)
assert_redirected_to post_path(@comment.post)
assert_response :success
end
should "allow changing the body and is_deleted" do
put_auth comment_path(@comment.id), @user, params: {comment: {body: "herp derp", is_deleted: true}}
put_auth comment_path(@comment.id), @user, params: {comment: {body: "herp derp", is_deleted: true}}, xhr: true
assert_equal("herp derp", @comment.reload.body)
assert_equal(true, @comment.is_deleted)
assert_redirected_to post_path(@post)
assert_response :success
end
should "not allow changing do_not_bump_post or post_id" do