From a2393a387435025e71b37e72a3694b8d01aae7c7 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 25 Oct 2023 15:29:03 +0100 Subject: [PATCH] Display historic merges --- app/models/organisation.rb | 20 ++--------- .../_merged_organisation_details.html.erb | 34 ++++++++++--------- .../requests/organisations_controller_spec.rb | 11 +++--- 3 files changed, 27 insertions(+), 38 deletions(-) diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 5ba1f273d..e17d0d6eb 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -33,7 +33,6 @@ class Organisation < ApplicationRecord scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } scope :search_by, ->(param) { search_by_name(param) } scope :merged_during_open_collection_period, -> { where("merge_date >= ?", FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period) } - scope :absorbed_on, ->(date) { where("merge_date = ?", date) } has_paper_trail @@ -142,22 +141,9 @@ class Organisation < ApplicationRecord sales_logs.duplicate_sets.map { |array_str| array_str ? array_str.map(&:to_i) : [] } end - def recent_merge_date - if merge_date.present? - merge_date - elsif absorbed_organisations.any? - absorbed_organisations.map(&:merge_date).compact.max - end - end - - def recently_absorbed_organisations - return unless absorbed_organisations.any? - return absorbed_organisations if recent_merge_date.blank? - - absorbed_organisations.absorbed_on(recent_merge_date) - end + def recently_absorbed_organisations_grouped_by_merge_date + return unless absorbed_organisations.present? && absorbed_organisations.merged_during_open_collection_period.present? - def has_absorbed_organisations_during_open_collection_period? - absorbed_organisations.any? && (recent_merge_date.blank? || recent_merge_date.present? && recent_merge_date > FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period) + absorbed_organisations.merged_during_open_collection_period.group_by(&:merge_date) end end diff --git a/app/views/organisations/_merged_organisation_details.html.erb b/app/views/organisations/_merged_organisation_details.html.erb index ad11ff28c..0f35b5f4a 100644 --- a/app/views/organisations/_merged_organisation_details.html.erb +++ b/app/views/organisations/_merged_organisation_details.html.erb @@ -1,22 +1,24 @@ -<% if @organisation.has_absorbed_organisations_during_open_collection_period? %> +<% if @organisation.recently_absorbed_organisations_grouped_by_merge_date.present? %> <%= govuk_details(summary_text: "View all organisations that were merged into #{@organisation.name}") do %> -

Merge date: <%= @organisation.recent_merge_date&.to_formatted_s(:govuk_date) %>

- <%= govuk_table do |table| %> - <%= table.head do |head| %> - <%= head.row do |row| %> - <% row.cell(header: true, text: "Organisation name", html_attributes: { scope: "col" }) %> - <% row.cell(header: true, text: "Organisation ID", html_attributes: { scope: "col" }) %> + <% @organisation.recently_absorbed_organisations_grouped_by_merge_date.each do |merge_date, organisations| %> +

Merge date: <%= merge_date&.to_formatted_s(:govuk_date) %>

+ <%= govuk_table do |table| %> + <%= table.head do |head| %> + <%= head.row do |row| %> + <% row.cell(header: true, text: "Organisation name", html_attributes: { scope: "col", class: "govuk-!-width-one-half" }) %> + <% row.cell(header: true, text: "Organisation ID", html_attributes: { scope: "col", class: "govuk-!-width-one-half" }) %> + <% end %> <% end %> - <% end %> - <% @organisation.recently_absorbed_organisations&.each do |absorbed_org| %> - <%= table.body do |body| %> - <%= body.row do |row| %> - <% if current_user.support? %> - <% row.cell(text: simple_format(govuk_link_to(absorbed_org.name, organisation_path(absorbed_org)), { class: "govuk-!-font-weight-bold scheme-name-cell" }, wrapper_tag: "div")) %> - <% else %> - <% row.cell(text: absorbed_org.name) %> + <% organisations.each do |absorbed_org| %> + <%= table.body do |body| %> + <%= body.row do |row| %> + <% if current_user.support? %> + <% row.cell(text: simple_format(govuk_link_to(absorbed_org.name, organisation_path(absorbed_org)), { class: "govuk-!-font-weight-bold scheme-name-cell" }, wrapper_tag: "div")) %> + <% else %> + <% row.cell(text: absorbed_org.name) %> + <% end %> + <% row.cell(text: "ORG#{absorbed_org.id}") %> <% end %> - <% row.cell(text: "ORG#{absorbed_org.id}") %> <% end %> <% end %> <% end %> diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 7d27c71b9..340f1facf 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -287,24 +287,25 @@ RSpec.describe OrganisationsController, type: :request do context "when the organisation has absorbed other organisations" do let!(:absorbed_organisation) { create(:organisation, name: "First Absorbed Organisation") } let!(:other_absorbed_organisation) { create(:organisation, name: "Other Absorbed Organisation") } - let!(:old_absorbed_organisation) { create(:organisation, name: "Old Absorbed Organisation") } + let!(:previously_absorbed_organisation) { create(:organisation, name: "Previously Absorbed Organisation") } before do absorbed_organisation.update!(merge_date: Time.zone.local(2023, 4, 3), absorbing_organisation: organisation) other_absorbed_organisation.update!(merge_date: Time.zone.local(2023, 4, 3), absorbing_organisation: organisation) - old_absorbed_organisation.update!(merge_date: Time.zone.local(2023, 4, 2), absorbing_organisation: organisation) + previously_absorbed_organisation.update!(merge_date: Time.zone.local(2023, 4, 2), absorbing_organisation: organisation) get "/organisations/#{organisation.id}/details", headers:, params: {} end - it "displays a list of absorbed organisations" do + it "displays separate lists of absorbed organisations" do expect(page).to have_content("View all organisations that were merged into #{organisation.name}") expect(page).to have_content("Merge date: 3 April 2023") expect(page).to have_content("First Absorbed Organisation") expect(page).to have_content("Other Absorbed Organisation") - expect(page).not_to have_content("Old Absorbed Organisation") + expect(page).to have_content("Previously Absorbed Organisation") expect(page).to have_content("ORG#{absorbed_organisation.id}") expect(page).to have_content("ORG#{other_absorbed_organisation.id}") - expect(page).not_to have_content("ORG#{old_absorbed_organisation.id}") + expect(page).to have_content("Merge date: 2 April 2023") + expect(page).to have_content("ORG#{previously_absorbed_organisation.id}") end end