diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 3dc642345..a76c9db52 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -150,7 +150,7 @@ class SchemesController < ApplicationController @scheme.update!(secondary_client_group: nil) if @scheme.has_other_client_group == "No" if scheme_params[:confirmed] == "true" || @scheme.confirmed? if check_answers && should_direct_via_secondary_client_group_page?(page) - redirect_to scheme_secondary_client_group_path(@scheme, referrer: "check-answers") + redirect_to scheme_secondary_client_group_path(@scheme, referrer: "has-other-client-group") else @scheme.locations.update!(confirmed: true) flash[:notice] = if scheme_previously_confirmed @@ -162,7 +162,7 @@ class SchemesController < ApplicationController end elsif check_answers if should_direct_via_secondary_client_group_page?(page) - redirect_to scheme_secondary_client_group_path(@scheme, referrer: "check-answers") + redirect_to scheme_secondary_client_group_path(@scheme, referrer: "has-other-client-group") else redirect_to scheme_check_answers_path(@scheme) end diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index 12d86aba8..f262c08ff 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -101,6 +101,24 @@ module SchemesHelper organisation.owned_schemes.duplicate_sets.any? || organisation.owned_schemes.any? { |scheme| scheme.locations.duplicate_sets.any? } end + def scheme_back_button_path(scheme, current_page) + return scheme_check_answers_path(scheme) if request.params[:referrer] == "check-answers" + return scheme_confirm_secondary_client_group_path(scheme, referrer: "check-answers") if request.params[:referrer] == "has-other-client-group" + + case current_page + when "details" + schemes_path + when "primary_client_group" + scheme_details_path(scheme) + when "confirm_secondary_client_group" + scheme_primary_client_group_path(scheme) + when "secondary_client_group" + scheme_confirm_secondary_client_group_path(scheme) + when "support" + scheme.has_other_client_group == "Yes" ? scheme_secondary_client_group_path(scheme) : scheme_confirm_secondary_client_group_path(scheme) + end + end + private ActivePeriod = Struct.new(:from, :to) diff --git a/app/views/schemes/confirm_secondary.html.erb b/app/views/schemes/confirm_secondary.html.erb index 9b715c264..9feca87bc 100644 --- a/app/views/schemes/confirm_secondary.html.erb +++ b/app/views/schemes/confirm_secondary.html.erb @@ -1,7 +1,7 @@ <% content_for :title, "Does this scheme provide for another client group?" %> <% content_for :before_content do %> - <%= govuk_back_link(href: :back) %> + <%= govuk_back_link(href: scheme_back_button_path(@scheme, "confirm_secondary_client_group")) %> <% end %> <%= render partial: "organisations/headings", locals: { main: "Does this scheme provide for another client group?", sub: @scheme.service_name } %> diff --git a/app/views/schemes/details.html.erb b/app/views/schemes/details.html.erb index 4b23ab016..d1943cabe 100644 --- a/app/views/schemes/details.html.erb +++ b/app/views/schemes/details.html.erb @@ -1,7 +1,7 @@ <% content_for :title, "Create a new supported housing scheme" %> <% content_for :before_content do %> - <%= govuk_back_link(href: :back) %> + <%= govuk_back_link(href: scheme_back_button_path(@scheme, "details")) %> <% end %> <%= form_for(@scheme, method: :patch) do |f| %> diff --git a/app/views/schemes/primary_client_group.html.erb b/app/views/schemes/primary_client_group.html.erb index 55dc504b4..68da2f35c 100644 --- a/app/views/schemes/primary_client_group.html.erb +++ b/app/views/schemes/primary_client_group.html.erb @@ -1,13 +1,7 @@ <% content_for :title, "What client group is this scheme intended for?" %> -<% if request.referer&.include?("new") %> - <% back_button_path = scheme_details_path(@scheme) %> -<% else %> - <% back_button_path = :back %> -<% end %> - <% content_for :before_content do %> - <%= govuk_back_link(href: back_button_path) %> + <%= govuk_back_link(href: scheme_back_button_path(@scheme, "primary_client_group")) %> <% end %> <%= form_for(@scheme, method: :patch) do |f| %> diff --git a/app/views/schemes/secondary_client_group.html.erb b/app/views/schemes/secondary_client_group.html.erb index cd50283e3..8200702d2 100644 --- a/app/views/schemes/secondary_client_group.html.erb +++ b/app/views/schemes/secondary_client_group.html.erb @@ -1,7 +1,7 @@ <% content_for :title, "What is the other client group?" %> <% content_for :before_content do %> - <%= govuk_back_link(href: :back) %> + <%= govuk_back_link(href: scheme_back_button_path(@scheme, "secondary_client_group")) %> <% end %> <%= form_for(@scheme, method: :patch) do |f| %> diff --git a/app/views/schemes/support.html.erb b/app/views/schemes/support.html.erb index a5d30ed7f..eca72014f 100644 --- a/app/views/schemes/support.html.erb +++ b/app/views/schemes/support.html.erb @@ -1,7 +1,7 @@ <% content_for :title, "What support does this scheme provide?" %> <% content_for :before_content do %> - <%= govuk_back_link(href: :back) %> + <%= govuk_back_link(href: scheme_back_button_path(@scheme, "support")) %> <% end %> <%= form_for(@scheme, method: :patch) do |f| %> diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 83ba11fd9..2eb2330c8 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -2035,6 +2035,17 @@ RSpec.describe SchemesController, type: :request do expect(page).to have_content("What client group is this scheme intended for?") end + it "has correct back link" do + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/details") + end + + context "and accessed from check answers" do + it "has correct back link" do + get "/schemes/#{scheme.id}/primary-client-group?referrer=check-answers" + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/check-answers") + end + end + context "when attempting to access primary-client-group scheme page for another organisation" do before do get "/schemes/#{another_scheme.id}/primary-client-group" @@ -2112,6 +2123,17 @@ RSpec.describe SchemesController, type: :request do expect(page).to have_content("Does this scheme provide for another client group?") end + it "has correct back link" do + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/primary-client-group") + end + + context "and accessed from check answers" do + it "has correct back link" do + get "/schemes/#{scheme.id}/confirm-secondary-client-group?referrer=check-answers" + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/check-answers") + end + end + context "when attempting to access confirm-secondary-client-group scheme page for another organisation" do before do get "/schemes/#{another_scheme.id}/confirm-secondary-client-group" @@ -2189,6 +2211,24 @@ RSpec.describe SchemesController, type: :request do expect(page).to have_content("What is the other client group?") end + it "has correct back link" do + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/confirm-secondary-client-group") + end + + context "and accessed from check answers" do + it "has correct back link" do + get "/schemes/#{scheme.id}/secondary-client-group?referrer=check-answers" + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/check-answers") + end + end + + context "and accessed from has other client group" do + it "has correct back link" do + get "/schemes/#{scheme.id}/secondary-client-group?referrer=has-other-client-group" + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/confirm-secondary-client-group?referrer=check-answers") + end + end + context "when attempting to access secondary-client-group scheme page for another organisation" do before do get "/schemes/#{another_scheme.id}/secondary-client-group" @@ -2258,7 +2298,7 @@ RSpec.describe SchemesController, type: :request do context "when signed in as a data coordinator" do let(:user) { create(:user, :data_coordinator) } - let(:scheme) { create(:scheme, owning_organisation: user.organisation, confirmed: nil) } + let(:scheme) { create(:scheme, owning_organisation: user.organisation, confirmed: nil, has_other_client_group: "Yes") } let(:another_scheme) { create(:scheme, confirmed: nil) } before do @@ -2271,6 +2311,27 @@ RSpec.describe SchemesController, type: :request do expect(page).to have_content("What support does this scheme provide?") end + context "when scheme has secondary client group" do + it "has correct back link" do + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/secondary-client-group") + end + end + + context "when scheme has no secondary client group" do + let(:scheme) { create(:scheme, owning_organisation: user.organisation, confirmed: nil, has_other_client_group: "No") } + + it "has correct back link" do + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/confirm-secondary-client-group") + end + end + + context "and accessed from check answers" do + it "has correct back link" do + get "/schemes/#{scheme.id}/support?referrer=check-answers" + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/check-answers") + end + end + context "when attempting to access secondary-client-group scheme page for another organisation" do before do get "/schemes/#{another_scheme.id}/support" @@ -2433,6 +2494,17 @@ RSpec.describe SchemesController, type: :request do expect(page).to have_content("Create a new supported housing scheme") end + it "has correct back link" do + expect(page).to have_link("Back", href: "/schemes") + end + + context "and accessed from check answers" do + it "has correct back link" do + get "/schemes/#{scheme.id}/details?referrer=check-answers" + expect(page).to have_link("Back", href: "/schemes/#{scheme.id}/check-answers") + end + end + context "when attempting to access check-answers scheme page for another organisation" do before do get "/schemes/#{another_scheme.id}/details"