Browse Source

small refactors and rubocop

pull/49/head
Kat 4 years ago
parent
commit
71b0a285ec
  1. 6
      app/controllers/case_logs_controller.rb
  2. 20
      app/helpers/check_answers_helper.rb
  3. 6
      app/models/case_log.rb
  4. 2
      app/views/form/_check_answers_table.html.erb
  5. 2
      db/schema.rb
  6. 23
      spec/models/case_log_spec.rb

6
app/controllers/case_logs_controller.rb

@ -134,10 +134,8 @@ private
questions_for_page.each do |question, content| questions_for_page.each do |question, content|
if content.key?("conditional_route_to") if content.key?("conditional_route_to")
content["conditional_route_to"].each do |route, answer| content["conditional_route_to"].each do |route, answer|
if !responses_for_page[question].nil? if responses_for_page[question].present? && answer.include?(responses_for_page[question])
if answer.include?(responses_for_page[question]) return "case_log_#{route}_path"
return "case_log_#{route}_path"
end
end end
end end
end end

20
app/helpers/check_answers_helper.rb

@ -13,36 +13,36 @@ module CheckAnswersHelper
total_questions = {} total_questions = {}
page_name = form.pages_for_subsection(subsection).keys.first page_name = form.pages_for_subsection(subsection).keys.first
while page_name != "check_answers" && page_name != :check_answers while page_name.to_s != "check_answers"
questions = form.questions_for_page(page_name) questions = form.questions_for_page(page_name)
question_key = questions.keys[0] question_key = questions.keys[0]
question_value = questions.values[0] question_value = questions.values[0]
appliccable_questions = filter_conditional_questions(questions, case_log) applicable_questions = filter_conditional_questions(questions, case_log)
total_questions = total_questions.merge(appliccable_questions) total_questions = total_questions.merge(applicable_questions)
page_name = get_next_page_name(form, page_name, appliccable_questions, question_key, case_log, question_value) page_name = get_next_page_name(form, page_name, applicable_questions, question_key, case_log, question_value)
end end
total_questions total_questions
end end
def filter_conditional_questions(questions, case_log) def filter_conditional_questions(questions, case_log)
appliccable_questions = questions applicable_questions = questions
questions.each do |k, question| questions.each do |k, question|
question.fetch("conditional_for", []).each do |conditional_question_key, condition| question.fetch("conditional_for", []).each do |conditional_question_key, condition|
if condition_not_met(case_log, k, question, condition) if condition_not_met(case_log, k, question, condition)
appliccable_questions = appliccable_questions.reject { |z| z == conditional_question_key } applicable_questions = applicable_questions.reject { |z| z == conditional_question_key }
end end
end end
end end
appliccable_questions applicable_questions
end end
def get_next_page_name(form, page_name, appliccable_questions, question_key, case_log, question_value) def get_next_page_name(form, page_name, applicable_questions, question_key, case_log, question_value)
if appliccable_questions[question_key].key?("conditional_route_to") if applicable_questions[question_key].key?("conditional_route_to")
appliccable_questions[question_key]["conditional_route_to"].each do |conditional_page_key, condition| applicable_questions[question_key]["conditional_route_to"].each do |conditional_page_key, condition|
unless condition_not_met(case_log, question_key, question_value, condition) unless condition_not_met(case_log, question_key, question_value, condition)
return conditional_page_key return conditional_page_key
end end

6
app/models/case_log.rb

@ -28,7 +28,7 @@ class CaseLogValidator < ActiveModel::Validator
end end
end end
end end
def validate_other_reason_for_leaving_last_settled_home(record) def validate_other_reason_for_leaving_last_settled_home(record)
if record.reason_for_leaving_last_settled_home == "Other" && record.other_reason_for_leaving_last_settled_home.blank? if record.reason_for_leaving_last_settled_home == "Other" && record.other_reason_for_leaving_last_settled_home.blank?
record.errors.add :other_reason_for_leaving_last_settled_home, "If reason for leaving settled home is other then the other reason must be provided" record.errors.add :other_reason_for_leaving_last_settled_home, "If reason for leaving settled home is other then the other reason must be provided"
@ -44,10 +44,10 @@ class CaseLogValidator < ActiveModel::Validator
# that have just been submitted. If we're submitting a log via API or Bulk Upload # that have just been submitted. If we're submitting a log via API or Bulk Upload
# we want to validate all data fields. # we want to validate all data fields.
question_to_validate = options[:previous_page] question_to_validate = options[:previous_page]
if question_to_validate if question_to_validate
if respond_to?("validate_#{question_to_validate}") if respond_to?("validate_#{question_to_validate}")
public_send("validate_#{question_to_validate}", record) public_send("validate_#{question_to_validate}", record)
end end
else else
# This assumes that all methods in this class other than this one are # This assumes that all methods in this class other than this one are
# validations to be run # validations to be run

2
app/views/form/_check_answers_table.html.erb

@ -1,7 +1,7 @@
<dl class="govuk-summary-list govuk-!-margin-bottom-9"> <dl class="govuk-summary-list govuk-!-margin-bottom-9">
<div class="govuk-summary-list__row"> <div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key"> <dt class="govuk-summary-list__key">
<%= question_info["check_answer_label"].to_s != "" ? question_info["check_answer_label"].to_s : question_info["header"].to_s%> <%= question_info["check_answer_label"].to_s.present? ? question_info["check_answer_label"].to_s : question_info["header"].to_s%>
<dt> <dt>
<dd class="govuk-summary-list__value"> <dd class="govuk-summary-list__value">
<%= @case_log[question_title] %> <%= @case_log[question_title] %>

2
db/schema.rb

@ -84,6 +84,7 @@ ActiveRecord::Schema.define(version: 2021_10_15_090040) do
t.string "property_void_date" t.string "property_void_date"
t.string "property_major_repairs" t.string "property_major_repairs"
t.string "property_major_repairs_date" t.string "property_major_repairs_date"
t.integer "property_number_of_times_relet"
t.string "property_wheelchair_accessible" t.string "property_wheelchair_accessible"
t.string "net_income" t.string "net_income"
t.string "net_income_frequency" t.string "net_income_frequency"
@ -131,7 +132,6 @@ ActiveRecord::Schema.define(version: 2021_10_15_090040) do
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.datetime "discarded_at" t.datetime "discarded_at"
t.integer "property_number_of_times_relet"
t.index ["discarded_at"], name: "index_case_logs_on_discarded_at" t.index ["discarded_at"], name: "index_case_logs_on_discarded_at"
end end

23
spec/models/case_log_spec.rb

@ -28,32 +28,31 @@ RSpec.describe Form, type: :model do
describe "reasonable preference validation" do describe "reasonable preference validation" do
it "if given reasonable preference is yes a reason must be selected" do it "if given reasonable preference is yes a reason must be selected" do
expect { expect {
CaseLog.create!(reasonable_preference: "Yes", CaseLog.create!(reasonable_preference: "Yes",
reasonable_preference_reason_homeless: nil, reasonable_preference_reason_homeless: nil,
reasonable_preference_reason_unsatisfactory_housing: nil, reasonable_preference_reason_unsatisfactory_housing: nil,
reasonable_preference_reason_medical_grounds: nil, reasonable_preference_reason_medical_grounds: nil,
reasonable_preference_reason_avoid_hardship: nil, reasonable_preference_reason_avoid_hardship: nil,
reasonable_preference_reason_do_not_know: nil reasonable_preference_reason_do_not_know: nil)
)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "if not previously homeless reasonable preference should not be selected" do it "if not previously homeless reasonable preference should not be selected" do
expect { expect {
CaseLog.create!( CaseLog.create!(
homelessness: "No", homelessness: "No",
reasonable_preference: "Yes" reasonable_preference: "Yes",
) )
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "if not given reasonable preference a reason should not be selected" do it "if not given reasonable preference a reason should not be selected" do
expect { expect {
CaseLog.create!( CaseLog.create!(
homelessness: "Yes", homelessness: "Yes",
reasonable_preference: "No", reasonable_preference: "No",
reasonable_preference_reason_homeless: true reasonable_preference_reason_homeless: true,
) )
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end

Loading…
Cancel
Save