From b1e040ac5ae7e465180cff791d717f2a4d72aa20 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Wed, 19 Apr 2023 16:31:15 +0100 Subject: [PATCH] create a method on the FormHandler that returns the sales form questions for all years in the order that they appear in the form --- app/models/form_handler.rb | 24 ++++++++++++++++ spec/models/form_handler_spec.rb | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index ee84c3e32..aa56fc587 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -40,6 +40,30 @@ class FormHandler } end + # there is a problem with this that is potentially more general, which is what we plan to do about + # forms that are 2+ years old. + # eg. if there is a question/attribute on the 21/22 form that no longer exists, we will not export that already + # What is the status of old forms on the web app? I believe we are not allowed to edit but are we allowed to view? + # If yes that might be a problem. + # If not I suggest that it might be easier in this method to replace line 1 with: + # sales_forms = [2021..2023].each { |year| Form.new(nil, year, SALES_SECTIONS, sales) } + # sidenote, why do we save a reference to the next years sales log in the FormHandler? + def ordered_sales_questions_for_all_years + sales_forms = forms.filter { |name, _form| name.end_with? 'sales' }.values + ordered_questions = sales_forms.pop.questions.uniq { |q| q.id } + index_of_last_question = 0 + sales_forms.flat_map(&:questions).each_with_index do |question, i| + index = ordered_questions.index { |q| q.id == question.id } + if index + index_of_last_question = index + else + ordered_questions.insert(index_of_last_question + 1, question) + index_of_last_question += 1 + end + end + ordered_questions + end + def lettings_forms forms = {} directories.each do |directory| diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index bbeb56e55..2a5e8be59 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -219,5 +219,54 @@ RSpec.describe FormHandler do end end end + + describe "#ordered_sales_questions_for_all_years" do + let(:result) { FormHandler.instance.ordered_sales_questions_for_all_years } + let(:now) { Time.zone.now } + + it "returns an array of questions" do + 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 + unique_id_count = result.map(&:id).uniq.count + expect(result.count).to be unique_id_count + end + + it "returns the questions in the same order as the form" do + household_situation_question_ids = %w[prevten ppcodenk ppostcode_full previous_la_known prevloc buyers_organisations] + index_of_household_situation_start = 0 + household_situation_question_ids.each_with_index do |id, i| + if i = 0 + index_of_household_situation_start = result.index { |q| q.id == id } + else + question_index = result.index { |q| q.id == id } + expect(question_index).to be index_of_household_situation_start + i + end + end + end + + it "returns questions form all years" do + current_sales_question_ids = FormHandler.instance.forms["current_sales"].questions.map(&:id).uniq + previous_sales_question_ids = FormHandler.instance.forms["previous_sales"].questions.map(&:id).uniq + expect(result.count).to be > current_sales_question_ids.count + expect(result.count).to be > previous_sales_question_ids.count + end + + it "inserts questions from previous years that do not appear in more recent years in the correct position" do + current_sales_question_ids = FormHandler.instance.forms["current_sales"].questions.map(&:id).uniq + previous_sales_question_ids = FormHandler.instance.forms["previous_sales"].questions.map(&:id).uniq + obsolete_question_ids = previous_sales_question_ids - current_sales_question_ids + expect(obsolete_question_ids.count).to be_positive + obsolete_question_ids.each do |obsolete_id| + index = previous_sales_question_ids.index(obsolete_id) + previous_question_id = previous_sales_question_ids[index - 1] + expect(result).to include { |q| q.id == id } + index_of_previous_question_in_result = result.index { |q| q.id == previous_question_id } + index_of_obsolete_question_in_result = result.index { |q| q.id == obsolete_id } + expect(index_of_obsolete_question_in_result).to be index_of_previous_question_in_result + 1 + end + end + end # rubocop:enable RSpec/PredicateMatcher end