From b65f718afe8102c29e57cfec06dc145039b45d3a Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 28 Sep 2022 10:19:35 +0100 Subject: [PATCH] wip --- app/controllers/form_controller.rb | 14 +++++++++++--- spec/features/lettings_log_spec.rb | 7 +++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 5d68142af..571fff956 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -1,7 +1,8 @@ class FormController < ApplicationController before_action :authenticate_user! before_action :find_resource, only: %i[submit_form review] - before_action :find_resource_by_named_id, except: %i[submit_form review] + before_action :find_resource_by_named_id, except: %i[submit_form review show_new_page] + before_action :create_new_resource, only: %i[show_new_page] def submit_form if @log @@ -13,7 +14,7 @@ class FormController < ApplicationController session[:errors] = session[:fields] = nil redirect_to(successful_redirect_path) else - redirect_path = "#{@log.model_name.param_key}_#{@page.id}_path" + redirect_path = @log.status == "not_started" ? "#{@log.model_name.param_key}s_new_#{@page.id}_path" : "#{@log.model_name.param_key}_#{@page.id}_path" mandatory_questions_with_no_response.map do |question| @log.errors.add question.id.to_sym, question.unanswered_error_message end @@ -62,7 +63,6 @@ class FormController < ApplicationController 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 @@ -159,6 +159,14 @@ private end end + def create_new_resource + @log = if request.path.include?("sales-logs") + SalesLog.new + else + LettingsLog.new + end + end + def new_log_request? request.path.split("/").include?("new") end diff --git a/spec/features/lettings_log_spec.rb b/spec/features/lettings_log_spec.rb index 1bd72b5cf..ec08e4082 100644 --- a/spec/features/lettings_log_spec.rb +++ b/spec/features/lettings_log_spec.rb @@ -124,6 +124,13 @@ RSpec.describe "Lettings Log Features" do end end + context "when creating a new log" do + it "creates a log after answering at least 1 question" do + visit("/lettings-logs") + expect { click_button("Create a new lettings log") }.to change(LettingsLog, :count).by(0) + end + end + context "when returning to the list of logs via breadcrumbs link" do before do visit("/lettings-logs")