From 8033d2f1dcbb5db1dad0aa418dbb44df51891722 Mon Sep 17 00:00:00 2001 From: JG Date: Tue, 5 Jul 2022 14:06:06 +0100 Subject: [PATCH] fixed routing - WIP --- app/controllers/locations_controller.rb | 22 +++++---- app/helpers/tab_nav_helper.rb | 2 +- app/views/locations/edit.html.erb | 63 +++++++++++++++++++++++++ config/routes.rb | 5 -- 4 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 app/views/locations/edit.html.erb diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 3cf231246..496007740 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -6,12 +6,16 @@ class LocationsController < ApplicationController before_action :find_scheme before_action :authenticate_action! + def index + @pagy, @locations = pagy(@scheme.locations) + @total_count = @scheme.locations.size + end + def new @location = Location.new end def create - debugger @location = Location.new(location_params) if @location.save @@ -21,7 +25,7 @@ class LocationsController < ApplicationController end end - def details; end + def edit; end def update if @location.update(location_params) @@ -31,16 +35,14 @@ class LocationsController < ApplicationController end end - def index - @scheme = Scheme.find_by(id: params[:id]) - @pagy, @locations = pagy(@scheme.locations) - @total_count = @scheme.locations.size - end - private def find_scheme - @scheme = params[:scheme_id] && params[:id] ? Scheme.find(params[:scheme_id]) : Scheme.find(params[:id]) + if %w[new create].include?(action_name) + @scheme = Scheme.find(params[:id]) + else + @scheme = @location.scheme + end end def find_location @@ -52,7 +54,7 @@ private end def authenticate_action! - if %w[new create details update].include?(action_name) && !((current_user.organisation == @scheme.organisation) || current_user.support?) + if %w[details edit update].include?(action_name) && !((current_user.organisation == @scheme.organisation) || current_user.support?) render_not_found and return end end diff --git a/app/helpers/tab_nav_helper.rb b/app/helpers/tab_nav_helper.rb index ef8b240bc..4df951357 100644 --- a/app/helpers/tab_nav_helper.rb +++ b/app/helpers/tab_nav_helper.rb @@ -8,7 +8,7 @@ module TabNavHelper def location_cell(location) link_text = location.postcode - [govuk_link_to(link_text, location_path(location)), "Location #{location.name}"].join("\n") + [govuk_link_to(link_text, edit_location_path(location)), "Location #{location.name}"].join("\n") end def scheme_cell(scheme) diff --git a/app/views/locations/edit.html.erb b/app/views/locations/edit.html.erb new file mode 100644 index 000000000..bbcccdef6 --- /dev/null +++ b/app/views/locations/edit.html.erb @@ -0,0 +1,63 @@ +<% content_for :title, "Add a location to this scheme" %> + +<% content_for :before_content do %> + <%= govuk_back_link( + text: "Back", + href: "/schemes/#{@scheme.id}/support", + ) %> +<% end %> + +<%= render partial: "organisations/headings", locals: { main: "Add a location to this scheme", sub: @scheme.service_name } %> + +<%= form_for(@location, method: :patch, url: location_path) do |f| %> +
+
+ <%= f.govuk_error_summary %> + + <%= f.govuk_text_field :postcode, + label: { size: "m" }, + hint: { text: "For example, SW1P 4DF." }, + width: 5 %> + + <%= f.govuk_text_field :name, + label: { text: "Name (optional)", size: "m" }, + hint: { text: "This is how you refer to this location within your organisation" } %> + + <%= f.govuk_number_field :total_units, + label: { text: "Total number of units at this location", size: "m" }, + width: 2, + hint: { text: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff.s" }, + autofocus: true %> + + <% type_of_units_selection = Location.type_of_units.keys.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + + <%= f.govuk_collection_radio_buttons :type_of_unit, + type_of_units_selection, + :id, + :name, + legend: { text: "What is this type of scheme?", size: "m" } %> + + <% wheelchair_user_selection = Location.wheelchair_adaptations.keys.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + + <%= f.govuk_collection_radio_buttons :wheelchair_adaptation, + wheelchair_user_selection, + :id, + :name, + hint: { text: "This includes stairlifts, ramps, level-access showers or grab rails" }, + legend: { text: "Are the majority of units in this location built or adapted to wheelchair-user standards?", size: "m" } %> + + <%= govuk_section_break(visible: true, size: "m") %> + + <% another_location_selection = %w[Yes no].map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> + + <%= f.govuk_collection_radio_buttons :add_another_location, + another_location_selection, + :id, + :name, + inline: true, + legend: { text: "Do you want to add another location?", size: "m" } %> + + <%= f.govuk_submit "Save and continue" %> +
+
+<% end %> diff --git a/config/routes.rb b/config/routes.rb index d6e8b6a3a..a643f2ab4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -45,11 +45,6 @@ Rails.application.routes.draw do member do resources :locations - # get "locations", to: "schemes#locations" - # get "location/new", to: "locations#new" - # post "location/create", to: "locations#create" - # get "location", to: "locations#details" - # patch "location", to: "locations#update" end end