Browse Source

Display historic merges

pull/1996/head
Kat 3 years ago
parent
commit
a2393a3874
  1. 20
      app/models/organisation.rb
  2. 34
      app/views/organisations/_merged_organisation_details.html.erb
  3. 11
      spec/requests/organisations_controller_spec.rb

20
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

34
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 %>
<p>Merge date: <%= @organisation.recent_merge_date&.to_formatted_s(:govuk_date) %></p>
<%= 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| %>
<p><strong>Merge date:</strong> <%= merge_date&.to_formatted_s(:govuk_date) %></p>
<%= 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 %>

11
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

Loading…
Cancel
Save