Browse Source

Add cancel functionality for scheduled name changes with confirmation page

pull/3058/head
Manny Dinssa 2 weeks ago
parent
commit
7c2242544e
  1. 14
      app/controllers/organisation_name_changes_controller.rb
  2. 2
      app/models/organisation.rb
  3. 4
      app/models/organisation_name_change.rb
  4. 7
      app/views/organisation_name_changes/_name_history_list.html.erb
  5. 30
      app/views/organisation_name_changes/cancel_confirmation.html.erb
  6. 2
      config/routes.rb
  7. 2
      db/migrate/20250416111741_create_organisation_name_changes.rb

14
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

2
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,

4
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)

7
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 %>

30
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 %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<span class="govuk-caption-xl">Cancel scheduled name change </span>
<h1 class="govuk-heading-xl">
<%= content_for(:title) %>
</h1>
<%= govuk_warning_text(text: "You will not be able to undo this action.") %>
<div class="govuk-inset-text">
This name change would have updated the organisation name from
<strong><%= @organisation_name_change.previous_change.is_a?(OrganisationNameChange) ? @organisation_name_change.previous_change.name : @organisation_name_change.organisation.name %></strong>
to <strong><%= @organisation_name_change.name %></strong>.
</div>
<div class="govuk-button-group">
<%= govuk_button_to(
"Cancel",
cancel_name_change_organisation_path(@organisation_name_change),
method: :delete,
) %>
<%= govuk_button_link_to "Back", change_name_organisation_path(@organisation_name_change.organisation), secondary: true %>
</div>
</div>
</div>

2
config/routes.rb

@ -158,6 +158,8 @@ Rails.application.routes.draw do
post "data-sharing-agreement", to: "organisations#confirm_data_sharing_agreement"
get "change-name", to: "organisation_name_changes#change_name", as: "change_name"
post "change-name", to: "organisation_name_changes#create"
get "cancel-name-change/:change_id", to: "organisation_name_changes#cancel_confirmation", as: "cancel_name_change_confirmation"
delete "cancel-name-change/:change_id", to: "organisation_name_changes#cancel", as: "cancel_name_change"
get "users", to: "organisations#users"
get "lettings-logs", to: "organisations#lettings_logs"

2
db/migrate/20250416111741_create_organisation_name_changes.rb

@ -9,6 +9,6 @@ class CreateOrganisationNameChanges < ActiveRecord::Migration[7.0]
t.timestamps
end
add_index :organisation_name_changes, %i[organisation_id startdate], unique: true, name: "index_org_name_changes_on_org_id_and_startdate"
add_index :organisation_name_changes, %i[organisation_id startdate discarded_at], unique: true, name: "index_org_name_changes_on_org_id_startdate_discarded_at"
end
end

Loading…
Cancel
Save