Browse Source

Not save not started logs for non support users

CLDC-1324
Kat 2 years ago
parent
commit
20708325e0
  1. 8
      app/controllers/form_controller.rb
  2. 4
      app/controllers/lettings_logs_controller.rb
  3. 4
      app/controllers/logs_controller.rb
  4. 4
      app/controllers/sales_logs_controller.rb
  5. 16
      app/helpers/check_answers_helper.rb
  6. 10
      app/models/form.rb
  7. 6
      app/models/form/question.rb
  8. 6
      app/views/form/check_answers.html.erb
  9. 2
      app/views/logs/edit.html.erb
  10. 12
      config/routes.rb
  11. 3
      spec/factories/lettings_log.rb
  12. 1
      spec/features/form/form_navigation_spec.rb
  13. 1
      spec/features/form/validations_spec.rb
  14. 2
      spec/features/lettings_log_spec.rb
  15. 1
      spec/requests/form_controller_spec.rb
  16. 2
      spec/requests/lettings_logs_controller_spec.rb

8
app/controllers/form_controller.rb

@ -10,7 +10,9 @@ class FormController < ApplicationController
responses_for_page = responses_for_page(@page)
mandatory_questions_with_no_response = mandatory_questions_with_no_response(responses_for_page)
if mandatory_questions_with_no_response.empty? && @log.update(responses_for_page)
if @log.not_started? && responses_for_page.values.all?(&:blank?) && mandatory_questions_with_no_response.empty?
redirect_to(successful_redirect_path)
elsif mandatory_questions_with_no_response.empty? && @log.update(responses_for_page)
session[:errors] = session[:fields] = nil
redirect_to(successful_redirect_path)
else
@ -124,7 +126,7 @@ private
@log = if params[:sales_log_id].present?
current_user.sales_logs.find_by(id: params[:sales_log_id])
elsif new_log_request?
create_new_resource
create_new_resource
else
current_user.lettings_logs.find_by(id: params[:lettings_log_id])
end
@ -143,7 +145,7 @@ private
end
def new_log_request_referrer?
request.referer&.split("/").include?("new")
request.referer&.split("/")&.include?("new")
end
def is_referrer_check_answers?

4
app/controllers/lettings_logs_controller.rb

@ -97,7 +97,7 @@ private
@log = LettingsLog.find_by(id: params[:id])
end
def post_create_redirect_url
new_log_lettings_logs_path
def post_create_redirect_url(log)
log.not_started? ? new_log_lettings_logs_path : lettings_log_url(log)
end
end

4
app/controllers/logs_controller.rb

@ -11,11 +11,13 @@ private
def create
log = yield
log.save! unless api_log_params.empty?
raise "Caller must pass a block that implements model creation" if log.blank?
respond_to do |format|
format.html do
redirect_to(post_create_redirect_url)
redirect_to(post_create_redirect_url(log))
end
format.json do
if log.save

4
app/controllers/sales_logs_controller.rb

@ -40,8 +40,8 @@ class SalesLogsController < LogsController
end
end
def post_create_redirect_url
new_log_sales_logs_path
def post_create_redirect_url(log)
log.not_started? ? new_log_sales_logs_path : sales_log_url(log)
end
def permitted_log_params

16
app/helpers/check_answers_helper.rb

@ -32,6 +32,22 @@ module CheckAnswersHelper
"#{log.class.name.underscore}_#{redirect_path.underscore.tr('/', '_')}_path"
end
def log_breadcrumbs(log, subsection)
if log.not_started?
{
"Logs" => "/logs",
"New log" => send("new_log_#{@log.class.name.underscore}s_path"),
subsection.label => "",
}
else
{
"Logs" => "/logs",
"Log #{@log.id}" => send("#{@log.class.name.underscore}_path", @log),
subsection.label => "",
}
end
end
private
def answered_questions_count(subsection, lettings_log, current_user)

10
app/models/form.rb

@ -74,14 +74,18 @@ class Form
def next_page_redirect_path(page, log, current_user)
nxt_page = next_page(page, log, current_user)
if nxt_page == :check_answers
"#{type}_log_#{subsection_for_page(page).id}_check_answers_path"
log.not_started? ? "#{type}_log_new_#{subsection_for_page(page).id}_check_answers_path" : "#{type}_log_#{subsection_for_page(page).id}_check_answers_path"
else
log.id ? "#{type}_log_#{nxt_page}_path" : "#{type}_log_new_#{nxt_page}_path"
log.not_started? ? "#{type}_log_new_#{nxt_page}_path" : "#{type}_log_#{nxt_page}_path"
end
end
def cancel_path(page, log)
"#{log.class.name.underscore}_#{page.subsection.id}_check_answers_path"
if log.not_started?
"#{log.class.name.underscore}_new_#{page.subsection.id}_check_answers_path"
else
"#{log.class.name.underscore}_#{page.subsection.id}_check_answers_path"
end
end
def next_incomplete_section_redirect_path(subsection, log)

6
app/models/form/question.rb

@ -122,7 +122,11 @@ class Form::Question
end
def action_href(log, page_id)
"/#{log.model_name.param_key.dasherize}s/#{log.id}/#{page_id.to_s.dasherize}?referrer=check_answers"
if log.not_started?
"/#{log.model_name.param_key.dasherize}/new/#{page_id.to_s.dasherize}?referrer=check_answers"
else
"/#{log.model_name.param_key.dasherize}s/#{log.id}/#{page_id.to_s.dasherize}?referrer=check_answers"
end
end
def completed?(log)

6
app/views/form/check_answers.html.erb

@ -1,9 +1,5 @@
<% content_for :title, "#{subsection.id.humanize} - Check your answers" %>
<% content_for :breadcrumbs, govuk_breadcrumbs(breadcrumbs: {
"Logs" => "/logs",
"Log #{@log.id}" => send("#{@log.class.name.underscore}_path", @log),
subsection.label => "",
}) %>
<% content_for :breadcrumbs, govuk_breadcrumbs(breadcrumbs: log_breadcrumbs(@log, subsection)) %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters-from-desktop">

2
app/views/logs/edit.html.erb

@ -1,4 +1,4 @@
<% content_for :title, "Log #{@log.id ? @log.id : "new"}" %>
<% content_for :title, @log.id ? "Log #{@log.id}" : "New log" %>
<% content_for :breadcrumbs, govuk_breadcrumbs(breadcrumbs: {
"Logs" => @log.lettings? ? lettings_logs_path : sales_logs_path,
content_for(:title) => "",

12
config/routes.rb

@ -36,6 +36,18 @@ Rails.application.routes.draw do
end
end
FormHandler.instance.forms.each do |_key, form|
form.pages.map do |page|
get "/lettings-log/new/#{page.id.to_s.dasherize}", to: "form#show_page"
get "/sales-log/new/#{page.id.to_s.dasherize}", to: "form#show_page"
end
form.subsections.map do |subsection|
get "/lettings-log/new/#{subsection.id.to_s.dasherize}/check-answers", to: "form#check_answers"
get "/sales-log/new/#{subsection.id.to_s.dasherize}/check-answers", to: "form#check_answers"
end
end
get "/accessibility-statement", to: "content#accessibility_statement"
get "/privacy-notice", to: "content#privacy_notice"
get "/data-sharing-agreement", to: "content#data_sharing_agreement"

3
spec/factories/lettings_log.rb

@ -9,6 +9,9 @@ FactoryBot.define do
rent_type { 1 }
startdate { Time.zone.local(2022, 5, 1) }
end
trait :just_started do
rent_type { 1 }
end
trait :in_progress do
status { 1 }
tenancycode { Faker::Name.initials(number: 10) }

1
spec/features/form/form_navigation_spec.rb

@ -16,6 +16,7 @@ RSpec.describe "Form Navigation" do
let(:empty_lettings_log) do
FactoryBot.create(
:lettings_log,
:just_started,
owning_organisation: user.organisation,
managing_organisation: user.organisation,
created_by: user,

1
spec/features/form/validations_spec.rb

@ -16,6 +16,7 @@ RSpec.describe "validations" do
let(:empty_lettings_log) do
FactoryBot.create(
:lettings_log,
:just_started,
owning_organisation: user.organisation,
managing_organisation: user.organisation,
)

2
spec/features/lettings_log_spec.rb

@ -128,7 +128,7 @@ RSpec.describe "Lettings Log Features" 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
end
context "when returning to the list of logs via breadcrumbs link" do

1
spec/requests/form_controller_spec.rb

@ -218,6 +218,7 @@ RSpec.describe FormController, type: :request do
let(:lettings_log) do
FactoryBot.create(
:lettings_log,
:just_started,
owning_organisation: organisation,
managing_organisation: organisation,
)

2
spec/requests/lettings_logs_controller_spec.rb

@ -134,7 +134,7 @@ RSpec.describe LettingsLogsController, type: :request do
before do
RequestHelper.stub_http_requests
sign_in user
post "/lettings-logs", headers:
post "/lettings-logs", headers:, params: { lettings_log: { tenancycode: "1234" } }
end
it "tracks who created the record" do

Loading…
Cancel
Save