Browse Source

Not save not started logs for non support users

CLDC-1324
Kat 2 years ago
parent
commit
20708325e0
  1. 6
      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. 8
      app/models/form.rb
  7. 4
      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. 1
      spec/requests/form_controller_spec.rb
  15. 2
      spec/requests/lettings_logs_controller_spec.rb

6
app/controllers/form_controller.rb

@ -10,7 +10,9 @@ class FormController < ApplicationController
responses_for_page = responses_for_page(@page) responses_for_page = responses_for_page(@page)
mandatory_questions_with_no_response = mandatory_questions_with_no_response(responses_for_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 session[:errors] = session[:fields] = nil
redirect_to(successful_redirect_path) redirect_to(successful_redirect_path)
else else
@ -143,7 +145,7 @@ private
end end
def new_log_request_referrer? def new_log_request_referrer?
request.referer&.split("/").include?("new") request.referer&.split("/")&.include?("new")
end end
def is_referrer_check_answers? def is_referrer_check_answers?

4
app/controllers/lettings_logs_controller.rb

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

4
app/controllers/logs_controller.rb

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

4
app/controllers/sales_logs_controller.rb

@ -40,8 +40,8 @@ class SalesLogsController < LogsController
end end
end end
def post_create_redirect_url def post_create_redirect_url(log)
new_log_sales_logs_path log.not_started? ? new_log_sales_logs_path : sales_log_url(log)
end end
def permitted_log_params 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" "#{log.class.name.underscore}_#{redirect_path.underscore.tr('/', '_')}_path"
end 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 private
def answered_questions_count(subsection, lettings_log, current_user) def answered_questions_count(subsection, lettings_log, current_user)

8
app/models/form.rb

@ -74,15 +74,19 @@ class Form
def next_page_redirect_path(page, log, current_user) def next_page_redirect_path(page, log, current_user)
nxt_page = next_page(page, log, current_user) nxt_page = next_page(page, log, current_user)
if nxt_page == :check_answers 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 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
end end
def cancel_path(page, log) def cancel_path(page, log)
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" "#{log.class.name.underscore}_#{page.subsection.id}_check_answers_path"
end end
end
def next_incomplete_section_redirect_path(subsection, log) def next_incomplete_section_redirect_path(subsection, log)
subsection_ids = subsections.map(&:id) subsection_ids = subsections.map(&:id)

4
app/models/form/question.rb

@ -122,8 +122,12 @@ class Form::Question
end end
def action_href(log, page_id) def action_href(log, page_id)
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" "/#{log.model_name.param_key.dasherize}s/#{log.id}/#{page_id.to_s.dasherize}?referrer=check_answers"
end end
end
def completed?(log) def completed?(log)
return answer_options.keys.any? { |key| value_is_yes?(log[key]) } if type == "checkbox" return answer_options.keys.any? { |key| value_is_yes?(log[key]) } if type == "checkbox"

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

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

12
config/routes.rb

@ -36,6 +36,18 @@ Rails.application.routes.draw do
end end
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 "/accessibility-statement", to: "content#accessibility_statement"
get "/privacy-notice", to: "content#privacy_notice" get "/privacy-notice", to: "content#privacy_notice"
get "/data-sharing-agreement", to: "content#data_sharing_agreement" 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 } rent_type { 1 }
startdate { Time.zone.local(2022, 5, 1) } startdate { Time.zone.local(2022, 5, 1) }
end end
trait :just_started do
rent_type { 1 }
end
trait :in_progress do trait :in_progress do
status { 1 } status { 1 }
tenancycode { Faker::Name.initials(number: 10) } 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 let(:empty_lettings_log) do
FactoryBot.create( FactoryBot.create(
:lettings_log, :lettings_log,
:just_started,
owning_organisation: user.organisation, owning_organisation: user.organisation,
managing_organisation: user.organisation, managing_organisation: user.organisation,
created_by: user, created_by: user,

1
spec/features/form/validations_spec.rb

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

1
spec/requests/form_controller_spec.rb

@ -218,6 +218,7 @@ RSpec.describe FormController, type: :request do
let(:lettings_log) do let(:lettings_log) do
FactoryBot.create( FactoryBot.create(
:lettings_log, :lettings_log,
:just_started,
owning_organisation: organisation, owning_organisation: organisation,
managing_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 before do
RequestHelper.stub_http_requests RequestHelper.stub_http_requests
sign_in user sign_in user
post "/lettings-logs", headers: post "/lettings-logs", headers:, params: { lettings_log: { tenancycode: "1234" } }
end end
it "tracks who created the record" do it "tracks who created the record" do

Loading…
Cancel
Save