diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 6e71de6c6..3ed641b51 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -60,8 +60,34 @@ class FormController < ApplicationController end end + def show_new_page + page_id = request.path.split("/")[-1].underscore + save_new_log page_id + if @log + restore_error_field_values + page_id = request.path.split("/")[-1].underscore + @page = @log.form.get_page(page_id) + @subsection = @log.form.subsection_for_page(@page) + if @page.routed_to?(@log, current_user) + render "form/page" + else + redirect_to lettings_log_path(@log) + end + else + render_not_found + end + end + private + def save_new_log(page_id) + if current_user.support? + @log.save! unless page_id.eql?("organisation") && @log.id.nil? + else + @log.save! unless page_id.eql?("needs_type") && @log.id.nil? + end + end + def restore_error_field_values if session["errors"] JSON(session["errors"]).each do |field, messages| @@ -105,22 +131,39 @@ private end end + def find_resource - @log = if params.key?("sales_log") - current_user.sales_logs.find_by(id: params[:id]) + @log = if is_new_log_request? || new_log_referrer? + LettingsLog.new(owning_organisation: current_user.support? ? nil : current_user.organisation) else - current_user.lettings_logs.find_by(id: params[:id]) + params.key?("sales_log") ? current_user.sales_logs.find_by(id: params[:id]) : current_user.lettings_logs.find_by(id: params[:id]) end end def find_resource_by_named_id @log = if params[:sales_log_id].present? - current_user.sales_logs.find_by(id: params[:sales_log_id]) + if new_log_request? + SalesLog.new + else + current_user.sales_logs.find_by(id: params[:sales_log_id]) + end else - current_user.lettings_logs.find_by(id: params[:lettings_log_id]) + if new_log_request? + LettingsLog.new + else + current_user.lettings_logs.find_by(id: params[:lettings_log_id]) + end end end + def new_log_request? + request.path.split("/").include?("new") + end + + def new_log_request_referrer? + request.referer.split("/").include?("new") + end + def is_referrer_check_answers? referrer = request.headers["HTTP_REFERER"].presence || "" referrer.present? && CGI.parse(referrer.split("?")[-1]).present? && CGI.parse(referrer.split("?")[-1])["referrer"][0] == "check_answers" diff --git a/app/controllers/lettings_logs_controller.rb b/app/controllers/lettings_logs_controller.rb index 8397092b7..056374079 100644 --- a/app/controllers/lettings_logs_controller.rb +++ b/app/controllers/lettings_logs_controller.rb @@ -88,8 +88,9 @@ class LettingsLogsController < LogsController def csv_confirmation; end private + def new_log_request? - request.path.include?("new") + request.path.include?("new-log") end def permitted_log_params diff --git a/app/controllers/sales_logs_controller.rb b/app/controllers/sales_logs_controller.rb index c8edfcfb5..81a4be9bd 100644 --- a/app/controllers/sales_logs_controller.rb +++ b/app/controllers/sales_logs_controller.rb @@ -51,6 +51,6 @@ class SalesLogsController < LogsController private def new_log_request? - request.path.include?("new") + request.path.include?("new-log") end end diff --git a/app/models/form.rb b/app/models/form.rb index 22321f431..133b84c3d 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -76,7 +76,7 @@ class Form if nxt_page == :check_answers "#{type}_log_#{subsection_for_page(page).id}_check_answers_path" else - "#{type}_log_#{nxt_page}_path" + log.id ? "#{type}_log_#{nxt_page}_path" : "new_#{nxt_page}_#{type}_logs_path" end end diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb index 8d90f12b9..7ef03c2d3 100644 --- a/app/views/form/page.html.erb +++ b/app/views/form/page.html.erb @@ -4,8 +4,10 @@ <%= govuk_back_link(href: "javascript:history.back()") %> <% end %> +<% path_url = @log.id ? form_lettings_log_path(@log) : new_form_lettings_logs_path %> +
-<%= form_with model: @log, url: form_lettings_log_path(@log), method: "post", local: true do |f| %> +<%= form_with model: @log, url: path_url, method: "post", local: true do |f| %>