Browse Source

finilised

pull/719/head
JG 3 years ago
parent
commit
866daca350
  1. 17
      app/controllers/locations_controller.rb
  2. 2
      app/views/locations/edit.html.erb
  3. 26
      app/views/locations/edit_name.html.erb
  4. 2
      config/routes.rb
  5. 32
      spec/features/schemes_spec.rb
  6. 28
      spec/requests/locations_controller_spec.rb

17
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

2
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" %>
</div>
</div>

26
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| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= 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" %>
</div>
</div>
<% end %>

2
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

32
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

28
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) }

Loading…
Cancel
Save