Browse Source

Add scheme outcomes page

pull/2604/head
Kat 2 years ago
parent
commit
1537da2f96
  1. 20
      app/helpers/merge_requests_helper.rb
  2. 14
      app/models/merge_request.rb
  3. 4
      app/models/organisation.rb
  4. 23
      app/views/merge_requests/scheme_outcomes.html.erb
  5. 57
      spec/helpers/merge_requests_helper_spec.rb
  6. 148
      spec/models/merge_request_spec.rb
  7. 27
      spec/requests/merge_requests_controller_spec.rb

20
app/helpers/merge_requests_helper.rb

@ -107,4 +107,24 @@ module MergeRequestsHelper
count = merge_request.total_visible_users_after_merge
"#{"#{count} user".pluralize(count)} after merge"
end
def merging_organisations_without_schemes_text(organisations)
return "" unless organisations.count.positive?
if organisations.count == 1
"#{organisations.first.name} has no schemes."
else
"#{organisations.map(&:name).to_sentence} have no schemes."
end
end
def link_to_merging_organisation_schemes(organisation)
count_text = organisation.owned_schemes.count == 1 ? "1 #{organisation.name} scheme" : "all #{organisation.owned_schemes.count} #{organisation.name} schemes"
govuk_link_to "View #{count_text} (opens in a new tab)", schemes_organisation_path(organisation), target: "_blank"
end
def total_schemes_after_merge_text(merge_request)
count = merge_request.total_visible_schemes_after_merge
"#{"#{count} scheme".pluralize(count)} after merge"
end
end

14
app/models/merge_request.rb

@ -84,6 +84,18 @@ class MergeRequest < ApplicationRecord
end
def total_schemes_label
"#{total_visible_schemes_after_merge} Schemes"
"#{total_visible_schemes_after_merge} #{'Scheme'.pluralize(total_visible_schemes_after_merge)}"
end
def organisations_with_schemes
return [] unless absorbing_organisation.present? && merging_organisations.any?
([absorbing_organisation] + merging_organisations).select(&:has_visible_schemes?)
end
def organisations_without_schemes
return [] unless absorbing_organisation.present? && merging_organisations.any?
([absorbing_organisation] + merging_organisations).reject(&:has_visible_schemes?)
end
end

4
app/models/organisation.rb

@ -195,4 +195,8 @@ class Organisation < ApplicationRecord
def has_visible_users?
users.visible.count.positive?
end
def has_visible_schemes?
owned_schemes.visible.count.positive?
end
end

23
app/views/merge_requests/scheme_outcomes.html.erb

@ -0,0 +1,23 @@
<% content_for :before_content do %>
<% title = "Schemes" %>
<% content_for :title, title %>
<%= govuk_back_link href: organisations_path(tab: "merge-requests") %>
<% end %>
<h1 class="govuk-heading-l">
<span class="govuk-caption-l"><%= @merge_request.absorbing_organisation_name %></span>
Schemes
</h1>
<% unless @merge_request.status == "request_merged" || @merge_request.status == "processing" %>
<h2 class="govuk-heading-m"><%= total_schemes_after_merge_text(@merge_request) %></h2>
<p class="govuk-body">
<%= merging_organisations_without_schemes_text(@merge_request.organisations_without_schemes) %>
</p>
<% @merge_request.organisations_with_schemes.map do |org| %>
<p class="govuk-body">
<%= link_to_merging_organisation_schemes(org) %>
</p>
<% end %>
<% end %>

57
spec/helpers/merge_requests_helper_spec.rb

@ -53,4 +53,61 @@ RSpec.describe MergeRequestsHelper do
end
end
end
describe "#merging_organisations_without_schemes_text" do
context "with 1 organisation" do
let(:organisation) { build(:organisation, name: "Org 1") }
it "returns the correct text" do
expect(merging_organisations_without_schemes_text([organisation])).to eq("Org 1 has no schemes.")
end
end
context "with 2 organisations" do
let(:organisation) { build(:organisation, name: "Org 1") }
let(:organisation_2) { build(:organisation, name: "Org 2") }
it "returns the correct text" do
expect(merging_organisations_without_schemes_text([organisation, organisation_2])).to eq("Org 1 and Org 2 have no schemes.")
end
end
context "with 3 organisations" do
let(:organisation) { build(:organisation, name: "Org 1") }
let(:organisation_2) { build(:organisation, name: "Org 2") }
let(:organisation_3) { build(:organisation, name: "Org 3") }
it "returns the correct text" do
expect(merging_organisations_without_schemes_text([organisation, organisation_2, organisation_3])).to eq("Org 1, Org 2, and Org 3 have no schemes.")
end
end
end
describe "#link_to_merging_organisation_schemes" do
context "with 1 organisation scheme" do
let(:organisation) { create(:organisation, name: "Org 1") }
before do
create(:scheme, owning_organisation: organisation)
end
it "returns the correct link" do
expect(link_to_merging_organisation_schemes(organisation)).to include("View 1 Org 1 scheme (opens in a new tab)")
expect(link_to_merging_organisation_schemes(organisation)).to include(schemes_organisation_path(organisation))
end
end
context "with multiple organisation schemes" do
let(:organisation) { create(:organisation, name: "Org 1") }
before do
create_list(:scheme, 2, owning_organisation: organisation)
end
it "returns the correct link" do
expect(link_to_merging_organisation_schemes(organisation)).to include("View all 2 Org 1 schemes (opens in a new tab)")
expect(link_to_merging_organisation_schemes(organisation)).to include(schemes_organisation_path(organisation))
end
end
end
end

148
spec/models/merge_request_spec.rb

@ -168,6 +168,78 @@ RSpec.describe MergeRequest, type: :model do
end
end
describe "#organisations_with_schemes" do
let(:merge_request) { create(:merge_request, absorbing_organisation:) }
let(:absorbing_organisation) { create(:organisation) }
let(:merging_organisation_1) { create(:organisation) }
let(:merging_organisation_2) { create(:organisation) }
before do
create(:merge_request_organisation, merge_request:, merging_organisation: merging_organisation_1)
create(:merge_request_organisation, merge_request:, merging_organisation: merging_organisation_2)
end
context "when absorbing organisation has schemes" do
before do
create(:scheme, owning_organisation: absorbing_organisation)
end
context "and some merging organisations have schemes" do
before do
create(:scheme, owning_organisation: merging_organisation_1)
end
it "returns correct organisations with schemes" do
expect(absorbing_organisation.owned_schemes.count).to eq(1)
expect(merging_organisation_1.owned_schemes.count).to eq(1)
expect(merging_organisation_2.owned_schemes.count).to eq(0)
expect(merge_request.organisations_with_schemes.count).to eq(2)
expect(merge_request.organisations_with_schemes).to include(merging_organisation_1)
expect(merge_request.organisations_with_schemes).to include(absorbing_organisation)
end
end
context "and no merging organisations have schemes" do
it "returns correct organisations with schemes" do
expect(absorbing_organisation.owned_schemes.count).to eq(1)
expect(merging_organisation_1.owned_schemes.count).to eq(0)
expect(merging_organisation_2.owned_schemes.count).to eq(0)
expect(merge_request.organisations_with_schemes.count).to eq(1)
expect(merge_request.organisations_with_schemes).to include(absorbing_organisation)
end
end
end
context "when absorbing organisation has no schemes" do
context "and some merging organisations have schemes" do
before do
create(:scheme, owning_organisation: merging_organisation_1)
end
it "returns correct organisations with schemes" do
expect(merging_organisation_1.owned_schemes.count).to eq(1)
expect(absorbing_organisation.owned_schemes.count).to eq(0)
expect(merging_organisation_2.owned_schemes.count).to eq(0)
expect(merge_request.organisations_with_schemes.count).to eq(1)
expect(merge_request.organisations_with_schemes).to include(merging_organisation_1)
end
end
context "and no merging organisations have schemes" do
it "returns correct organisations with schemes" do
expect(absorbing_organisation.owned_schemes.count).to eq(0)
expect(merging_organisation_1.owned_schemes.count).to eq(0)
expect(merging_organisation_2.owned_schemes.count).to eq(0)
expect(merge_request.organisations_with_schemes.count).to eq(0)
end
end
end
end
describe "#organisations_without_users" do
context "when absorbing organisation has users" do
let(:merge_request) { create(:merge_request, absorbing_organisation:) }
@ -249,4 +321,80 @@ RSpec.describe MergeRequest, type: :model do
end
end
end
describe "#organisations_without_schemes" do
let(:merge_request) { create(:merge_request, absorbing_organisation:) }
let(:absorbing_organisation) { create(:organisation) }
let(:merging_organisation_1) { create(:organisation) }
let(:merging_organisation_2) { create(:organisation) }
before do
create(:merge_request_organisation, merge_request:, merging_organisation: merging_organisation_1)
create(:merge_request_organisation, merge_request:, merging_organisation: merging_organisation_2)
end
context "when absorbing organisation has schemes" do
before do
create(:scheme, owning_organisation: absorbing_organisation)
end
context "and some merging organisations have schemes" do
before do
create(:scheme, owning_organisation: merging_organisation_1)
end
it "returns correct organisations with schemes" do
expect(absorbing_organisation.owned_schemes.count).to eq(1)
expect(merging_organisation_1.owned_schemes.count).to eq(1)
expect(merging_organisation_2.owned_schemes.count).to eq(0)
expect(merge_request.organisations_without_schemes.count).to eq(1)
expect(merge_request.organisations_without_schemes).to include(merging_organisation_2)
end
end
context "and no merging organisations have schemes" do
it "returns correct organisations with schemes" do
expect(absorbing_organisation.owned_schemes.count).to eq(1)
expect(merging_organisation_1.owned_schemes.count).to eq(0)
expect(merging_organisation_2.owned_schemes.count).to eq(0)
expect(merge_request.organisations_without_schemes.count).to eq(2)
expect(merge_request.organisations_without_schemes).to include(merging_organisation_1)
expect(merge_request.organisations_without_schemes).to include(merging_organisation_2)
end
end
end
context "when absorbing organisation has no schemes" do
context "and some merging organisations have schemes" do
before do
create(:scheme, owning_organisation: merging_organisation_1)
end
it "returns correct organisations with schemes" do
expect(merging_organisation_1.owned_schemes.count).to eq(1)
expect(absorbing_organisation.owned_schemes.count).to eq(0)
expect(merging_organisation_2.owned_schemes.count).to eq(0)
expect(merge_request.organisations_without_schemes.count).to eq(2)
expect(merge_request.organisations_without_schemes).to include(absorbing_organisation)
expect(merge_request.organisations_without_schemes).to include(merging_organisation_2)
end
end
context "and no merging organisations have schemes" do
it "returns correct organisations with schemes" do
expect(absorbing_organisation.owned_schemes.count).to eq(0)
expect(merging_organisation_1.owned_schemes.count).to eq(0)
expect(merging_organisation_2.owned_schemes.count).to eq(0)
expect(merge_request.organisations_without_schemes.count).to eq(3)
expect(merge_request.organisations_without_schemes).to include(absorbing_organisation)
expect(merge_request.organisations_without_schemes).to include(merging_organisation_1)
expect(merge_request.organisations_without_schemes).to include(merging_organisation_2)
end
end
end
end
end

27
spec/requests/merge_requests_controller_spec.rb

@ -546,6 +546,33 @@ RSpec.describe MergeRequestsController, type: :request do
expect(page).to have_content("19 users after merge")
end
end
describe "#scheme_outcomes" do
let(:merge_request) { create(:merge_request, absorbing_organisation: organisation) }
let(:organisation_with_no_schemes) { create(:organisation, name: "Organisation with no schemes") }
let(:organisation_with_no_schemes_too) { create(:organisation, name: "Organisation with no schemes too") }
let(:organisation_with_some_schemes) { create(:organisation, name: "Organisation with some schemes") }
let(:organisation_with_some_more_schemes) { create(:organisation, name: "Organisation with many schemes") }
before do
create_list(:scheme, 4, owning_organisation: organisation_with_some_schemes)
create_list(:scheme, 6, owning_organisation: organisation_with_some_more_schemes)
create_list(:scheme, 3, owning_organisation: organisation)
create(:merge_request_organisation, merge_request:, merging_organisation: organisation_with_no_schemes)
create(:merge_request_organisation, merge_request:, merging_organisation: organisation_with_no_schemes_too)
create(:merge_request_organisation, merge_request:, merging_organisation: organisation_with_some_schemes)
create(:merge_request_organisation, merge_request:, merging_organisation: organisation_with_some_more_schemes)
get "/merge-request/#{merge_request.id}/scheme-outcomes", headers:
end
it "shows scheme outcomes after merge" do
expect(page).to have_link("View all 4 Organisation with some schemes schemes (opens in a new tab)", href: schemes_organisation_path(organisation_with_some_schemes))
expect(page).to have_link("View all 6 Organisation with many schemes schemes (opens in a new tab)", href: schemes_organisation_path(organisation_with_some_more_schemes))
expect(page).to have_link("View all 3 MHCLG schemes (opens in a new tab)", href: schemes_organisation_path(organisation))
expect(page).to have_content("Organisation with no schemes and Organisation with no schemes too have no schemes.")
expect(page).to have_content("13 schemes after merge")
end
end
end
context "when user is signed in with a data coordinator user" do

Loading…
Cancel
Save