diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index 703fabf18..ac2600fec 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -39,16 +39,12 @@ class AttestationTemplate < ApplicationRecord end def unspecified_champs_for_dossier(dossier) - all_champs_with_libelle_index = (dossier.champs + dossier.champs_private) - .reduce({}) do |acc, champ| - acc[champ.libelle] = champ - acc - end + all_champs_with_libelle_index = (dossier.champs + dossier.champs_private).index_by { |champ| "tdc#{champ.stable_id}" } used_tags.filter_map do |used_tag| corresponding_champ = all_champs_with_libelle_index[used_tag] - if corresponding_champ && corresponding_champ.value.blank? + if corresponding_champ && corresponding_champ.blank? corresponding_champ end end @@ -113,7 +109,7 @@ class AttestationTemplate < ApplicationRecord # We can't use flat_map as scan will return 3 levels of array, # using flat_map would give us 2, whereas flatten will # give us 1, which is what we want - [title, body] + [normalize_tags(title), normalize_tags(body)] .map { |str| str.scan(delimiters_regex) } .flatten end diff --git a/app/models/champ.rb b/app/models/champ.rb index 30c896e11..539c5af8b 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -95,14 +95,7 @@ class Champ < ApplicationRecord end def blank? - case type_de_champ.type_champ - when TypeDeChamp.type_champs.fetch(:carte) - geo_areas.blank? || value == '[]' - when TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) - value.blank? || value == '[]' - else - value.blank? - end + value.blank? end def search_terms diff --git a/app/models/champs/carte_champ.rb b/app/models/champs/carte_champ.rb index ccec62cc0..f8b67c82c 100644 --- a/app/models/champs/carte_champ.rb +++ b/app/models/champs/carte_champ.rb @@ -109,6 +109,10 @@ class Champs::CarteChamp < Champ nil end + def blank? + geo_areas.blank? + end + private def selection_utilisateur_legacy_geometry diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index c6aa132a7..5a239151c 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -62,6 +62,10 @@ class Champs::MultipleDropDownListChamp < Champ enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX end + def blank? + selected_options.blank? + end + private def format_before_save diff --git a/app/models/champs/repetition_champ.rb b/app/models/champs/repetition_champ.rb index 60064d04c..7e9ac54aa 100644 --- a/app/models/champs/repetition_champ.rb +++ b/app/models/champs/repetition_champ.rb @@ -30,8 +30,8 @@ class Champs::RepetitionChamp < Champ end end - def mandatory_and_blank? - mandatory? && champs.empty? + def blank? + champs.empty? end def search_terms diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb index 2b9b0a6e3..f2cc90961 100644 --- a/spec/factories/champ.rb +++ b/spec/factories/champ.rb @@ -176,7 +176,11 @@ FactoryBot.define do factory :champ_repetition, class: 'Champs::RepetitionChamp' do type_de_champ { association :type_de_champ_repetition, procedure: dossier.procedure } - after(:build) do |champ_repetition, _evaluator| + transient do + rows { 2 } + end + + after(:build) do |champ_repetition, evaluator| types_de_champ = champ_repetition.type_de_champ.types_de_champ existing_type_de_champ_text = types_de_champ.find { |tdc| tdc.libelle == 'Nom' } type_de_champ_text = existing_type_de_champ_text || build( @@ -195,12 +199,12 @@ FactoryBot.define do ) champ_repetition.type_de_champ.types_de_champ << [type_de_champ_text, type_de_champ_number] - champ_repetition.champs << [ - build(:champ_text, dossier: champ_repetition.dossier, row: 0, type_de_champ: type_de_champ_text, parent: champ_repetition), - build(:champ_number, dossier: champ_repetition.dossier, row: 0, type_de_champ: type_de_champ_number, parent: champ_repetition), - build(:champ_text, dossier: champ_repetition.dossier, row: 1, type_de_champ: type_de_champ_text, parent: champ_repetition), - build(:champ_number, dossier: champ_repetition.dossier, row: 1, type_de_champ: type_de_champ_number, parent: champ_repetition) - ] + evaluator.rows.times do |row| + champ_repetition.champs << [ + build(:champ_text, dossier: champ_repetition.dossier, row: row, type_de_champ: type_de_champ_text, parent: champ_repetition), + build(:champ_number, dossier: champ_repetition.dossier, row: row, type_de_champ: type_de_champ_number, parent: champ_repetition) + ] + end end trait :without_champs do diff --git a/spec/models/champ_shared_example.rb b/spec/models/champ_shared_example.rb index 1e32f858b..beebf0daf 100644 --- a/spec/models/champ_shared_example.rb +++ b/spec/models/champ_shared_example.rb @@ -11,16 +11,32 @@ shared_examples 'champ_spec' do context 'when carte mandatory and blank' do let(:type_de_champ) { build(:type_de_champ_carte, mandatory: mandatory) } - let(:value) { '[]' } + let(:champ) { build(:champ_carte, type_de_champ: type_de_champ, value: value) } + let(:value) { nil } it { expect(champ.mandatory_and_blank?).to be(true) } end context 'when multiple_drop_down_list mandatory and blank' do let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, mandatory: mandatory) } + let(:champ) { build(:champ_multiple_drop_down_list, type_de_champ: type_de_champ, value: value) } let(:value) { '[]' } it { expect(champ.mandatory_and_blank?).to be(true) } end + context 'when repetition blank' do + let(:type_de_champ) { build(:type_de_champ_repetition) } + let(:champ) { build(:champ_repetition, type_de_champ: type_de_champ, rows: 0) } + + it { expect(champ.blank?).to be(true) } + end + + context 'when repetition not blank' do + let(:type_de_champ) { build(:type_de_champ_repetition) } + let(:champ) { build(:champ_repetition, type_de_champ: type_de_champ) } + + it { expect(champ.blank?).to be(false) } + end + context 'when not blank' do let(:value) { 'yop' } it { expect(champ.mandatory_and_blank?).to be(false) } diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index e4ac26716..97caeeceb 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -564,7 +564,7 @@ describe Dossier do describe "#unspecified_attestation_champs" do let(:procedure) { create(:procedure, attestation_template: attestation_template, types_de_champ: types_de_champ, types_de_champ_private: types_de_champ_private) } - let(:dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:en_instruction)) } + let(:dossier) { create(:dossier, :en_instruction, procedure: procedure) } let(:types_de_champ) { [] } let(:types_de_champ_private) { [] }