diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index d12968cc7..d08a14209 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -27,9 +27,18 @@ class LocationsController < ApplicationController def edit; end + def edit_name; end + def update + page = params[:location][:page] + if @location.update(location_params) - location_params[:add_another_location] == "Yes" ? redirect_to(new_location_path(@location.scheme)) : redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) + case page + when "edit" + location_params[:add_another_location] == "Yes" ? redirect_to(new_location_path(@location.scheme)) : redirect_to(scheme_check_answers_path(@scheme, anchor: "locations")) + when "edit-name" + redirect_to(locations_path(@scheme)) + end else render :edit, status: :unprocessable_entity end @@ -38,7 +47,7 @@ class LocationsController < ApplicationController private def find_scheme - @scheme = if %w[new create index].include?(action_name) + @scheme = if %w[new create index edit_name].include?(action_name) Scheme.find(params[:id]) else @location.scheme @@ -46,7 +55,7 @@ private end def find_location - @location = Location.find(params[:id]) + @location = params[:location_id].present? ? Location.find(params[:location_id]) : Location.find(params[:id]) end def authenticate_scope! @@ -54,7 +63,7 @@ private end def authenticate_action! - if %w[new edit update create index].include?(action_name) && !((current_user.organisation == @scheme.owning_organisation) || current_user.support?) + if %w[new edit update create index edit_name].include?(action_name) && !((current_user.organisation == @scheme.owning_organisation) || current_user.support?) render_not_found and return end end diff --git a/app/views/locations/edit.html.erb b/app/views/locations/edit.html.erb index bbcccdef6..e8a38ecb3 100644 --- a/app/views/locations/edit.html.erb +++ b/app/views/locations/edit.html.erb @@ -57,6 +57,8 @@ inline: true, legend: { text: "Do you want to add another location?", size: "m" } %> + <%= f.hidden_field :page, value: "edit" %> + <%= f.govuk_submit "Save and continue" %> diff --git a/app/views/locations/edit_name.html.erb b/app/views/locations/edit_name.html.erb new file mode 100644 index 000000000..22ce4be1f --- /dev/null +++ b/app/views/locations/edit_name.html.erb @@ -0,0 +1,26 @@ +<% content_for :title, "Location name for #{@location.postcode}" %> + +<% content_for :before_content do %> + <%= govuk_back_link( + text: "Back", + href: "/schemes/#{@scheme.id}/locations", + ) %> +<% end %> + +<%= render partial: "organisations/headings", locals: { main: "Location name for #{@location.postcode}", sub: @scheme.service_name } %> + +<%= form_for(@location, method: :patch, url: location_path(location_id: @location.id)) do |f| %> +
+
+ <%= f.govuk_error_summary %> + + <%= f.govuk_text_field :name, + label: { hidden: true }, + hint: { text: "This is how you refer to this location within your organisation" } %> + + <%= f.hidden_field :page, value: "edit-name" %> + + <%= f.govuk_submit "Save and continue" %> +
+
+<% end %> diff --git a/config/routes.rb b/config/routes.rb index c7635c860..de42be41a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -46,7 +46,7 @@ Rails.application.routes.draw do member do resources :locations do - get "edit-name", to: "locations#edit-name" + get "edit-name", to: "locations#edit_name" end end end diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb index b7c34583b..43f966c1c 100644 --- a/spec/features/schemes_spec.rb +++ b/spec/features/schemes_spec.rb @@ -681,7 +681,7 @@ RSpec.describe "Schemes scheme Features" do context "when I click to see individual scheme" do let(:scheme) { schemes.first } - let!(:location) { FactoryBot.create(:location, scheme: scheme) } + let!(:location) { FactoryBot.create(:location, scheme:) } before do click_link(scheme.service_name) @@ -735,12 +735,36 @@ RSpec.describe "Schemes scheme Features" do context "when I click to change location name" do before do - click_link(scheme.locations.first.postcode) + click_link(location.postcode) end it "shows available fields to edit" do - expect(page).to have_current_path("/schemes/#{scheme.id}/locations/1/edit-name") - expect(page).to have_content "Location name for #{scheme.locations.first.postcode}" + expect(page).to have_current_path("/schemes/#{scheme.id}/locations/#{location.id}/edit-name") + expect(page).to have_content "Location name for #{location.postcode}" + end + + context "when I press the back button" do + before do + click_link "Back" + end + + it "I see location details" do + expect(page).to have_content scheme.locations.first.id + expect(page).to have_current_path("/schemes/#{scheme.id}/locations") + end + end + + context "and I change the location name" do + before do + fill_in "location-name-field", with: "NewName" + click_button "Save and continue" + end + + it "returns to locations page and shows the new name" do + expect(page).to have_content location.id + expect(page).to have_content "NewName" + expect(page).to have_current_path("/schemes/#{scheme.id}/locations") + end end end end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index be23b8a04..8e1cde0ac 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -390,7 +390,7 @@ RSpec.describe LocationsController, type: :request do let(:user) { FactoryBot.create(:user, :data_coordinator) } let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:location) { FactoryBot.create(:location, scheme:) } - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } before do sign_in user @@ -413,7 +413,7 @@ RSpec.describe LocationsController, type: :request do end context "when postcode is submitted with lower case" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz", page: "edit" } } } it "updates existing location for scheme with postcode " do expect(Location.last.postcode).to eq("ZZ11ZZ") @@ -423,7 +423,7 @@ RSpec.describe LocationsController, type: :request do context "when trying to update location for a scheme that belongs to another organisation" do let(:another_scheme) { FactoryBot.create(:scheme) } let(:another_location) { FactoryBot.create(:location) } - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } it "displays the new page with an error message" do patch "/schemes/#{another_scheme.id}/locations/#{another_location.id}", params: params @@ -432,7 +432,7 @@ RSpec.describe LocationsController, type: :request do end context "when required postcode param is invalid" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "invalid" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "invalid", page: "edit" } } } it "displays the new page with an error message" do expect(response).to have_http_status(:unprocessable_entity) @@ -441,7 +441,7 @@ RSpec.describe LocationsController, type: :request do end context "when do you want to add another location is selected as yes" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates existing location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -459,7 +459,7 @@ RSpec.describe LocationsController, type: :request do end context "when do you want to add another location is selected as no" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates existing location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -477,7 +477,7 @@ RSpec.describe LocationsController, type: :request do end context "when do you want to add another location is not selected" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates existing location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -499,7 +499,7 @@ RSpec.describe LocationsController, type: :request do let(:user) { FactoryBot.create(:user, :data_coordinator) } let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:location) { FactoryBot.create(:location, scheme:) } - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } before do allow(user).to receive(:need_two_factor_authentication?).and_return(false) @@ -522,7 +522,7 @@ RSpec.describe LocationsController, type: :request do end context "when postcode is submitted with lower case" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz", page: "edit" } } } it "updates a location for scheme with postcode " do expect(Location.last.postcode).to eq("ZZ11ZZ") @@ -530,7 +530,7 @@ RSpec.describe LocationsController, type: :request do end context "when required postcode param is missing" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "invalid" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "invalid", page: "edit" } } } it "displays the new page with an error message" do expect(response).to have_http_status(:unprocessable_entity) @@ -539,7 +539,7 @@ RSpec.describe LocationsController, type: :request do end context "when do you want to add another location is selected as yes" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -556,7 +556,7 @@ RSpec.describe LocationsController, type: :request do end context "when do you want to add another location is selected as no" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates a location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -573,7 +573,7 @@ RSpec.describe LocationsController, type: :request do end context "when do you want to add another location is not selected" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates a location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -819,7 +819,7 @@ RSpec.describe LocationsController, type: :request do expect(page).to have_content("Location name for #{location.postcode}") end - context "when trying to new location to a scheme that belongs to another organisation" do + context "when trying to edit location name of location that belongs to another organisation" do let(:another_scheme) { FactoryBot.create(:scheme) } let(:another_location) { FactoryBot.create(:location, scheme: another_scheme) }