Browse Source

CLDC-1643 Fix inferred check answers value (#1257)

* refactor: make has_inferred_check_answers_value? do what it says it does

* refactor: combine method calls

* refactor: linting

* test: update

* refactor: copy

* db: update

* refactor: update db and lint

* feat: add present? back in to avoid 0 errors

* feat: flip ==

* refactor: flip ||

* refactor: flip name
pull/1272/head
natdeanlewissoftwire 2 years ago committed by GitHub
parent
commit
80b6297e8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      app/models/form/question.rb
  2. 2
      app/models/form/subsection.rb
  3. 68
      db/seeds.rb
  4. 4
      spec/models/form/question_spec.rb
  5. 2
      spec/requests/lettings_logs_controller_spec.rb

21
app/models/form/question.rb

@ -99,13 +99,6 @@ class Form::Question
!!derived !!derived
end end
def has_inferred_check_answers_value?(log)
return true if selected_answer_option_is_derived?(log)
return inferred_check_answers_value&.any? { |inferred_value| inferred_value["condition"].values.first == log[inferred_value["condition"].keys.first] } if inferred_check_answers_value.present?
false
end
def displayed_answer_options(log, _current_user = nil) def displayed_answer_options(log, _current_user = nil)
answer_options.select do |_key, val| answer_options.select do |_key, val|
!val.is_a?(Hash) || !val["depends_on"] || form.depends_on_met(val["depends_on"], log) !val.is_a?(Hash) || !val["depends_on"] || form.depends_on_met(val["depends_on"], log)
@ -113,7 +106,7 @@ class Form::Question
end end
def action_text(log) def action_text(log)
if has_inferred_check_answers_value?(log) if is_derived_or_has_inferred_check_answers_value?(log)
"Change" "Change"
elsif type == "checkbox" elsif type == "checkbox"
answer_options.keys.any? { |key| value_is_yes?(log[key]) } ? "Change" : "Answer" answer_options.keys.any? { |key| value_is_yes?(log[key]) } ? "Change" : "Answer"
@ -261,6 +254,10 @@ class Form::Question
@guidance_partial && @guidance_position == GuidancePosition::BOTTOM @guidance_partial && @guidance_position == GuidancePosition::BOTTOM
end end
def is_derived_or_has_inferred_check_answers_value?(log)
selected_answer_option_is_derived?(log) || has_inferred_check_answers_value?(log)
end
private private
def selected_answer_option_is_derived?(log) def selected_answer_option_is_derived?(log)
@ -268,6 +265,12 @@ private
selected_option.is_a?(Hash) && selected_option["depends_on"] && form.depends_on_met(selected_option["depends_on"], log) selected_option.is_a?(Hash) && selected_option["depends_on"] && form.depends_on_met(selected_option["depends_on"], log)
end end
def has_inferred_check_answers_value?(log)
return false unless inferred_check_answers_value
inferred_check_answers_value&.any? { |inferred_value| log[inferred_value["condition"].keys.first] == inferred_value["condition"].values.first }
end
def has_inferred_display_value?(log) def has_inferred_display_value?(log)
inferred_check_answers_value.present? && inferred_check_answers_value.any? { |inferred_value| log[inferred_value["condition"].keys.first] == inferred_value["condition"].values.first } inferred_check_answers_value.present? && inferred_check_answers_value.any? { |inferred_value| log[inferred_value["condition"].keys.first] == inferred_value["condition"].values.first }
end end
@ -310,7 +313,7 @@ private
def inferred_answer_value(log) def inferred_answer_value(log)
return unless inferred_check_answers_value return unless inferred_check_answers_value
inferred_answer = inferred_check_answers_value.find { |inferred_value| inferred_value["condition"].values.first == log[inferred_value["condition"].keys.first] } inferred_answer = inferred_check_answers_value.find { |inferred_value| log[inferred_value["condition"].keys.first] == inferred_value["condition"].values.first }
inferred_answer["value"] if inferred_answer.present? inferred_answer["value"] if inferred_answer.present?
end end

2
app/models/form/subsection.rb

@ -44,7 +44,7 @@ class Form::Subsection
def applicable_questions(log) def applicable_questions(log)
questions.select do |q| questions.select do |q|
(q.displayed_to_user?(log) && !q.derived?) || q.has_inferred_check_answers_value?(log) (q.displayed_to_user?(log) && !q.derived?) || q.is_derived_or_has_inferred_check_answers_value?(log)
end end
end end

68
db/seeds.rb

@ -146,36 +146,6 @@ unless Rails.env.test?
child_organisation: managing_agent2, child_organisation: managing_agent2,
) )
if (Rails.env.development? || Rails.env.review?) && SalesLog.count.zero?
SalesLog.find_or_create_by!(
saledate: Date.new(2023, 1, 1),
purchid: "1",
ownershipsch: 1,
type: 2,
jointpur: 1,
jointmore: 1,
)
SalesLog.find_or_create_by!(
saledate: Date.new(2023, 1, 1),
purchid: "1",
ownershipsch: 2,
type: 9,
jointpur: 1,
jointmore: 1,
)
SalesLog.find_or_create_by!(
saledate: Date.new(2023, 1, 1),
purchid: "1",
ownershipsch: 3,
type: 10,
companybuy: 1,
)
pp "Seeded a sales log of each type"
end
if Rails.env.development? || Rails.env.review? if Rails.env.development? || Rails.env.review?
User.find_or_create_by!( User.find_or_create_by!(
name: "Provider", name: "Provider",
@ -197,7 +167,7 @@ unless Rails.env.test?
user.confirmed_at = Time.zone.now user.confirmed_at = Time.zone.now
end end
User.find_or_create_by!( support_user = User.find_or_create_by!(
name: "Support", name: "Support",
email: "support@example.com", email: "support@example.com",
organisation: org, organisation: org,
@ -210,6 +180,42 @@ unless Rails.env.test?
pp "Seeded 3 dummy users" pp "Seeded 3 dummy users"
end end
if (Rails.env.development? || Rails.env.review?) && SalesLog.count.zero?
SalesLog.find_or_create_by!(
created_by: support_user,
owning_organisation: org,
saledate: Date.new(2023, 1, 1),
purchid: "1",
ownershipsch: 1,
type: 2,
jointpur: 1,
jointmore: 1,
)
SalesLog.find_or_create_by!(
created_by: support_user,
owning_organisation: org,
saledate: Date.new(2023, 1, 1),
purchid: "1",
ownershipsch: 2,
type: 9,
jointpur: 1,
jointmore: 1,
)
SalesLog.find_or_create_by!(
created_by: support_user,
owning_organisation: org,
saledate: Date.new(2023, 1, 1),
purchid: "1",
ownershipsch: 3,
type: 10,
companybuy: 1,
)
pp "Seeded a sales log of each type"
end
if Rails.env.development? || Rails.env.review? if Rails.env.development? || Rails.env.review?
dummy_org = Organisation.find_or_create_by!( dummy_org = Organisation.find_or_create_by!(
name: "FooBar LTD", name: "FooBar LTD",

4
spec/models/form/question_spec.rb

@ -274,8 +274,8 @@ RSpec.describe Form::Question, type: :model do
FactoryBot.create(:lettings_log, :in_progress, hhmemb: 2, details_known_2: 0, age2_known: 0, age2: 12) FactoryBot.create(:lettings_log, :in_progress, hhmemb: 2, details_known_2: 0, age2_known: 0, age2: 12)
end end
it "knows it has an inferred value for check answers" do it "knows it has an inferred value or is derived for check answers" do
expect(question.has_inferred_check_answers_value?(lettings_log)).to be true expect(question.is_derived_or_has_inferred_check_answers_value?(lettings_log)).to be true
end end
end end

2
spec/requests/lettings_logs_controller_spec.rb

@ -1037,7 +1037,7 @@ RSpec.describe LettingsLogsController, type: :request do
expect(page).not_to have_content(non_affected_lettings_logs.first.id) expect(page).not_to have_content(non_affected_lettings_logs.first.id)
end end
it "only displays the logs creted by the user" do it "only displays the logs created by the user" do
affected_lettings_logs.first.update!(created_by: other_user) affected_lettings_logs.first.update!(created_by: other_user)
get "/lettings-logs/update-logs", headers:, params: {} get "/lettings-logs/update-logs", headers:, params: {}
expect(page).to have_content(affected_lettings_logs.second.id) expect(page).to have_content(affected_lettings_logs.second.id)

Loading…
Cancel
Save