Improve enterprise removal (success case)

Make enterprise removal use turbo, which provides the following
benefits:

* More responsive removal since there's no full page reload.
* A success flash message (previously nothing was displayed).
* No double alert prompt.

It also goes in the direction of removing mrujs in favor of turbo.
This commit is contained in:
David Rodríguez
2025-10-23 10:22:05 +02:00
parent 04323388ad
commit 6aa7ef3c21
5 changed files with 39 additions and 3 deletions

View File

@@ -162,6 +162,15 @@ module Admin
end
end
def destroy
@object.destroy
flash.now[:success] = flash_message_for(@object, :successfully_removed)
respond_to do |format|
format.turbo_stream { render :destroy, status: :ok }
end
end
protected
def delete_custom_tab

View File

@@ -63,8 +63,11 @@ module EnterprisesHelper
url = object_url(enterprise)
name = t(:delete)
options = {}
options[:class] = "delete-resource"
options[:data] = { action: 'remove', confirm: enterprise_confirm_delete_message(enterprise) }
options[:data] = {
turbo: true,
'turbo-method': 'delete',
'turbo-confirm': enterprise_confirm_delete_message(enterprise)
}
link_to_with_icon 'icon-trash', name, url, options
end

View File

@@ -22,7 +22,7 @@
%tbody
= f.fields_for :collection do |enterprise_form|
- enterprise = enterprise_form.object
%tr{class: "enterprise-#{enterprise.id}"}
%tr{class: "enterprise-#{enterprise.id}", id: "resource-#{enterprise.id}"}
%td= link_to enterprise.name, main_app.edit_admin_enterprise_path(enterprise)
%td
= enterprise_form.check_box :is_primary_producer

View File

@@ -0,0 +1,4 @@
- unless flash[:error]
= turbo_stream.remove "resource-#{@object.id}"
= turbo_stream.append "flashes" do
= render(partial: 'admin/shared/flashes', locals: { flashes: flash })

View File

@@ -68,6 +68,26 @@ RSpec.describe '
expect(page).to have_checked_field "enterprise_visible_only_through_links"
end
it "deleting an existing enterprise successfully" do
enterprise = create(:enterprise)
user = create(:user)
admin = login_as_admin
visit '/admin/enterprises'
expect do
accept_alert do
within "tr.enterprise-#{enterprise.id}" do
first("a", text: 'Delete').click
end
end
expect(page).to have_content("Successfully Removed")
end.to change{ Enterprise.count }.by(-1)
end
it "editing an existing enterprise" do
@enterprise = create(:enterprise)
e2 = create(:enterprise)