diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 368af3bab..17fd9af77 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -102,12 +102,6 @@ private result[question.id] = question_params end - if %w[checkbox].include?(question.type) && - !question.default_answer.nil? && - question.answer_keys_without_dividers.all? { |answer_key| result[answer_key] != 1 } - result[question.default_answer] = 1 - end - if question.id == "owning_organisation_id" owning_organisation = result["owning_organisation_id"].present? ? Organisation.find(result["owning_organisation_id"]) : nil if current_user.support? && @log.managing_organisation.blank? && owning_organisation&.managing_agents&.empty? @@ -235,8 +229,6 @@ private end def question_missing_response?(responses_for_page, question) - return unless question.default_answer.nil? - if %w[checkbox validation_override].include?(question.type) answered = question.answer_keys_without_dividers.map do |option| session["fields"][option] = @log[option] = params[@log.model_name.param_key][question.id].include?(option) ? 1 : 0 diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index ec8d21b30..4fd98319f 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -4,6 +4,7 @@ module DerivedVariables::SalesLogVariables def set_derived_fields! reset_invalidated_derived_values!(DEPENDENCIES) + self.pregblank = 1 if self.no_buyer_organisation? self.ethnic = 17 if ethnic_refused? self.mscharge = nil if no_monthly_leasehold_charges? if exdate.present? diff --git a/app/models/form/question.rb b/app/models/form/question.rb index 3bea2013a..391771ad9 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -6,7 +6,7 @@ class Form::Question :top_guidance_partial, :bottom_guidance_partial, :prefix, :suffix, :requires_js, :fields_added, :derived, :check_answers_card_number, :unresolved_hint_text, :question_number, :hide_question_number_on_page, - :plain_label, :error_label, :default_answer + :plain_label, :error_label def initialize(id, hsh, page) @id = id @@ -42,7 +42,6 @@ class Form::Question @plain_label = hsh["plain_label"] @error_label = hsh["error_label"] @disable_clearing_if_not_routed_or_dynamic_answer_options = hsh["disable_clearing_if_not_routed_or_dynamic_answer_options"] - @default_answer = hsh["default_answer"] end end diff --git a/app/models/form/sales/questions/buyers_organisations.rb b/app/models/form/sales/questions/buyers_organisations.rb index 3faa33f67..9c1cdf0e1 100644 --- a/app/models/form/sales/questions/buyers_organisations.rb +++ b/app/models/form/sales/questions/buyers_organisations.rb @@ -7,7 +7,6 @@ class Form::Sales::Questions::BuyersOrganisations < ::Form::Question @type = "checkbox" @hint_text = "Select all that apply. This question is optional. If no options are applicable, leave the options blank, and select save and continue." @answer_options = ANSWER_OPTIONS - @default_answer = "pregblank" @question_number = 59 end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index b4eab2093..b17956b3a 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -78,7 +78,7 @@ class SalesLog < Log scope.pluck("ARRAY_AGG(id)") } - OPTIONAL_FIELDS = %w[purchid othtype].freeze + OPTIONAL_FIELDS = %w[purchid othtype buyers_organisations].freeze RETIREMENT_AGES = { "M" => 65, "F" => 60, "X" => 65 }.freeze DUPLICATE_LOG_ATTRIBUTES = %w[owning_organisation_id purchid saledate age1_known age1 sex1 ecstat1 postcode_full].freeze @@ -394,6 +394,13 @@ class SalesLog < Log has_mscharge&.zero? end + def no_buyer_organisation? + pregyrha&.zero? && + pregla&.zero? && + pregghb&.zero? && + pregother&.zero? + end + def buyers_age_for_old_persons_shared_ownership_invalid? return unless old_persons_shared_ownership? diff --git a/spec/models/form/sales/questions/buyers_organisations_spec.rb b/spec/models/form/sales/questions/buyers_organisations_spec.rb index 74ad964d5..b0c932acc 100644 --- a/spec/models/form/sales/questions/buyers_organisations_spec.rb +++ b/spec/models/form/sales/questions/buyers_organisations_spec.rb @@ -35,10 +35,6 @@ RSpec.describe Form::Sales::Questions::BuyersOrganisations, type: :model do expect(question.hint_text).to eq("Select all that apply. This question is optional. If no options are applicable, leave the options blank, and select save and continue.") end - it "has the correct default" do - expect(question.default_answer).to eq("pregblank") - end - it "has the correct answer_options" do expect(question.answer_options).to eq( { diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 00792b0bd..9c6810963 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -596,6 +596,22 @@ RSpec.describe SalesLog, type: :model do expect { sales_log.update!(nationality_all_buyer2_group: nil) }.not_to change(sales_log, :nationality_all_buyer2) end end + + it "sets pregblank field when no buyer organisation is selected" do + expect { sales_log.update!( + pregyrha: 0, + pregla: 0, + pregghb: 0, + pregother: 0) } + .to change(sales_log, :pregblank).to 1 + end + + %i[pregyrha pregla pregghb pregother].each do |field| + it "does not set pregblank field when #{field} is selected" do + expect { sales_log.update!({ field => 1, })} + .not_to change(sales_log, :pregblank) + end + end end context "when saving addresses" do