Matthew Phelan 3 years ago
parent
commit
88da04550d
  1. 4
      app/controllers/case_logs_controller.rb
  2. 14
      app/helpers/tasklist_helper.rb
  3. 34
      app/models/case_log.rb
  4. 7
      app/views/case_logs/_tasklist.html.erb
  5. 1
      app/views/form/page.html.erb
  6. 17
      db/migrate/20211011115946_rename_economic_status_fields.rb
  7. 3
      db/schema.rb
  8. 12
      spec/features/case_log_spec.rb
  9. 51
      spec/helpers/tasklist_helper_spec.rb

4
app/controllers/case_logs_controller.rb

@ -26,7 +26,7 @@ class CaseLogsController < ApplicationController
previous_page = params[:case_log][:previous_page] previous_page = params[:case_log][:previous_page]
questions_for_page = form.questions_for_page(previous_page) questions_for_page = form.questions_for_page(previous_page)
responses_for_page = question_responses(questions_for_page) responses_for_page = question_responses(questions_for_page)
@case_log.previous_page = previous_page
if @case_log.update(responses_for_page) if @case_log.update(responses_for_page)
redirect_path = form.next_page_redirect_path(previous_page) redirect_path = form.next_page_redirect_path(previous_page)
redirect_to(send(redirect_path, @case_log)) redirect_to(send(redirect_path, @case_log))
@ -45,7 +45,7 @@ class CaseLogsController < ApplicationController
form = Form.new(2021, 2022) form = Form.new(2021, 2022)
form.all_pages.map do |page_key, page_info| form.all_pages.map do |page_key, page_info|
define_method(page_key) do define_method(page_key) do |_errors = {}|
@case_log = CaseLog.find(params[:case_log_id]) @case_log = CaseLog.find(params[:case_log_id])
render "form/page", locals: { form: form, page_key: page_key, page_info: page_info } render "form/page", locals: { form: form, page_key: page_key, page_info: page_info }
end end

14
app/helpers/tasklist_helper.rb

@ -36,6 +36,15 @@ module TasklistHelper
subsections.count { |subsection| get_subsection_status(subsection, case_log, form.questions_for_subsection(subsection).keys) == status } subsections.count { |subsection| get_subsection_status(subsection, case_log, form.questions_for_subsection(subsection).keys) == status }
end end
def get_first_page_or_check_answers(subsection, case_log, form, questions)
path = if is_started?(subsection, case_log, questions)
"case_log_#{subsection}_check_answers_path"
else
"case_log_#{form.first_page_for_subsection(subsection)}_path"
end
send(path, case_log)
end
private private
def all_questions_completed(case_log) def all_questions_completed(case_log)
@ -46,4 +55,9 @@ private
status = get_subsection_status(subsection, case_log, questions) status = get_subsection_status(subsection, case_log, questions)
%i[not_started in_progress].include?(status) %i[not_started in_progress].include?(status)
end end
def is_started?(subsection, case_log, questions)
status = get_subsection_status(subsection, case_log, questions)
%i[in_progress completed].include?(status)
end
end end

34
app/models/case_log.rb

@ -1,5 +1,37 @@
class CaseLogValidator < ActiveModel::Validator
# Methods need to be named 'validate_' followed by field name
# this is how the metaprogramming of the method name being
# call in the validate method works.
def validate_tenant_code(record)
if record.tenant_code.blank?
record.errors.add :tenant_code, "Tenant code can't be blank"
end
end
def validate_tenant_age(record)
if record.tenant_age.blank?
record.errors.add :tenant_age, "Tenant age can't be blank"
elsif !/^[1-9][0-9]?$|^100$/.match?(record.tenant_age.to_s)
record.errors.add :tenant_age, "Tenant age must be between 0 and 100"
end
end
def validate(record)
question_to_validate = options[:previous_page]
if respond_to?("validate_#{question_to_validate}")
public_send("validate_#{question_to_validate}", record)
end
end
end
class CaseLog < ApplicationRecord class CaseLog < ApplicationRecord
validate :instance_validations
attr_writer :previous_page
enum status: { "in progress" => 0, "submitted" => 1 } enum status: { "in progress" => 0, "submitted" => 1 }
# validates :tenant_age, presence: true def instance_validations
validates_with CaseLogValidator, ({ previous_page: @previous_page } || {})
end
end end

7
app/views/case_logs/_tasklist.html.erb

@ -9,9 +9,10 @@
<ul class="app-task-list__items"> <ul class="app-task-list__items">
<% section_value["subsections"].map do |subsection_key, subsection_value| %> <% section_value["subsections"].map do |subsection_key, subsection_value| %>
<li class="app-task-list__item" id=<%= subsection_key %>> <li class="app-task-list__item" id=<%= subsection_key %>>
<% first_page = @form.first_page_for_subsection(subsection_key) %> <% questions_for_subsection = @form.questions_for_subsection(subsection_key).keys %>
<%= link_to subsection_value["label"], send("case_log_#{first_page}_path", @case_log), class: "task-name govuk-link" %> <% next_page_path = get_first_page_or_check_answers(subsection_key, @case_log, @form, questions_for_subsection) %>
<% subsection_status=get_subsection_status(subsection_key, @case_log, @form.questions_for_subsection(subsection_key).keys) %> <%= link_to subsection_value["label"], next_page_path, class: "task-name govuk-link" %>
<% subsection_status = get_subsection_status(subsection_key, @case_log, questions_for_subsection) %>
<strong class="govuk-tag app-task-list__tag <%= TasklistHelper::STYLES[subsection_status] %>"> <strong class="govuk-tag app-task-list__tag <%= TasklistHelper::STYLES[subsection_status] %>">
<%= TasklistHelper::STATUSES[subsection_status] %> <%= TasklistHelper::STATUSES[subsection_status] %>
</strong> </strong>

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

@ -6,6 +6,7 @@
<% end %> <% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= turbo_frame_tag "case_log_form", target: "_top" do %>
<% if page_info["header"].present? %> <% if page_info["header"].present? %>
<h1 class="govuk-heading-xl"> <h1 class="govuk-heading-xl">
<%= page_info["header"] %> <%= page_info["header"] %>

17
db/migrate/20211011115946_rename_economic_status_fields.rb

@ -1,5 +1,5 @@
class RenameEconomicStatusFields < ActiveRecord::Migration[6.1] class RenameEconomicStatusFields < ActiveRecord::Migration[6.1]
def change def up
change_table :case_logs, bulk: true do |t| change_table :case_logs, bulk: true do |t|
t.rename :person_2_economic, :person_2_economic_status t.rename :person_2_economic, :person_2_economic_status
t.rename :person_3_economic, :person_3_economic_status t.rename :person_3_economic, :person_3_economic_status
@ -8,7 +8,20 @@ class RenameEconomicStatusFields < ActiveRecord::Migration[6.1]
t.rename :person_6_economic, :person_6_economic_status t.rename :person_6_economic, :person_6_economic_status
t.rename :person_7_economic, :person_7_economic_status t.rename :person_7_economic, :person_7_economic_status
t.rename :person_8_economic, :person_8_economic_status t.rename :person_8_economic, :person_8_economic_status
t.rename :postcode, :property_postcode
end end
remove_column :case_logs, :postcode
end
def down
change_table :case_logs, bulk: true do |t|
t.rename :person_2_economic_status, :person_2_economic
t.rename :person_3_economic_status, :person_3_economic
t.rename :person_4_economic_status, :person_4_economic
t.rename :person_5_economic_status, :person_5_economic
t.rename :person_6_economic_status, :person_6_economic
t.rename :person_7_economic_status, :person_7_economic
t.rename :person_8_economic_status, :person_8_economic
end
add_column :case_logs, :postcode, :string
end end
end end

3
db/schema.rb

@ -101,12 +101,14 @@ ActiveRecord::Schema.define(version: 2021_10_11_115946) do
t.string "time_lived_in_la" t.string "time_lived_in_la"
t.string "time_on_la_waiting_list" t.string "time_on_la_waiting_list"
t.string "previous_la" t.string "previous_la"
t.string "property_postcode"
t.string "reasonable_preference" t.string "reasonable_preference"
t.string "reasonable_preference_reason" t.string "reasonable_preference_reason"
t.string "cbl_letting" t.string "cbl_letting"
t.string "chr_letting" t.string "chr_letting"
t.string "cap_letting" t.string "cap_letting"
t.string "outstanding_rent_or_charges" t.string "outstanding_rent_or_charges"
t.string "other_reason_for_leaving_last_settled_home"
t.boolean "accessibility_requirements_fully_wheelchair_accessible_housing" t.boolean "accessibility_requirements_fully_wheelchair_accessible_housing"
t.boolean "accessibility_requirements_wheelchair_access_to_essential_rooms" t.boolean "accessibility_requirements_wheelchair_access_to_essential_rooms"
t.boolean "accessibility_requirements_level_access_housing" t.boolean "accessibility_requirements_level_access_housing"
@ -130,7 +132,6 @@ ActiveRecord::Schema.define(version: 2021_10_11_115946) do
t.boolean "reasonable_preference_reason_medical_grounds" t.boolean "reasonable_preference_reason_medical_grounds"
t.boolean "reasonable_preference_reason_avoid_hardship" t.boolean "reasonable_preference_reason_avoid_hardship"
t.boolean "reasonable_preference_reason_do_not_know" t.boolean "reasonable_preference_reason_do_not_know"
t.string "property_postcode"
end end
end end

12
spec/features/case_log_spec.rb

@ -83,18 +83,6 @@ RSpec.describe "Test Features" do
end end
end end
it "displays the household questions when you click into that section" do
visit("/case_logs/#{id}")
click_link("Household characteristics")
expect(page).to have_field("case-log-tenant-code-field")
click_button("Save and continue")
expect(page).to have_field("case-log-tenant-age-field")
click_button("Save and continue")
expect(page).to have_field("case-log-tenant-gender-male-field")
visit page.driver.request.env["HTTP_REFERER"]
expect(page).to have_field("case-log-tenant-age-field")
end
describe "form questions" do describe "form questions" do
let(:case_log_with_checkbox_questions_answered) do let(:case_log_with_checkbox_questions_answered) do
FactoryBot.create( FactoryBot.create(

51
spec/helpers/tasklist_helper_spec.rb

@ -1,12 +1,15 @@
require "rails_helper" require "rails_helper"
RSpec.describe TasklistHelper do RSpec.describe TasklistHelper do
let!(:empty_case_log) { FactoryBot.create(:case_log) }
let!(:case_log) { FactoryBot.create(:case_log, :in_progress) }
let(:form) { Form.new(2021, 2022) }
describe "get subsection status" do describe "get subsection status" do
@form = Form.new(2021, 2022) let(:section) { "income_and_benefits" }
income_and_benefits_questions = @form.questions_for_subsection("income_and_benefits").keys let(:income_and_benefits_questions) { form.questions_for_subsection("income_and_benefits").keys }
declaration_questions = @form.questions_for_subsection("declaration").keys let(:declaration_questions) { form.questions_for_subsection("declaration").keys }
local_authority_questions = @form.questions_for_subsection("local_authority").keys let(:local_authority_questions) { form.questions_for_subsection("local_authority").keys }
let!(:case_log) { FactoryBot.create(:case_log) }
it "returns not started if none of the questions in the subsection are answered" do it "returns not started if none of the questions in the subsection are answered" do
status = get_subsection_status("income_and_benefits", case_log, income_and_benefits_questions) status = get_subsection_status("income_and_benefits", case_log, income_and_benefits_questions)
@ -38,47 +41,47 @@ RSpec.describe TasklistHelper do
end end
describe "get next incomplete section" do describe "get next incomplete section" do
let!(:case_log) { FactoryBot.create(:case_log) }
it "returns the first subsection name if it is not completed" do it "returns the first subsection name if it is not completed" do
@form = Form.new(2021, 2022) expect(get_next_incomplete_section(form, case_log)).to eq("household_characteristics")
expect(get_next_incomplete_section(@form, case_log)).to eq("household_characteristics")
end end
it "returns the first subsection name if it is partially completed" do it "returns the first subsection name if it is partially completed" do
@form = Form.new(2021, 2022)
case_log["tenant_code"] = 123 case_log["tenant_code"] = 123
expect(get_next_incomplete_section(@form, case_log)).to eq("household_characteristics") expect(get_next_incomplete_section(form, case_log)).to eq("household_characteristics")
end end
end end
describe "get sections count" do describe "get sections count" do
let!(:empty_case_log) { FactoryBot.create(:case_log) }
let!(:case_log) { FactoryBot.create(:case_log, :in_progress) }
it "returns the total of sections if no status is given" do it "returns the total of sections if no status is given" do
@form = Form.new(2021, 2022) expect(get_sections_count(form, empty_case_log)).to eq(9)
expect(get_sections_count(@form, empty_case_log)).to eq(9)
end end
it "returns 0 sections for completed sections if no sections are completed" do it "returns 0 sections for completed sections if no sections are completed" do
@form = Form.new(2021, 2022) expect(get_sections_count(form, empty_case_log, :completed)).to eq(0)
expect(get_sections_count(@form, empty_case_log, :completed)).to eq(0)
end end
it "returns the number of not started sections" do it "returns the number of not started sections" do
@form = Form.new(2021, 2022) expect(get_sections_count(form, empty_case_log, :not_started)).to eq(8)
expect(get_sections_count(@form, empty_case_log, :not_started)).to eq(8)
end end
it "returns the number of sections in progress" do it "returns the number of sections in progress" do
@form = Form.new(2021, 2022) expect(get_sections_count(form, case_log, :in_progress)).to eq(3)
expect(get_sections_count(@form, case_log, :in_progress)).to eq(3)
end end
it "returns 0 for invalid state" do it "returns 0 for invalid state" do
@form = Form.new(2021, 2022) expect(get_sections_count(form, case_log, :fake)).to eq(0)
expect(get_sections_count(@form, case_log, :fake)).to eq(0) end
end
describe "get_first_page_or_check_answers" do
let(:household_characteristics_questions) { form.questions_for_subsection("household_characteristics").keys }
it "returns the check answers page path if the section has been started already" do
expect(get_first_page_or_check_answers("household_characteristics", case_log, form, household_characteristics_questions)).to match(/check_answers/)
end
it "returns the first question page path for the section if it has not been started yet" do
expect(get_first_page_or_check_answers("household_characteristics", empty_case_log, form, household_characteristics_questions)).to match(/tenant_code/)
end end
end end
end end

Loading…
Cancel
Save