From 2e70faf61aeff21fcf702ec6a18c5e9d64489284 Mon Sep 17 00:00:00 2001 From: Chris Todorov Date: Mon, 1 Apr 2024 21:15:13 -0700 Subject: [PATCH] Improve resource name pluralization in confirm dialog This change adds proper pluralization in the confirm dialog for the resource being modified. Previously the dialog always used the plural version of the resource. --- .../components/solidus_admin/ui/table/component.js | 14 ++++++++++---- .../components/solidus_admin/ui/table/component.rb | 9 +++++++-- .../solidus_admin/ui/table/component.yml | 2 +- admin/spec/features/products_spec.rb | 6 +++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/admin/app/components/solidus_admin/ui/table/component.js b/admin/app/components/solidus_admin/ui/table/component.js index 0e5472fa8bf..66837d4ce75 100644 --- a/admin/app/components/solidus_admin/ui/table/component.js +++ b/admin/app/components/solidus_admin/ui/table/component.js @@ -130,10 +130,16 @@ export default class extends Controller { } confirmAction(event) { - const message = event.params.message.replace( - "${count}", - this.selectedRows().length - ) + const message = event.params.message + .replace( + "${count}", + this.selectedRows().length + ).replace( + "${resource}", + this.selectedRows().length > 1 ? + event.params.resourcePlural : + event.params.resourceSingular + ) if (!confirm(message)) { event.preventDefault() diff --git a/admin/app/components/solidus_admin/ui/table/component.rb b/admin/app/components/solidus_admin/ui/table/component.rb index 091bde08e0c..c398b521d74 100644 --- a/admin/app/components/solidus_admin/ui/table/component.rb +++ b/admin/app/components/solidus_admin/ui/table/component.rb @@ -18,6 +18,10 @@ def initialize(**args) self.batch_actions = batch_actions.to_a.map { |action| BatchAction.new(**action) } end + def singular_name + self[:class].model_name.human if self[:class] + end + def plural_name self[:class].model_name.human.pluralize if self[:class] end @@ -116,9 +120,10 @@ def render_batch_action_button(batch_action) params["data-action"] = "click->#{stimulus_id}#confirmAction" params["data-#{stimulus_id}-message-param"] = t( ".action_confirmation", - action: batch_action.label.downcase, - resource: @data.plural_name.downcase + action: batch_action.label.downcase ) + params["data-#{stimulus_id}-resource-singular-param"] = @data.singular_name.downcase + params["data-#{stimulus_id}-resource-plural-param"] = @data.plural_name.downcase end render component("ui/button").new(**params) diff --git a/admin/app/components/solidus_admin/ui/table/component.yml b/admin/app/components/solidus_admin/ui/table/component.yml index 8f1c086e3db..0b43b9cf759 100644 --- a/admin/app/components/solidus_admin/ui/table/component.yml +++ b/admin/app/components/solidus_admin/ui/table/component.yml @@ -5,7 +5,7 @@ en: select_row: 'Select row' filter: 'Filter' search_placeholder: 'Search all %{resources}' - action_confirmation: 'Are you sure you want to %{action} ${count} %{resource}?' + action_confirmation: 'Are you sure you want to %{action} ${count} ${resource}?' refine_search: 'Refine Search' batch_actions: Batch actions cancel: Cancel diff --git a/admin/spec/features/products_spec.rb b/admin/spec/features/products_spec.rb index 0181dc9e130..9014543741d 100644 --- a/admin/spec/features/products_spec.rb +++ b/admin/spec/features/products_spec.rb @@ -30,7 +30,7 @@ visit "/admin/products" select_row("Just a product") - accept_confirm("Are you sure you want to delete 1 products?") do + accept_confirm("Are you sure you want to delete 1 product?") do click_button "Delete" end @@ -48,7 +48,7 @@ visit "/admin/products" find('main tbody tr:nth-child(2)').find('input').check - accept_confirm("Are you sure you want to discontinue 1 products?") do + accept_confirm("Are you sure you want to discontinue 1 product?") do click_button "Discontinue" end @@ -66,7 +66,7 @@ find('main tbody tr:nth-child(2)').find('input').check - accept_confirm("Are you sure you want to activate 1 products?") do + accept_confirm("Are you sure you want to activate 1 product?") do click_button "Activate" end