Browse Source

route and button to new logs

CLDC-1324-remove-not-started-status
JG 2 years ago
parent
commit
f02fcd2648
  1. 43
      app/controllers/form_controller.rb
  2. 22
      app/controllers/lettings_logs_controller.rb
  3. 4
      app/helpers/tasklist_helper.rb
  4. 2
      app/models/form.rb
  5. 3
      app/models/form/subsection.rb
  6. 4
      app/models/lettings_log.rb
  7. 4
      app/views/form/page.html.erb
  8. 2
      app/views/lettings_logs/edit.html.erb
  9. 2
      app/views/lettings_logs/index.html.erb
  10. 8
      config/routes.rb

43
app/controllers/form_controller.rb

@ -60,11 +60,34 @@ class FormController < ApplicationController
render_not_found render_not_found
end end
end end
define_method("new_log_#{page.id}") do |_errors = {}|
if @lettings_log
save_new_log page
restore_error_field_values
@subsection = @lettings_log.form.subsection_for_page(page)
@page = @lettings_log.form.get_page(page.id)
if @page.routed_to?(@lettings_log, current_user)
render "form/page"
else
redirect_to lettings_log_path(@lettings_log)
end
else
render_not_found
end
end
end end
end end
private private
def save_new_log(page)
if current_user.support?
@lettings_log.save! unless page.id.eql?("organisation") && @lettings_log.id.nil?
else
@lettings_log.save! unless page.id.eql?("needs_type") && @lettings_log.id.nil?
end
end
def restore_error_field_values def restore_error_field_values
if session["errors"] if session["errors"]
JSON(session["errors"]).each do |field, messages| JSON(session["errors"]).each do |field, messages|
@ -109,11 +132,27 @@ private
end end
def find_resource def find_resource
@lettings_log = current_user.lettings_logs.find_by(id: params[:id]) @lettings_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])
end
end end
def find_resource_by_named_id def find_resource_by_named_id
@lettings_log = current_user.lettings_logs.find_by(id: params[:lettings_log_id]) @lettings_log = if is_new_log_request?
LettingsLog.new
else
current_user.lettings_logs.find_by(id: params[:lettings_log_id])
end
end
def new_log_request?
request.path.split("/").include?("new")
end
def new_log_request_referrer?
request.referer.split("/").include?("new")
end end
def is_referrer_check_answers? def is_referrer_check_answers?

22
app/controllers/lettings_logs_controller.rb

@ -28,18 +28,9 @@ class LettingsLogsController < ApplicationController
end end
def create def create
lettings_log = LettingsLog.new(lettings_log_params)
respond_to do |format| respond_to do |format|
format.html do format.html do
lettings_log.save! redirect_to new_log_lettings_logs_path
redirect_to lettings_log_url(lettings_log)
end
format.json do
if lettings_log.save
render json: lettings_log, status: :created
else
render json: { errors: lettings_log.errors.messages }, status: :unprocessable_entity
end
end end
end end
end end
@ -71,7 +62,12 @@ class LettingsLogsController < ApplicationController
end end
def edit def edit
@lettings_log = current_user.lettings_logs.find_by(id: params[:id]) @lettings_log = if new_log_request?
LettingsLog.new(lettings_log_params)
else
current_user.lettings_logs.find_by(id: params[:id])
end
if @lettings_log if @lettings_log
render :edit, locals: { current_user: } render :edit, locals: { current_user: }
else else
@ -95,6 +91,10 @@ private
API_ACTIONS = %w[create show update destroy].freeze API_ACTIONS = %w[create show update destroy].freeze
def new_log_request?
request.path.split("/").include?("new")
end
def search_term def search_term
params["search"] params["search"]
end end

4
app/helpers/tasklist_helper.rb

@ -17,7 +17,11 @@ module TasklistHelper
else else
"lettings_log_#{next_question_page(subsection, lettings_log, current_user)}_path" "lettings_log_#{next_question_page(subsection, lettings_log, current_user)}_path"
end end
if lettings_log.id
send(path, lettings_log) send(path, lettings_log)
else
"/logs/new/#{next_question_page(subsection, lettings_log, current_user)}"
end
end end
def next_question_page(subsection, lettings_log, current_user) def next_question_page(subsection, lettings_log, current_user)

2
app/models/form.rb

@ -60,7 +60,7 @@ class Form
if nxt_page == :check_answers if nxt_page == :check_answers
"lettings_log_#{subsection_for_page(page).id}_check_answers_path" "lettings_log_#{subsection_for_page(page).id}_check_answers_path"
else else
"lettings_log_#{nxt_page}_path" lettings_log.id ? "lettings_log_#{nxt_page}_path" : "new_#{nxt_page}_lettings_logs_path"
end end
end end

3
app/models/form/subsection.rb

@ -34,7 +34,8 @@ class Form::Subsection
qs = applicable_questions(lettings_log) qs = applicable_questions(lettings_log)
qs_optional_removed = qs.reject { |q| lettings_log.optional_fields.include?(q.id) } qs_optional_removed = qs.reject { |q| lettings_log.optional_fields.include?(q.id) }
return :not_started if qs.count.positive? && qs.all? { |question| lettings_log[question.id].blank? || question.read_only? || question.derived? } return :not_started if lettings_log.id.nil?
return :in_progress if qs.count.positive? && qs.all? { |question| lettings_log[question.id].blank? || question.read_only? || question.derived? }
return :completed if qs_optional_removed.all? { |question| question.completed?(lettings_log) } return :completed if qs_optional_removed.all? { |question| question.completed?(lettings_log) }
:in_progress :in_progress

4
app/models/lettings_log.rb

@ -70,7 +70,7 @@ class LettingsLog < ApplicationRecord
OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenancycode propcode].freeze OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenancycode propcode].freeze
RENT_TYPE_MAPPING_LABELS = { 1 => "Social Rent", 2 => "Affordable Rent", 3 => "Intermediate Rent" }.freeze RENT_TYPE_MAPPING_LABELS = { 1 => "Social Rent", 2 => "Affordable Rent", 3 => "Intermediate Rent" }.freeze
HAS_BENEFITS_OPTIONS = [1, 6, 8, 7].freeze HAS_BENEFITS_OPTIONS = [1, 6, 8, 7].freeze
STATUS = { "not_started" => 0, "in_progress" => 1, "completed" => 2 }.freeze STATUS = { "in_progress" => 1, "completed" => 2 }.freeze
NUM_OF_WEEKS_FROM_PERIOD = { 2 => 26, 3 => 13, 4 => 12, 5 => 50, 6 => 49, 7 => 48, 8 => 47, 9 => 46, 1 => 52 }.freeze NUM_OF_WEEKS_FROM_PERIOD = { 2 => 26, 3 => 13, 4 => 12, 5 => 50, 6 => 49, 7 => 48, 8 => 47, 9 => 46, 1 => 52 }.freeze
SUFFIX_FROM_PERIOD = { 2 => "every 2 weeks", 3 => "every 4 weeks", 4 => "every month" }.freeze SUFFIX_FROM_PERIOD = { 2 => "every 2 weeks", 3 => "every 4 weeks", 4 => "every month" }.freeze
RETIREMENT_AGES = { "M" => 67, "F" => 60, "X" => 67 }.freeze RETIREMENT_AGES = { "M" => 67, "F" => 60, "X" => 67 }.freeze
@ -534,8 +534,6 @@ private
def update_status! def update_status!
self.status = if all_fields_completed? && errors.empty? self.status = if all_fields_completed? && errors.empty?
"completed" "completed"
elsif all_fields_nil?
"not_started"
else else
"in_progress" "in_progress"
end end

4
app/views/form/page.html.erb

@ -4,8 +4,10 @@
<%= govuk_back_link(href: "javascript:history.back()") %> <%= govuk_back_link(href: "javascript:history.back()") %>
<% end %> <% end %>
<% path_url = @lettings_log.id ? form_lettings_log_path(@lettings_log) : new_form_lettings_logs_path %>
<div data-controller="govukfrontend"></div> <div data-controller="govukfrontend"></div>
<%= form_with model: @lettings_log, url: form_lettings_log_path(@lettings_log), method: "post", local: true do |f| %> <%= form_with model: @lettings_log, url: path_url, method: "post", local: true do |f| %>
<div class="govuk-grid-row"> <div class="govuk-grid-row">
<div class="govuk-grid-column-<%= @page.questions[0].type == "interruption_screen" ? "full-from-desktop" : "two-thirds-from-desktop" %>"> <div class="govuk-grid-column-<%= @page.questions[0].type == "interruption_screen" ? "full-from-desktop" : "two-thirds-from-desktop" %>">
<% remove_other_page_errors(@lettings_log, @page) %> <% remove_other_page_errors(@lettings_log, @page) %>

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

@ -1,4 +1,4 @@
<% content_for :title, "Log #{@lettings_log.id}" %> <% content_for :title, "Log #{@lettings_log.id ? @lettings_log.id : "new"}" %>
<% content_for :breadcrumbs, govuk_breadcrumbs(breadcrumbs: { <% content_for :breadcrumbs, govuk_breadcrumbs(breadcrumbs: {
"Logs" => "/logs", "Logs" => "/logs",
content_for(:title) => "", content_for(:title) => "",

2
app/views/lettings_logs/index.html.erb

@ -7,7 +7,7 @@
<div class="app-filter-layout" data-controller="filter-layout"> <div class="app-filter-layout" data-controller="filter-layout">
<div class="govuk-button-group app-filter-toggle"> <div class="govuk-button-group app-filter-toggle">
<%= govuk_button_to "Create a new lettings log", lettings_logs_path %> <%= govuk_button_to "Create a new lettings log", create_new_log_lettings_logs_path, method: :get %>
<%#= govuk_link_to "Upload logs", bulk_upload_lettings_logs_path %> <%#= govuk_link_to "Upload logs", bulk_upload_lettings_logs_path %>
</div> </div>

8
config/routes.rb

@ -75,8 +75,16 @@ Rails.application.routes.draw do
resources :lettings_logs, path: "/logs" do resources :lettings_logs, path: "/logs" do
collection do collection do
get "create-new-log", to: "lettings_logs#create"
get "new-log", to: "lettings_logs#show"
post "new-form", to: "form#submit_form"
post "bulk-upload", to: "bulk_upload#bulk_upload" post "bulk-upload", to: "bulk_upload#bulk_upload"
get "bulk-upload", to: "bulk_upload#show" get "bulk-upload", to: "bulk_upload#show"
FormHandler.instance.forms.each do |_key, form|
form.pages.map do |page|
get "new/#{page.id.to_s.dasherize}", to: "form#new_log_#{page.id}"
end
end
end end
member do member do

Loading…
Cancel
Save