From 7c2242544e664df79294a4ee2a4361c8daff3fdf Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Thu, 17 Apr 2025 17:07:04 +0100 Subject: [PATCH] Add cancel functionality for scheduled name changes with confirmation page --- .../organisation_name_changes_controller.rb | 14 +++++++++ app/models/organisation.rb | 2 ++ app/models/organisation_name_change.rb | 4 +-- .../_name_history_list.html.erb | 7 ++++- .../cancel_confirmation.html.erb | 30 +++++++++++++++++++ config/routes.rb | 2 ++ ...111741_create_organisation_name_changes.rb | 2 +- 7 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 app/views/organisation_name_changes/cancel_confirmation.html.erb diff --git a/app/controllers/organisation_name_changes_controller.rb b/app/controllers/organisation_name_changes_controller.rb index dc32b2ddc..48cf785cc 100644 --- a/app/controllers/organisation_name_changes_controller.rb +++ b/app/controllers/organisation_name_changes_controller.rb @@ -18,6 +18,20 @@ class OrganisationNameChangesController < ApplicationController render :new, layout: "application" end + def cancel_confirmation + @organisation_name_change = OrganisationNameChange.find(params[:change_id]) + render :cancel_confirmation, layout: "application" + end + + def cancel + @organisation_name_change = OrganisationNameChange.find(params[:change_id]) + if @organisation_name_change.update_column(:discarded_at, Time.zone.today) + redirect_to organisation_path(@organisation_name_change.organisation), notice: "The scheduled name change has been successfully cancelled." + else + redirect_to organisation_path(@organisation_name_change.organisation), notice: "Failed to cancel the scheduled name change." + end + end + private def set_organisation diff --git a/app/models/organisation.rb b/app/models/organisation.rb index a4c64b1f3..f4d11dd3f 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -206,6 +206,7 @@ class Organisation < ApplicationRecord def name_changes_with_dates changes = organisation_name_changes.visible.order(:startdate).map do |change| { + id: change.id, name: change.name, start_date: change.startdate, end_date: change.end_date, @@ -214,6 +215,7 @@ class Organisation < ApplicationRecord end changes.unshift( + id: nil, name: self[:name], start_date: created_at, end_date: changes.first&.dig(:start_date)&.yesterday, diff --git a/app/models/organisation_name_change.rb b/app/models/organisation_name_change.rb index e3fb7b789..09b30efe3 100644 --- a/app/models/organisation_name_change.rb +++ b/app/models/organisation_name_change.rb @@ -33,7 +33,7 @@ class OrganisationNameChange < ApplicationRecord end def next_change - organisation.organisation_name_changes.where("startdate > ?", startdate).order(startdate: :asc).first + organisation.organisation_name_changes.visible.where("startdate > ?", startdate).order(startdate: :asc).first end def end_date @@ -41,7 +41,7 @@ class OrganisationNameChange < ApplicationRecord end def previous_change - organisation.organisation_name_changes.where("startdate < ?", startdate).order(startdate: :desc).first + organisation.organisation_name_changes.visible.where("startdate < ?", startdate).order(startdate: :desc).first end def active?(date = Time.zone.now) diff --git a/app/views/organisation_name_changes/_name_history_list.html.erb b/app/views/organisation_name_changes/_name_history_list.html.erb index 9603b6cb0..dc7d604aa 100644 --- a/app/views/organisation_name_changes/_name_history_list.html.erb +++ b/app/views/organisation_name_changes/_name_history_list.html.erb @@ -14,7 +14,12 @@ <% row.with_cell(text: change[:name]) %> <% row.with_cell(text: change[:start_date]&.to_formatted_s(:govuk_date)) %> <% row.with_cell(text: change[:end_date]&.to_formatted_s(:govuk_date) || "None") %> - <% row.with_cell text: status_tag(change[:status].to_sym) %> + <% row.with_cell do %> + <%= status_tag(change[:status].to_sym, ["govuk-!-margin-right-2 govuk-!-margin-bottom-1"]) %> + <% if change[:status] == "scheduled" && change[:id].present? %> + <%= govuk_link_to "Cancel", cancel_name_change_confirmation_organisation_url(change_id: change[:id]), class: "app-red-link app-red-link---no-visited-state" %> + <% end %> + <% end %> <% end %> <% end %> <% end %> diff --git a/app/views/organisation_name_changes/cancel_confirmation.html.erb b/app/views/organisation_name_changes/cancel_confirmation.html.erb new file mode 100644 index 000000000..9810067a5 --- /dev/null +++ b/app/views/organisation_name_changes/cancel_confirmation.html.erb @@ -0,0 +1,30 @@ +<% content_for :before_content do %> + <% content_for :title, "Are you sure you want to cancel this name change?" %> + <%= govuk_back_link(href: change_name_organisation_path(@organisation_name_change.organisation)) %> +<% end %> + +