From f42977e40a269a45a53a09e726ddb7ee825983fe Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 10 Feb 2020 19:15:42 +0100 Subject: [PATCH] form: add number to section headers --- app/models/champs/header_section_champ.rb | 7 +++++ .../editable_champs/_header_section.html.haml | 4 +++ .../champs/header_section_champ_spec.rb | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 spec/models/champs/header_section_champ_spec.rb diff --git a/app/models/champs/header_section_champ.rb b/app/models/champs/header_section_champ.rb index 95df5d03d..47ec93eb4 100644 --- a/app/models/champs/header_section_champ.rb +++ b/app/models/champs/header_section_champ.rb @@ -2,4 +2,11 @@ class Champs::HeaderSectionChamp < Champ def search_terms # The user cannot enter any information here so it doesn’t make much sense to search end + + def section_index + dossier + .champs + .filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:header_section) } + .index(self) + 1 + end end diff --git a/app/views/shared/dossiers/editable_champs/_header_section.html.haml b/app/views/shared/dossiers/editable_champs/_header_section.html.haml index 16c6c6c9f..108300647 100644 --- a/app/views/shared/dossiers/editable_champs/_header_section.html.haml +++ b/app/views/shared/dossiers/editable_champs/_header_section.html.haml @@ -1,2 +1,6 @@ +- section_index = champ.section_index + %h2.header-section + - if section_index + = "#{section_index}." = champ.libelle diff --git a/spec/models/champs/header_section_champ_spec.rb b/spec/models/champs/header_section_champ_spec.rb new file mode 100644 index 000000000..99b678241 --- /dev/null +++ b/spec/models/champs/header_section_champ_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe Champs::CheckboxChamp do + let(:types_de_champ) do + [ + create(:type_de_champ_header_section), + create(:type_de_champ_civilite), + create(:type_de_champ_text), + create(:type_de_champ_header_section), + create(:type_de_champ_email) + ] + end + + let(:procedure) { create(:procedure, types_de_champ: types_de_champ) } + let(:dossier) { create(:dossier, procedure: procedure) } + + describe '#section_index' do + let(:first_header) { dossier.champs[0] } + let(:second_header) { dossier.champs[3] } + + it 'returns the index of the section (starting from 1)' do + expect(first_header.section_index).to eq 1 + expect(second_header.section_index).to eq 2 + end + end +end