Browse Source

feat: add _update methods

pull/1034/head
natdeanlewissoftwire 4 years ago
parent
commit
360ba3b17e
  1. 186
      app/controllers/locations_controller.rb
  2. 16
      config/routes.rb
  3. 4
      spec/features/schemes_helpers.rb
  4. 4
      spec/features/schemes_spec.rb
  5. 18
      spec/requests/locations_controller_spec.rb

186
app/controllers/locations_controller.rb

@ -20,139 +20,139 @@ class LocationsController < ApplicationController
redirect_to scheme_location_postcode_path(@scheme, @location, route: params[:route])
end
def postcode
if params[:location].present?
@location.postcode = PostcodeService.clean(params[:location][:postcode])
if @location.postcode_changed?
@location.location_admin_district = nil
@location.location_code = nil
end
if @location.save(context: :postcode)
if @location.location_code.blank? || @location.location_admin_district.blank?
redirect_to scheme_location_local_authority_path(@scheme, @location, route: params[:route], referrer: params[:referrer])
elsif params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
redirect_to scheme_location_name_path(@scheme, @location, route: params[:route])
end
def postcode; end
def update_postcode
@location.postcode = PostcodeService.clean(params[:location][:postcode])
if @location.postcode_changed?
@location.location_admin_district = nil
@location.location_code = nil
end
if @location.save(context: :postcode)
if @location.location_code.blank? || @location.location_admin_district.blank?
redirect_to scheme_location_local_authority_path(@scheme, @location, route: params[:route], referrer: params[:referrer])
elsif params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
render :postcode, status: :unprocessable_entity
redirect_to scheme_location_name_path(@scheme, @location, route: params[:route])
end
else
render :postcode, status: :unprocessable_entity
end
end
def local_authority
if params[:location].present?
@location.location_admin_district = params[:location][:location_admin_district]
@location.location_code = Location.local_authorities.key(params[:location][:location_admin_district])
if @location.save(context: :location_admin_district)
if params[:referrer] == "check_answers" || params[:referrer] == "check_local_authority"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
redirect_to scheme_location_name_path(@scheme, @location, route: params[:route])
end
def local_authority; end
def update_local_authority
@location.location_admin_district = params[:location][:location_admin_district]
@location.location_code = Location.local_authorities.key(params[:location][:location_admin_district])
if @location.save(context: :location_admin_district)
if params[:referrer] == "check_answers" || params[:referrer] == "check_local_authority"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
render :local_authority, status: :unprocessable_entity
redirect_to scheme_location_name_path(@scheme, @location, route: params[:route])
end
else
render :local_authority, status: :unprocessable_entity
end
end
def name
if params[:location].present?
@location.name = params[:location][:name]
if @location.save(context: :name)
case params[:referrer]
when "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
when "details"
redirect_to scheme_location_path(@scheme, @location)
else
redirect_to scheme_location_units_path(@scheme, @location, route: params[:route])
end
def name; end
def update_name
@location.name = params[:location][:name]
if @location.save(context: :name)
case params[:referrer]
when "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
when "details"
redirect_to scheme_location_path(@scheme, @location)
else
render :name, status: :unprocessable_entity
redirect_to scheme_location_units_path(@scheme, @location, route: params[:route])
end
else
render :name, status: :unprocessable_entity
end
end
def units
if params[:location].present?
@location.units = params[:location][:units]
if @location.save(context: :units)
if params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
redirect_to scheme_location_type_of_unit_path(@scheme, @location, route: params[:route])
end
def units; end
def update_units
@location.units = params[:location][:units]
if @location.save(context: :units)
if params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
render :units, status: :unprocessable_entity
redirect_to scheme_location_type_of_unit_path(@scheme, @location, route: params[:route])
end
else
render :units, status: :unprocessable_entity
end
end
def type_of_unit
if params[:location].present?
@location.type_of_unit = params[:location][:type_of_unit]
if @location.save(context: :type_of_unit)
if params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
redirect_to scheme_location_mobility_standards_path(@scheme, @location, route: params[:route])
end
def type_of_unit; end
def update_type_of_unit
@location.type_of_unit = params[:location][:type_of_unit]
if @location.save(context: :type_of_unit)
if params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
render :type_of_unit, status: :unprocessable_entity
redirect_to scheme_location_mobility_standards_path(@scheme, @location, route: params[:route])
end
else
render :type_of_unit, status: :unprocessable_entity
end
end
def mobility_standards
if params[:location].present?
@location.mobility_type = params[:location][:mobility_type]
if @location.save(context: :mobility_type)
if params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
redirect_to scheme_location_availability_path(@scheme, @location, route: params[:route])
end
def mobility_standards; end
def update_mobility_standards
@location.mobility_type = params[:location][:mobility_type]
if @location.save(context: :mobility_type)
if params[:referrer] == "check_answers"
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
render :mobility_standards, status: :unprocessable_entity
redirect_to scheme_location_availability_path(@scheme, @location, route: params[:route])
end
else
render :mobility_standards, status: :unprocessable_entity
end
end
def availability
if params[:location].present?
day = params[:location]["startdate(3i)"]
month = params[:location]["startdate(2i)"]
year = params[:location]["startdate(1i)"]
if [day, month, year].none?(&:blank?)
if Date.valid_date?(year.to_i, month.to_i, day.to_i)
@location.startdate = Time.zone.local(year.to_i, month.to_i, day.to_i)
if @location.save(context: :startdate)
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
render :availability, status: :unprocessable_entity
end
def availability; end
def update_availability
day = params[:location]["startdate(3i)"]
month = params[:location]["startdate(2i)"]
year = params[:location]["startdate(1i)"]
if [day, month, year].none?(&:blank?)
if Date.valid_date?(year.to_i, month.to_i, day.to_i)
@location.startdate = Time.zone.local(year.to_i, month.to_i, day.to_i)
if @location.save(context: :startdate)
redirect_to scheme_location_check_answers_path(@scheme, @location, route: params[:route])
else
error_message = I18n.t("validations.location.startdate_invalid")
@location.errors.add :startdate, error_message
render :availability, status: :unprocessable_entity
end
else
error_message = I18n.t("validations.location.startdate_blank")
error_message = I18n.t("validations.location.startdate_invalid")
@location.errors.add :startdate, error_message
render :availability, status: :unprocessable_entity
end
else
error_message = I18n.t("validations.location.startdate_blank")
@location.errors.add :startdate, error_message
render :availability, status: :unprocessable_entity
end
end
def check_answers
if params[:location].present?
@location.confirmed = true
@location.save!
flash[:notice] = "#{@location.postcode} #{@location.startdate < Time.zone.now ? 'has been' : 'will be'} added to this scheme"
redirect_to scheme_locations_path(@scheme)
end
def check_answers; end
def update_check_answers
@location.confirmed = true
@location.save!
flash[:notice] = "#{@location.postcode} #{@location.startdate < Time.zone.now ? 'has been' : 'will be'} added to this scheme"
redirect_to scheme_locations_path(@scheme)
end
def show; end

16
config/routes.rb

@ -64,21 +64,21 @@ Rails.application.routes.draw do
get "reactivate", to: "locations#reactivate"
get "new-reactivation", to: "locations#new_reactivation"
get "postcode", to: "locations#postcode"
patch "postcode", to: "locations#postcode"
patch "postcode", to: "locations#update_postcode"
get "local-authority", to: "locations#local_authority"
patch "local-authority", to: "locations#local_authority"
patch "local-authority", to: "locations#update_local_authority"
get "name", to: "locations#name"
patch "name", to: "locations#name"
patch "name", to: "locations#update_name"
get "units", to: "locations#units"
patch "units", to: "locations#units"
patch "units", to: "locations#update_units"
get "type-of-unit", to: "locations#type_of_unit"
patch "type-of-unit", to: "locations#type_of_unit"
patch "type-of-unit", to: "locations#update_type_of_unit"
get "mobility-standards", to: "locations#mobility_standards"
patch "mobility-standards", to: "locations#mobility_standards"
patch "mobility-standards", to: "locations#update_mobility_standards"
get "availability", to: "locations#availability"
patch "availability", to: "locations#availability"
patch "availability", to: "locations#update_availability"
get "check-answers", to: "locations#check_answers"
patch "check-answers", to: "locations#check_answers"
patch "check-answers", to: "locations#update_check_answers"
patch "new-deactivation", to: "locations#new_deactivation"
patch "deactivate", to: "locations#deactivate"
patch "reactivate", to: "locations#reactivate"

4
spec/features/schemes_helpers.rb

@ -56,7 +56,7 @@ module SchemesHelpers
def fill_in_and_save_location
click_link "Locations"
click_link "Add a location"
click_button "Add a location"
fill_in with: "AA11AA"
click_button "Save and continue"
fill_in with: "Adur"
@ -76,7 +76,7 @@ module SchemesHelpers
def fill_in_and_save_second_location
click_link "Locations"
click_link "Add a location"
click_button "Add a location"
fill_in with: "AA12AA"
click_button "Save and continue"
fill_in with: "Adur"

4
spec/features/schemes_spec.rb

@ -270,7 +270,7 @@ RSpec.describe "Schemes scheme Features" do
context "when the user clicks add location" do
before do
click_link("Locations")
click_link("Add a location")
click_button("Add a location")
end
it "shows the new location form" do
@ -913,7 +913,7 @@ RSpec.describe "Schemes scheme Features" do
context "when the user clicks add location" do
before do
click_link("Locations")
click_link("Add a location")
click_button("Add a location")
end
it "shows the new location form" do

18
spec/requests/locations_controller_spec.rb

@ -10,10 +10,10 @@ RSpec.describe LocationsController, type: :request do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
describe "#new" do
describe "#create" do
context "when not signed in" do
it "redirects to the sign in page" do
get "/schemes/1/locations/new"
post "/schemes/1/locations/create"
expect(response).to redirect_to("/account/sign-in")
end
end
@ -23,7 +23,7 @@ RSpec.describe LocationsController, type: :request do
before do
sign_in user
get "/schemes/1/locations/new"
post "/schemes/1/locations/create"
end
it "returns 401 unauthorized" do
@ -38,11 +38,11 @@ RSpec.describe LocationsController, type: :request do
before do
sign_in user
get "/schemes/#{scheme.id}/locations/new"
post scheme_locations_path(scheme)
end
it "creates a new location for scheme and redirects to correct page" do
expect { get "/schemes/#{scheme.id}/locations/new" }.to change(Location, :count).by(1)
expect { post scheme_locations_path(scheme) }.to change(Location, :count).by(1)
end
it "redirects to the postcode page" do
@ -59,7 +59,7 @@ RSpec.describe LocationsController, type: :request do
let(:another_scheme) { FactoryBot.create(:scheme) }
it "displays the new page with an error message" do
get "/schemes/#{another_scheme.id}/locations/new"
post scheme_locations_path(another_scheme)
expect(response).to have_http_status(:not_found)
end
end
@ -72,11 +72,11 @@ RSpec.describe LocationsController, type: :request do
before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
get "/schemes/#{scheme.id}/locations/new"
post scheme_locations_path(scheme)
end
it "creates a new location for scheme and redirects to correct page" do
expect { get "/schemes/#{scheme.id}/locations/new" }.to change(Location, :count).by(1)
expect { post scheme_locations_path(scheme) }.to change(Location, :count).by(1)
end
it "redirects to the postcode page" do
@ -93,7 +93,7 @@ RSpec.describe LocationsController, type: :request do
let(:another_scheme) { FactoryBot.create(:scheme) }
it "displays the new page with an error message" do
get "/schemes/#{another_scheme.id}/locations/new"
post scheme_locations_path(another_scheme)
expect(response).to have_http_status(:not_found)
end
end

Loading…
Cancel
Save