DEV: Allow specifying button class in reviewable action definitions (#8093)

This avoids the need for using `@extend` in SCSS, which can be problematic in plugins

For context, see https://review.discourse.org/t/fix-make-compatible-with-debundled-plugin-css-assets-feature/5297/7
This commit is contained in:
David Taylor 2019-09-18 11:28:59 +01:00 committed by romanrizzi
parent 67e8fbc480
commit 68fc799380
7 changed files with 14 additions and 10 deletions

View File

@ -9,7 +9,7 @@
disabled=reviewableUpdating}} disabled=reviewableUpdating}}
{{else}} {{else}}
{{d-button {{d-button
class=(concat "reviewable-action " (dasherize first.id)) class=(concat "reviewable-action " (dasherize first.id) " " first.button_class)
icon=first.icon icon=first.icon
action=(action "perform" first) action=(action "perform" first)
translatedLabel=first.label translatedLabel=first.label

View File

@ -259,10 +259,6 @@
.reviewable-action, .reviewable-action,
.reviewable-action-dropdown { .reviewable-action-dropdown {
margin-right: 0.5em; margin-right: 0.5em;
&.delete-user {
@extend .btn-danger;
}
} }
} }
padding-bottom: 1em; padding-bottom: 1em;

View File

@ -267,10 +267,11 @@ protected
end end
end end
def build_action(actions, id, icon:, bundle: nil, client_action: nil, confirm: false) def build_action(actions, id, icon:, button_class: nil, bundle: nil, client_action: nil, confirm: false)
actions.add(id, bundle: bundle) do |action| actions.add(id, bundle: bundle) do |action|
prefix = "reviewables.actions.#{id}" prefix = "reviewables.actions.#{id}"
action.icon = icon action.icon = icon
action.button_class = button_class
action.label = "#{prefix}.title" action.label = "#{prefix}.title"
action.description = "#{prefix}.description" action.description = "#{prefix}.description"
action.client_action = client_action action.client_action = client_action

View File

@ -29,6 +29,7 @@ class ReviewableQueuedPost < Reviewable
if pending? && guardian.can_delete_user?(created_by) if pending? && guardian.can_delete_user?(created_by)
actions.add(:delete_user) do |action| actions.add(:delete_user) do |action|
action.icon = 'trash-alt' action.icon = 'trash-alt'
action.button_class = 'btn-danger'
action.label = 'reviewables.actions.delete_user.title' action.label = 'reviewables.actions.delete_user.title'
action.confirm_message = 'reviewables.actions.delete_user.confirm' action.confirm_message = 'reviewables.actions.delete_user.confirm'
end end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class ReviewableActionSerializer < ApplicationSerializer class ReviewableActionSerializer < ApplicationSerializer
attributes :id, :icon, :label, :confirm_message, :description, :client_action attributes :id, :icon, :button_class, :label, :confirm_message, :description, :client_action
def label def label
I18n.t(object.label) I18n.t(object.label)

View File

@ -33,11 +33,11 @@ class Reviewable < ActiveRecord::Base
end end
class Action < Item class Action < Item
attr_accessor :icon, :label, :description, :confirm_message, :client_action attr_accessor :icon, :button_class, :label, :description, :confirm_message, :client_action
def initialize(id, icon = nil, label = nil) def initialize(id, icon = nil, button_class = nil, label = nil)
super(id) super(id)
@icon, @label = icon, label @icon, @button_class, @label = icon, button_class, label
end end
end end

View File

@ -118,6 +118,12 @@ RSpec.describe ReviewableQueuedPost, type: :model do
end end
context "delete_user" do context "delete_user" do
it "has the correct button class" do
expect(reviewable.actions_for(Guardian.new(moderator)).to_a.
find { |a| a.id == :delete_user }.button_class).
to eq("btn-danger")
end
it "deletes the user and rejects the post" do it "deletes the user and rejects the post" do
other_reviewable = Fabricate(:reviewable_queued_post, created_by: reviewable.created_by) other_reviewable = Fabricate(:reviewable_queued_post, created_by: reviewable.created_by)