From ff6c355f10b84caabbb1feb5071f51501f44f263 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Tue, 20 Jun 2023 09:03:57 +0100 Subject: [PATCH] form handler to return all questions from lettings forms for all years with ordering interleaved functionality and tests --- app/models/form_handler.rb | 12 ++++++++ spec/models/form_handler_spec.rb | 51 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index c596e8b54..a1a57394f 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -52,6 +52,18 @@ class FormHandler ordered_questions end + def ordered_lettings_questions_for_all_years + lettings_forms = forms.filter { |name, _form| name.end_with? "lettings" }.values + ordered_questions = lettings_forms.pop.questions.uniq(&:id) + question_ids = ordered_questions.map(&:id) + all_questions_from_previous_forms = lettings_forms.flat_map(&:questions) + deprecated_questions_by_preceding_question_id(question_ids, all_questions_from_previous_forms).each do |preceding_question_id, deprecated_question| + index_of_preceding_question = ordered_questions.index { |q| q.id == preceding_question_id } + ordered_questions.insert(index_of_preceding_question + 1, deprecated_question) + end + ordered_questions + end + def deprecated_questions_by_preceding_question_id(current_form_question_ids, all_questions_from_previous_forms) deprecated_questions = {} all_questions_from_previous_forms.each_cons(2) do |preceding_question, question| diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index 547ed2f21..0c06e84fd 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -270,5 +270,56 @@ RSpec.describe FormHandler do expect(result.map(&:id)).to eq %w[1 1a_deprecated 2 2a_new 3] end end + + describe "#ordered_lettings_questions_for_all_years" do + let(:result) { described_class.instance.ordered_lettings_questions_for_all_years } + let(:now) { Time.zone.now } + + it "returns an array of questions" do + section = build(:section, :with_questions, question_ids: %w[1 2 3]) + lettings_form = FormFactory.new(year: 2936, type: "lettings") + .with_sections([section]) + .build + described_class.instance.use_fake_forms!({ only_lettings: lettings_form }) + expect(result).to(satisfy { |result| result.all? { |element| element.is_a?(Form::Question) } }) + end + + it "does not return multiple questions with the same id" do + first_section = build(:section, :with_questions, question_ids: %w[1 2 3]) + second_section = build(:section, :with_questions, question_ids: %w[2 3 4 5]) + lettings_form = FormFactory.new(year: 2936, type: "lettings") + .with_sections([first_section, second_section]) + .build + described_class.instance.use_fake_forms!({ only_lettings: lettings_form }) + expect(result.map(&:id)).to eq %w[1 2 3 4 5] + end + + it "returns the questions in the same order as the form" do + first_section = build(:section, :with_questions, question_ids: %w[1 2 3]) + second_section = build(:section, :with_questions, question_ids: %w[4 5 6]) + lettings_form = FormFactory.new(year: 2945, type: "lettings") + .with_sections([first_section, second_section]) + .build + described_class.instance.use_fake_forms!({ only_lettings: lettings_form }) + expect(result.map(&:id)).to eq %w[1 2 3 4 5 6] + end + + it "inserts questions from all years in their correct positions" do + original_section = build(:section, :with_questions, question_ids: %w[1 1a_deprecated 2 3]) + new_section = build(:section, :with_questions, question_ids: %w[1 2 2a_new 3]) + original_form = FormFactory.new(year: 2066, type: "lettings") + .with_sections([original_section]) + .build + new_form = FormFactory.new(year: 2485, type: "lettings") + .with_sections([new_section]) + .build + fake_forms = { + earlier_lettings: original_form, + newer_lettings: new_form, + } + described_class.instance.use_fake_forms!(fake_forms) + expect(result.map(&:id)).to eq %w[1 1a_deprecated 2 2a_new 3] + end + end # rubocop:enable RSpec/PredicateMatcher end