Browse Source

CLDC-3245: Use derived variables to set default value for Q59

pull/2271/head
Robert Sullivan 2 years ago
parent
commit
f1bc6676da
  1. 8
      app/controllers/form_controller.rb
  2. 1
      app/models/derived_variables/sales_log_variables.rb
  3. 3
      app/models/form/question.rb
  4. 1
      app/models/form/sales/questions/buyers_organisations.rb
  5. 9
      app/models/sales_log.rb
  6. 4
      spec/models/form/sales/questions/buyers_organisations_spec.rb
  7. 16
      spec/models/sales_log_spec.rb

8
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

1
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?

3
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

1
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

9
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?

4
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(
{

16
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

Loading…
Cancel
Save