diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 71ae5c2f08..f9e8fedcf9 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -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 diff --git a/app/helpers/enterprises_helper.rb b/app/helpers/enterprises_helper.rb index 367b69b690..bad58a9c52 100644 --- a/app/helpers/enterprises_helper.rb +++ b/app/helpers/enterprises_helper.rb @@ -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 diff --git a/app/views/admin/enterprises/_admin_index.html.haml b/app/views/admin/enterprises/_admin_index.html.haml index ce2d88f51b..5b97a5462d 100644 --- a/app/views/admin/enterprises/_admin_index.html.haml +++ b/app/views/admin/enterprises/_admin_index.html.haml @@ -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 diff --git a/app/views/admin/enterprises/destroy.turbo_stream.haml b/app/views/admin/enterprises/destroy.turbo_stream.haml new file mode 100644 index 0000000000..96df716a2a --- /dev/null +++ b/app/views/admin/enterprises/destroy.turbo_stream.haml @@ -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 }) diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index a26d515bd3..bcca16ebf4 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -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)