From 00aac08f2c716cba49811bd194fba690855b91e8 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 24 Jun 2022 16:54:26 +0100 Subject: [PATCH] WIP change update paths --- app/controllers/schemes_controller.rb | 96 +++++-------------- app/views/schemes/check_answers.html.erb | 4 +- app/views/schemes/confirm_secondary.html.erb | 4 +- app/views/schemes/new.html.erb | 5 +- .../schemes/primary_client_group.html.erb | 5 +- .../schemes/secondary_client_group.html.erb | 4 +- app/views/schemes/support.html.erb | 4 +- config/routes.rb | 8 +- 8 files changed, 43 insertions(+), 87 deletions(-) diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index a9dcca628..36bf409c6 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -4,7 +4,6 @@ class SchemesController < ApplicationController before_action :authenticate_user! before_action :find_resource, except: %i[index] - before_action :find_by_scheme_id, only: %i[edit] before_action :authenticate_scope! def index @@ -31,108 +30,63 @@ class SchemesController < ApplicationController end def create - @scheme = Scheme.create!(clean_params) - @path = scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id) - + @scheme = Scheme.create!(scheme_params) render "schemes/primary_client_group" end - def primary_client_group - @scheme = Scheme.find_by(id: params[:scheme_id]) - - @path = if params[:check_answers] - scheme_check_your_answers_path(scheme_id: @scheme.id) - else - scheme_confirm_secondary_client_group_path(scheme_id: @scheme.id) - end - - if params[:scheme] - required_params = params.require(:scheme).permit(:intended_stay, :support_type, :service_name, :sensitive, :organisation_id, :scheme_type, :registered_under_care_act, :total_units, :id, :confirmed, :secondary_client_group, :primary_client_group) - required_params[:sensitive] = required_params[:sensitive].to_i if required_params[:sensitive] - @scheme.update!(required_params) + def update + if @scheme.update(scheme_params) + schemes_path = case params[:scheme][:page] + when "primary-client-group" + scheme_confirm_secondary_client_group_path(@scheme) + when "confirm-secondary" + @scheme.has_other_client_group == "Yes" ? scheme_secondary_client_group_path(@scheme) : scheme_support_path(@scheme) + when "secondary-client-group" + scheme_support_path(@scheme) + when "support" + scheme_check_answers_path(@scheme) + end + + redirect_to schemes_path + else + render request.current_url, status: :unprocessable_entity end + end + + def edit; end + def primary_client_group render "schemes/primary_client_group" end def confirm_secondary_client_group - - @scheme = Scheme.find_by(id: params[:scheme_id]) - @path = params[:check_answers] ? scheme_secondary_client_group_path(scheme_id: @scheme.id, check_answers: true) : scheme_secondary_client_group_path(scheme_id: @scheme.id) - if params[:scheme] - required_params = params.require(:scheme).permit(:primary_client_group) if params - @scheme.update!(required_params) if required_params - end - render "schemes/confirm_secondary" end def secondary_client_group - @scheme = Scheme.find_by(id: params[:scheme_id]) - @path = params[:check_answers] ? scheme_check_your_answers_path(scheme_id: @scheme.id) : scheme_support_path(scheme_id: @scheme.id) - if params[:scheme] - required_params = params.require(:scheme).permit(:has_other_client_group) if params - @scheme.update!(required_params) if required_params - if @scheme.has_other_client_group == "Yes" - render("schemes/secondary_client_group") - elsif params[:check_answers] - redirect_to(scheme_check_your_answers_path(sheme_id: @scheme.id)) - else - redirect_to(scheme_support_path(scheme_id: @scheme.id)) - end - else - render "schemes/secondary_client_group" - end + render "schemes/secondary_client_group" end def support - @scheme = Scheme.find_by(id: params[:scheme_id]) - if params[:scheme] - required_params = params.require(:scheme).permit(:secondary_client_group) - @scheme.update!(required_params) if required_params - end - render "schemes/support" end def check_answers - @scheme = Scheme.find_by(id: params[:scheme_id]) - if params[:scheme] - required_params = params.require(:scheme).permit(:intended_stay, :support_type, :service_name, :sensitive, :organisation_id, :scheme_type, :registered_under_care_act, :total_units, :id, :confirmed, :secondary_client_group, :primary_client_group) - required_params[:sensitive] = required_params[:sensitive].to_i if required_params[:sensitive] - @scheme.update!(required_params) - end - render "schemes/check_answers" end - def update - @scheme = Scheme.find_by(id: params[:scheme_id]) - flash[:notice] = "#{@scheme.service_name} has been created." - - redirect_to schemes_path - end - - def edit; end - private - def clean_params - required_params = params.require(:scheme).permit(:service_name, :sensitive, :organisation_id, :scheme_type, :registered_under_care_act, :total_units, :id, :confirmed) - required_params[:sensitive] = required_params[:sensitive].to_i - required_params + def scheme_params + params.require(:scheme).permit(:service_name, :sensitive, :organisation_id, :scheme_type, :registered_under_care_act, :total_units, :id, :confirmed, :has_other_client_group) end def search_term params["search"] end - def find_by_scheme_id - @scheme = Scheme.find_by(id: params[:scheme_id]) - end - def find_resource - @scheme = Scheme.find_by(id: params[:id]) + @scheme = Scheme.find_by(id: params[:id]) || Scheme.find_by(id: params[:scheme_id]) end def authenticate_scope! diff --git a/app/views/schemes/check_answers.html.erb b/app/views/schemes/check_answers.html.erb index 20f29d0b9..99e1a8637 100644 --- a/app/views/schemes/check_answers.html.erb +++ b/app/views/schemes/check_answers.html.erb @@ -19,7 +19,7 @@ <% row.value { details_html(attr) } %> <% row.action( text: "Change", - href: scheme_details_path(scheme_id: @scheme.id, check_answers: true), + href: schemes_path(scheme_id: @scheme.id, check_answers: true), ) %> <% end %> <% end %> @@ -67,4 +67,4 @@ <% end %> <% end %> -<%= govuk_button_link_to "Create scheme", scheme_update_path, html: { method: :get } %> +<%= govuk_button_link_to "Create scheme", schemes_path, html: { method: :get } %> diff --git a/app/views/schemes/confirm_secondary.html.erb b/app/views/schemes/confirm_secondary.html.erb index 38bdc0eb8..8f2094c4a 100644 --- a/app/views/schemes/confirm_secondary.html.erb +++ b/app/views/schemes/confirm_secondary.html.erb @@ -9,7 +9,7 @@ <%= render partial: "organisations/headings", locals: { main: "Does this scheme provide for another client group?", sub: @scheme.service_name } %> -<%= form_for(@scheme, method: :patch, url: @path) do |f| %> +<%= form_for(@scheme, method: :patch) do |f| %>
@@ -22,6 +22,8 @@ legend: nil %> + <%= f.hidden_field :page, value: "confirm-secondary" %> + <%= f.govuk_submit "Save and continue" %>
diff --git a/app/views/schemes/new.html.erb b/app/views/schemes/new.html.erb index e94b1cdfb..45ab011ab 100644 --- a/app/views/schemes/new.html.erb +++ b/app/views/schemes/new.html.erb @@ -7,7 +7,7 @@ ) %> <% end %> -<%= form_for(@scheme, as: :scheme, method: :post, url: schemes_path(scheme: @scheme)) do |f| %> +<%= form_for(@scheme, as: :scheme, method: :post) do |f| %>
<%= f.govuk_error_summary %> @@ -22,8 +22,7 @@ <%= f.govuk_check_box :sensitive, - 1, - 0, + "Yes", checked: @scheme.sensitive?, multiple: false, label: { text: "This scheme contains confidential information" } %> diff --git a/app/views/schemes/primary_client_group.html.erb b/app/views/schemes/primary_client_group.html.erb index 98080d9b5..4ad176bd4 100644 --- a/app/views/schemes/primary_client_group.html.erb +++ b/app/views/schemes/primary_client_group.html.erb @@ -9,7 +9,8 @@ <%= render partial: "organisations/headings", locals: { main: "What client group is this scheme intended for?", sub: @scheme.service_name } %> -<%= form_for(@scheme, method: :patch, url: @path) do |f| %> + +<%= form_for(@scheme, method: :patch) do |f| %>
<%= f.govuk_error_summary %> @@ -24,6 +25,8 @@ :name, legend: nil %> + <%= f.hidden_field :page, value: "primary-client-group" %> + <%= f.govuk_submit "Save and continue" %>
diff --git a/app/views/schemes/secondary_client_group.html.erb b/app/views/schemes/secondary_client_group.html.erb index 079522edd..7068973d4 100644 --- a/app/views/schemes/secondary_client_group.html.erb +++ b/app/views/schemes/secondary_client_group.html.erb @@ -9,7 +9,7 @@ <%= render partial: "organisations/headings", locals: { main: "What is the other client group?", sub: @scheme.service_name } %> -<%= form_for(@scheme, method: :patch, url: @path) do |f| %> +<%= form_for(@scheme, method: :patch) do |f| %>
<%= f.govuk_error_summary %> @@ -24,6 +24,8 @@ :name, legend: nil %> + <%= f.hidden_field :page, value: "secondary-client-group" %> + <%= f.govuk_submit "Save and continue" %>
diff --git a/app/views/schemes/support.html.erb b/app/views/schemes/support.html.erb index 24900a5fc..a3f692c47 100644 --- a/app/views/schemes/support.html.erb +++ b/app/views/schemes/support.html.erb @@ -9,7 +9,7 @@ <%= render partial: "organisations/headings", locals: { main: "What support does this scheme provide?", sub: @scheme.service_name } %> -<%= form_for(@scheme, method: :patch, url: scheme_check_your_answers_path(scheme_id: @scheme.id)) do |f| %> +<%= form_for(@scheme, method: :patch) do |f| %>
<%= f.govuk_error_summary %> @@ -31,6 +31,8 @@ :name, legend: { text: "Intended length of stay", size: "m" } %> + <%= f.hidden_field :page, value: "support" %> + <%= f.govuk_submit "Save and continue" %>
diff --git a/config/routes.rb b/config/routes.rb index b9be442b3..0af30910b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,17 +36,11 @@ Rails.application.routes.draw do end resources :schemes do - patch "primary-client-group", to: "schemes#primary_client_group" get "primary-client-group", to: "schemes#primary_client_group" - patch "confirm-secondary-client-group", to: "schemes#confirm_secondary_client_group" get "confirm-secondary-client-group", to: "schemes#confirm_secondary_client_group" - patch "secondary-client-group", to: "schemes#secondary_client_group" get "secondary-client-group", to: "schemes#secondary_client_group" - patch "support", to: "schemes#support" get "support", to: "schemes#support" - patch "check-your-answers", to: "schemes#check_answers" - get "check-your-answers", to: "schemes#check_answers" - get "update", to: "schemes#update" + get "check-answers", to: "schemes#check_answers" member do get "locations", to: "schemes#locations"