Extract component to show moderation actions

Note that in proposal notifications we're writing the call to
render the component in the same line as the <div class="reply">
definition in order to be able to use the `:empty` selector when the
component renders nothing. No browser matches whitespace with the
`:empty` selector, so we can't add newline characters inside the tag. A
more elegant solution would be extracting the proposal notification
actions to a component and only rendering it if the moderation actions
component is rendered.
This commit is contained in:
Javi Martín
2021-12-02 02:18:46 +01:00
parent 49edd6a9b1
commit cac24b0159
12 changed files with 126 additions and 76 deletions

View File

@@ -1877,6 +1877,10 @@ table {
padding: $line-height / 4;
position: relative;
&:empty {
display: none;
}
.divider {
color: $text-light;
display: inline-block;

View File

@@ -0,0 +1,12 @@
<span class="moderation-actions">
<% if can? :hide, record %>
<%= link_to t("admin.actions.hide").capitalize, hide_path,
method: :put, remote: true, data: { confirm: confirm_hide_text } %>
<% end %>
<% if can? :hide, record.author %>
<%= raw separator %>
<%= link_to t("admin.actions.hide_author").capitalize, hide_moderation_user_path(record.author_id),
method: :put, data: { confirm: confirm_hide_author_text } %>
<% end %>
</span>

View File

@@ -0,0 +1,34 @@
class Shared::ModerationActionsComponent < ApplicationComponent
attr_reader :record
delegate :can?, to: :helpers
def initialize(record)
@record = record
end
def render?
can?(:hide, record) || can?(:hide, record.author)
end
private
def hide_path
polymorphic_path([:moderation, record], action: :hide)
end
def confirm_hide_text
t("admin.actions.confirm_action", action: t("admin.actions.hide"), name: record.human_name)
end
def confirm_hide_author_text
t("admin.actions.confirm_action", action: t("admin.actions.hide_author"), name: record.author.name)
end
def separator
if record.is_a?(Comment)
"&nbsp;&bull;&nbsp;"
else
"&nbsp;|&nbsp;"
end
end
end

View File

@@ -83,6 +83,10 @@ class Comment < ApplicationRecord
self.user = author
end
def human_name
body.truncate(32)
end
def total_votes
cached_votes_total
end

View File

@@ -1,10 +1 @@
<% if can? :hide, investment %>
<%= link_to t("admin.actions.hide").capitalize, hide_moderation_budget_investment_path(investment),
method: :put, remote: true, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide"), name: investment.title) } %>
<% end %>
<% if can? :hide, investment.author %>
&nbsp;|&nbsp;
<%= link_to t("admin.actions.hide_author").capitalize, hide_moderation_user_path(investment.author_id),
method: :put, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide_author"), name: investment.author.name) } %>
<% end %>
<%= render Shared::ModerationActionsComponent.new(investment) %>

View File

@@ -2,8 +2,7 @@
<%= render "shared/flag_actions", flaggable: comment, divider: true %>
</span>
<span class="js-moderation-actions">
<% if can? :hide, comment %>
<% if can?(:hide, comment) || can?(:hide, comment.user) %>
<span class="divider">&nbsp;&bull;&nbsp;</span>
<% if comment.author == current_user %>
<%= link_to t("comments.actions.delete"),
@@ -11,14 +10,6 @@
method: :put, remote: true, class: "delete-comment",
data: { confirm: t("comments.actions.confirm_delete") } %>
<% else %>
<%= link_to t("admin.actions.hide").capitalize, hide_moderation_comment_path(comment),
method: :put, remote: true, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide"), name: comment.body.truncate(32)) } %>
<%= render Shared::ModerationActionsComponent.new(comment) %>
<% end %>
<% end %>
<% if can? :hide, comment.user %>
<span class="divider">&nbsp;&bull;&nbsp;</span>
<%= link_to t("admin.actions.hide_author").capitalize, hide_moderation_user_path(comment.user_id),
method: :put, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide_author"), name: comment.author.name) } %>
<% end %>
</span>

View File

@@ -1,13 +1,4 @@
<% if can? :hide, debate %>
<%= link_to t("admin.actions.hide").capitalize, hide_moderation_debate_path(debate),
method: :put, remote: true, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide"), name: debate.title) } %>
<% end %>
<% if can? :hide, debate.author %>
&nbsp;|&nbsp;
<%= link_to t("admin.actions.hide_author").capitalize, hide_moderation_user_path(debate.author_id),
method: :put, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide_author"), name: debate.author.name) } %>
<% end %>
<%= render Shared::ModerationActionsComponent.new(debate) %>
<% if can? :mark_featured, debate %>
&nbsp;|&nbsp;

View File

@@ -1,10 +1 @@
<% if can? :hide, proposal %>
<%= link_to t("admin.actions.hide").capitalize, hide_moderation_legislation_proposal_path(proposal),
method: :put, remote: true, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide"), name: proposal.title) } %>
<% end %>
<% if can? :hide, proposal.author %>
&nbsp;|&nbsp;
<%= link_to t("admin.actions.hide_author").capitalize, hide_moderation_user_path(proposal.author_id),
method: :put, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide_author"), name: proposal.author.name) } %>
<% end %>
<%= render Shared::ModerationActionsComponent.new(proposal) %>

View File

@@ -1,16 +1 @@
<% if can? :hide, (notification || notification.author) %>
<div class="reply">
<span class="js-moderation-actions">
<% if can? :hide, notification %>
<%= link_to t("admin.actions.hide").capitalize, hide_moderation_proposal_notification_path(notification),
method: :put, remote: true, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide"), name: notification.title) } %>
<% end %>
<% if can? :hide, notification.author %>
<span class="divider">&nbsp;&bull;&nbsp;</span>
<%= link_to t("admin.actions.hide_author").capitalize, hide_moderation_user_path(notification.author_id),
method: :put, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide_author"), name: notification.author.name) } %>
<% end %>
</span>
</div>
<% end %>
<div class="reply"><%= render Shared::ModerationActionsComponent.new(notification) %></div>

View File

@@ -1,10 +1 @@
<% if can? :hide, proposal %>
<%= link_to t("admin.actions.hide").capitalize, hide_moderation_proposal_path(proposal),
method: :put, remote: true, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide"), name: proposal.title) } %>
<% end %>
<% if can? :hide, proposal.author %>
&nbsp;|&nbsp;
<%= link_to t("admin.actions.hide_author").capitalize, hide_moderation_user_path(proposal.author_id),
method: :put, data: { confirm: t("admin.actions.confirm_action", action: t("admin.actions.hide_author"), name: proposal.author.name) } %>
<% end %>
<%= render Shared::ModerationActionsComponent.new(proposal) %>

View File

@@ -0,0 +1,56 @@
require "rails_helper"
describe Shared::ModerationActionsComponent do
include Rails.application.routes.url_helpers
before { sign_in(create(:administrator).user) }
describe "Hide link" do
it "is shown for debates" do
debate = create(:debate)
render_inline Shared::ModerationActionsComponent.new(debate)
expect(page).to have_link "Hide", href: hide_moderation_debate_path(debate)
end
it "is shown for proposals" do
proposal = create(:proposal)
render_inline Shared::ModerationActionsComponent.new(proposal)
expect(page).to have_link "Hide", href: hide_moderation_proposal_path(proposal)
end
it "is shown for proposal notifications" do
notification = create(:proposal_notification)
render_inline Shared::ModerationActionsComponent.new(notification)
expect(page).to have_link "Hide", href: hide_moderation_proposal_notification_path(notification)
end
it "is shown for comments" do
comment = create(:comment)
render_inline Shared::ModerationActionsComponent.new(comment)
expect(page).to have_link "Hide", href: hide_moderation_comment_path(comment)
end
it "is shown for budget investments" do
investment = create(:budget_investment)
render_inline Shared::ModerationActionsComponent.new(investment)
expect(page).to have_link "Hide", href: hide_moderation_budget_investment_path(investment)
end
it "is shown for legislation proposals" do
proposal = create(:legislation_proposal)
render_inline Shared::ModerationActionsComponent.new(proposal)
expect(page).to have_link "Hide", href: hide_moderation_legislation_proposal_path(proposal)
end
end
end

View File

@@ -275,7 +275,7 @@ describe "Internal valuation comments on Budget::Investments" do
expect(page).to have_no_css(".comment-votes")
expect(page).to have_no_css(".js-flag-actions")
expect(page).to have_no_css(".js-moderation-actions")
expect(page).to have_no_css(".moderation-actions")
end
end