Browse Source

form handler to return all questions from lettings forms for all years with ordering interleaved

functionality and tests
pull/1719/head
Arthur Campbell 3 years ago
parent
commit
ff6c355f10
  1. 12
      app/models/form_handler.rb
  2. 51
      spec/models/form_handler_spec.rb

12
app/models/form_handler.rb

@ -52,6 +52,18 @@ class FormHandler
ordered_questions ordered_questions
end 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) def deprecated_questions_by_preceding_question_id(current_form_question_ids, all_questions_from_previous_forms)
deprecated_questions = {} deprecated_questions = {}
all_questions_from_previous_forms.each_cons(2) do |preceding_question, question| all_questions_from_previous_forms.each_cons(2) do |preceding_question, question|

51
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] expect(result.map(&:id)).to eq %w[1 1a_deprecated 2 2a_new 3]
end end
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 # rubocop:enable RSpec/PredicateMatcher
end end

Loading…
Cancel
Save