Add a more detailed confirmation message

The message "Are you sure?" is usually followed by blindly clicking
"Yes" without really thinking about what one is doing. So we're
including a bit more information about what's about to happen. That way
it's more likely users will notice it when they accidentally click the
wrong button.

Ideally we would offer the option to undo every common action and then
we wouldn't have to ask for confirmation. But since that isn't the case,
for now we're adding a better confirmation message.

Note we're removing the `resource_name` parameter from the translation
to confirm the action of deleting a record. The reason is, in many
languages it only makes sense to add the model name when it's got an
associated article, and, unlike in English (where "the" is used for
every word), that article is different depending on the noun it's
related to. So we'd have to provide a translation like "name with
article, when singular" for every model.

The complexity of these translations could scalate quickly. And, given
the context, IMHO it isn't essential to add the resouce name. When we're
in the proposals index and there's a proposal named "Improve XYZ", and
we click on "Delete" and see a message saying "This action will delete
XYZ", it is implied that XYZ is a proposal.

So instead we're changing the message so it works for every record with
no need of noun-dependent articles.
This commit is contained in:
Javi Martín
2021-08-17 23:16:16 +02:00
parent 2b4b2f3442
commit 6510cb9615
7 changed files with 59 additions and 15 deletions

View File

@@ -40,7 +40,11 @@ class Admin::ActionComponent < ApplicationComponent
def confirmation_text
if options[:confirm] == true
t("admin.actions.confirm")
if action == :destroy
t("admin.actions.confirm_delete", name: record_name)
else
t("admin.actions.confirm_action", action: text, name: record_name)
end
else
options[:confirm]
end

View File

@@ -1,7 +1,4 @@
<%= render Admin::TableActionsComponent.new(budget,
destroy_confirmation: t("admin.actions.confirm_delete", resource_name: t("admin.budgets.shared.resource_name"),
name: budget.name)
) do |actions| %>
<%= render Admin::TableActionsComponent.new(budget) do |actions| %>
<%= actions.action(:investments,
text: t("admin.budgets.index.budget_investments"),
path: admin_budget_budget_investments_path(budget_id: budget.id)) %>

View File

@@ -5,7 +5,10 @@ en:
actions:
actions: Actions
confirm: Are you sure?
confirm_action: "Are you sure? %{action} \"%{name}\""
confirm_delete: "Are you sure? This action will delete \"%{name}\" and can't be undone."
confirm_hide: Confirm moderation
delete: "Delete"
hide: Hide
hide_author: Hide author
label: "%{action} %{name}"
@@ -14,8 +17,6 @@ en:
unmark_featured: Unmark featured
edit: Edit
configure: Configure
delete: Delete
confirm_delete: "Are you sure? This action will delete %{resource_name} '%{name}' and can't be undone."
officing_booth:
title: "You are officing the booth located at %{booth}. If this is not correct, do not continue and call the help phone number. Thank you."
banners:
@@ -148,8 +149,6 @@ en:
calculate: Calculate Winner Investments
calculated: Winners being calculated, it may take a minute.
recalculate: Recalculate Winner Investments
shared:
resource_name: "the budget"
budget_groups:
name: "Name"
headings_name: "Headings"

View File

@@ -5,7 +5,10 @@ es:
actions:
actions: Acciones
confirm: '¿Estás seguro?'
confirm_action: "¿Estás seguro? %{action} \"%{name}\""
confirm_delete: "¿Estás seguro? Esta acción borrará \"%{name}\" y no se puede deshacer."
confirm_hide: Confirmar moderación
delete: Borrar
hide: Ocultar
hide_author: Bloquear al autor
label: "%{action} %{name}"
@@ -14,8 +17,6 @@ es:
unmark_featured: Quitar destacado
edit: Editar
configure: Configurar
confirm_delete: "¿Estás seguro? Esta acción borrará %{resource_name} '%{name}' y no se puede deshacer."
delete: Borrar
officing_booth:
title: "Estás ahora mismo en la mesa ubicada en %{booth}. Si esto no es correcto no sigas adelante y llama al teléfono de incidencias. Gracias."
banners:
@@ -148,8 +149,6 @@ es:
calculate: Calcular proyectos ganadores
calculated: Calculando ganadores, puede tardar un minuto.
recalculate: Recalcular proyectos ganadores
shared:
resource_name: "el presupuesto"
budget_groups:
name: "Nombre"
headings_name: "Partidas"

View File

@@ -60,4 +60,49 @@ describe Admin::ActionComponent do
end
end
end
describe "data-confirm attribute" do
it "is not rendered by default" do
render_inline Admin::ActionComponent.new(:edit, double, path: "/")
expect(page).to have_link count: 1
expect(page).not_to have_css "[data-confirm]"
end
it "is not rendered when confirm is nil" do
render_inline Admin::ActionComponent.new(:edit, double, path: "/", confirm: nil)
expect(page).to have_link count: 1
expect(page).not_to have_css "[data-confirm]"
end
it "renders with the given value" do
render_inline Admin::ActionComponent.new(:edit, double, path: "/", confirm: "Really?")
expect(page).to have_link count: 1
expect(page).to have_css "[data-confirm='Really?']"
end
context "when confirm is true" do
it "uses the human name as default" do
record = double(human_name: "Everywhere and nowhere")
text = 'Are you sure? Edit "Everywhere and nowhere"'
render_inline Admin::ActionComponent.new(:edit, record, path: "/", confirm: true)
expect(page).to have_link count: 1
expect(page).to have_css "[data-confirm='#{text}']"
end
it "includes a more detailed message for the destroy action" do
record = double(human_name: "Participatory Budget 2015")
text = 'Are you sure? This action will delete "Participatory Budget 2015" and can\\\'t be undone.'
render_inline Admin::ActionComponent.new(:destroy, record, path: "/", confirm: true)
expect(page).to have_link count: 1
expect(page).to have_css "[data-confirm='#{text}']"
end
end
end
end

View File

@@ -39,7 +39,7 @@ describe Admin::TableActionsComponent, controller: Admin::BaseController do
end
it "allows custom URLs" do
render_inline Admin::TableActionsComponent.new(nil, edit_path: "/myedit", destroy_path: "/mydestroy")
render_inline Admin::TableActionsComponent.new(record, edit_path: "/myedit", destroy_path: "/mydestroy")
expect(page).to have_link "Edit", href: "/myedit"
expect(page).to have_link "Delete", href: "/mydestroy"

View File

@@ -99,7 +99,7 @@ describe "Admin budgets", :admin do
visit admin_budgets_path
within "tr", text: "To be deleted" do
message = "Are you sure? This action will delete the budget 'To be deleted' and can't be undone."
message = "Are you sure? This action will delete \"To be deleted\" and can't be undone."
accept_confirm(message) { click_link "Delete" }
end