From 1bce3cf9c60318478883d925493c1415067a16e0 Mon Sep 17 00:00:00 2001 From: D Chua Date: Thu, 25 Jul 2024 11:54:44 +0100 Subject: [PATCH] Add functionality to delete retired apps Retired apps are being flagged in the Out of sync release alert and there is no way to remove them in the Release app. This commit allows an app to have the option of being deleted on the Edit page. There is a popup confirmation dialog when the delete button is pressed to ensure it is not removed by mistake. --- app/assets/javascripts/application.js | 2 ++ app/controllers/applications_controller.rb | 7 ++++++- app/views/applications/_form.html.erb | 12 ++++++++++++ .../functional/applications_controller_test.rb | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 1db87ae58..bd365c81d 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -1,3 +1,5 @@ //= require_tree . //= require govuk_publishing_components/dependencies //= require govuk_publishing_components/all_components + +//= require rails-ujs diff --git a/app/controllers/applications_controller.rb b/app/controllers/applications_controller.rb index 0c62b277f..89573d94f 100644 --- a/app/controllers/applications_controller.rb +++ b/app/controllers/applications_controller.rb @@ -1,5 +1,5 @@ class ApplicationsController < ApplicationController - before_action :find_application, only: %i[show edit update deploy stats] + before_action :find_application, only: %i[show edit update deploy stats destroy] include ActionView::Helpers::DateHelper @@ -74,6 +74,11 @@ def update end end + def destroy + @application.destroy! + redirect_to applications_path, notice: "Successfully deleted application" + end + private def find_application diff --git a/app/views/applications/_form.html.erb b/app/views/applications/_form.html.erb index ffdaac984..3d9ed6acf 100644 --- a/app/views/applications/_form.html.erb +++ b/app/views/applications/_form.html.erb @@ -61,5 +61,17 @@ <%= render "govuk_publishing_components/components/button", { text: @application.new_record? ? "Create Application" : "Update application", + margin_bottom: 4, } %> <% end %> + +<% unless current_page?(action: 'new') %> + <%= form_for @application, method: :delete do |f| %> + <%= render "govuk_publishing_components/components/button", { + text: "Delete application", + destructive: true, + data_attributes: { confirm: "Are you sure you wish to delete this application?" }, + info_text: "You can remove this app if it is retired.", + } %> + <% end %> +<% end %> diff --git a/test/functional/applications_controller_test.rb b/test/functional/applications_controller_test.rb index b2c31d2fb..f91983627 100644 --- a/test/functional/applications_controller_test.rb +++ b/test/functional/applications_controller_test.rb @@ -326,6 +326,24 @@ class ApplicationsControllerTest < ActionController::TestCase end end + context "POST destroy" do + setup do + stub_request(:get, Repo::REPO_JSON_URL).to_return(status: 200) + @app = FactoryBot.create(:application) + end + + should "delete the application" do + assert_difference "Application.count", -1 do + post :destroy, params: { id: @app.id } + end + end + + should "redirect to the index page" do + post :destroy, params: { id: @app.id } + assert_redirected_to applications_path + end + end + private def random_sha