diff --git a/app/models/form/question.rb b/app/models/form/question.rb index 02f9f3235..fffa0059e 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -99,13 +99,6 @@ class Form::Question !!derived 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) answer_options.select do |_key, val| !val.is_a?(Hash) || !val["depends_on"] || form.depends_on_met(val["depends_on"], log) @@ -113,7 +106,7 @@ class Form::Question end def action_text(log) - if has_inferred_check_answers_value?(log) + if is_derived_or_has_inferred_check_answers_value?(log) "Change" elsif type == "checkbox" 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 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 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) 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) inferred_check_answers_value.present? && inferred_check_answers_value.any? { |inferred_value| log[inferred_value["condition"].keys.first] == inferred_value["condition"].values.first } end @@ -310,7 +313,7 @@ private def inferred_answer_value(log) 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? end diff --git a/app/models/form/subsection.rb b/app/models/form/subsection.rb index d51f8434d..3f5827c92 100644 --- a/app/models/form/subsection.rb +++ b/app/models/form/subsection.rb @@ -44,7 +44,7 @@ class Form::Subsection def applicable_questions(log) 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 diff --git a/db/seeds.rb b/db/seeds.rb index 87f9f2001..10823c62d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -146,36 +146,6 @@ unless Rails.env.test? 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? User.find_or_create_by!( name: "Provider", @@ -197,7 +167,7 @@ unless Rails.env.test? user.confirmed_at = Time.zone.now end - User.find_or_create_by!( + support_user = User.find_or_create_by!( name: "Support", email: "support@example.com", organisation: org, @@ -210,6 +180,42 @@ unless Rails.env.test? pp "Seeded 3 dummy users" 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? dummy_org = Organisation.find_or_create_by!( name: "FooBar LTD", diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index 9d289bcb4..48e71c76b 100644 --- a/spec/models/form/question_spec.rb +++ b/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) end - it "knows it has an inferred value for check answers" do - expect(question.has_inferred_check_answers_value?(lettings_log)).to be true + it "knows it has an inferred value or is derived for check answers" do + expect(question.is_derived_or_has_inferred_check_answers_value?(lettings_log)).to be true end end diff --git a/spec/requests/lettings_logs_controller_spec.rb b/spec/requests/lettings_logs_controller_spec.rb index 57311b70d..9db2c3236 100644 --- a/spec/requests/lettings_logs_controller_spec.rb +++ b/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) 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) get "/lettings-logs/update-logs", headers:, params: {} expect(page).to have_content(affected_lettings_logs.second.id)