feat(dossier): exclude conditionally invisible fields from mandatory check

This commit is contained in:
Paul Chavard 2022-06-27 11:55:56 +02:00
parent 120b593015
commit 0f9d8b6a39
7 changed files with 21 additions and 21 deletions

View file

@ -89,8 +89,8 @@ class Champ < ApplicationRecord
@sections ||= dossier&.sections_for(self)
end
def mandatory_and_blank?
mandatory? && blank?
def mandatory_blank_and_visible?
mandatory? && blank? && visible?
end
def blank?

View file

@ -38,8 +38,8 @@ class Champs::PieceJustificativeChamp < Champ
# We dont know how to search inside documents yet
end
def mandatory_and_blank?
mandatory? && !piece_justificative_file.attached?
def mandatory_blank_and_visible?
mandatory? && !piece_justificative_file.attached? && visible?
end
def for_export

View file

@ -24,7 +24,7 @@ class Champs::SiretChamp < Champ
etablissement.present? ? etablissement.search_terms : [value]
end
def mandatory_and_blank?
mandatory? && Siret.new(siret: value).invalid?
def mandatory_blank_and_visible?
mandatory? && Siret.new(siret: value).invalid? && visible?
end
end

View file

@ -32,8 +32,8 @@ class Champs::TitreIdentiteChamp < Champ
# We dont know how to search inside documents yet
end
def mandatory_and_blank?
mandatory? && !piece_justificative_file.attached?
def mandatory_blank_and_visible?
mandatory? && !piece_justificative_file.attached? && visible?
end
def for_export

View file

@ -1077,7 +1077,7 @@ class Dossier < ApplicationRecord
def check_mandatory_champs
(champs + champs.filter(&:repetition?).flat_map(&:champs))
.filter(&:mandatory_and_blank?)
.filter(&:mandatory_blank_and_visible?)
.map do |champ|
"Le champ #{champ.libelle.truncate(200)} doit être rempli."
end

View file

@ -1,26 +1,26 @@
shared_examples 'champ_spec' do
describe 'mandatory_and_blank?' do
describe 'mandatory_blank_and_visible?' do
let(:type_de_champ) { build(:type_de_champ, mandatory: mandatory) }
let(:champ) { build(:champ, type_de_champ: type_de_champ, value: value) }
let(:value) { '' }
let(:mandatory) { true }
context 'when mandatory and blank' do
it { expect(champ.mandatory_and_blank?).to be(true) }
it { expect(champ.mandatory_blank_and_visible?).to be(true) }
end
context 'when carte mandatory and blank' do
let(:type_de_champ) { build(:type_de_champ_carte, mandatory: mandatory) }
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) }
it { expect(champ.mandatory_blank_and_visible?).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) }
it { expect(champ.mandatory_blank_and_visible?).to be(true) }
end
context 'when repetition blank' do
@ -39,18 +39,18 @@ shared_examples 'champ_spec' do
context 'when not blank' do
let(:value) { 'yop' }
it { expect(champ.mandatory_and_blank?).to be(false) }
it { expect(champ.mandatory_blank_and_visible?).to be(false) }
end
context 'when not mandatory' do
let(:mandatory) { false }
it { expect(champ.mandatory_and_blank?).to be(false) }
it { expect(champ.mandatory_blank_and_visible?).to be(false) }
end
context 'when not mandatory or blank' do
let(:value) { 'u' }
let(:mandatory) { false }
it { expect(champ.mandatory_and_blank?).to be(false) }
it { expect(champ.mandatory_blank_and_visible?).to be(false) }
end
end

View file

@ -78,7 +78,7 @@ describe Champs::LinkedDropDownListChamp do
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, drop_down_list_value: value) }
it 'blank is fine' do
is_expected.not_to be_mandatory_and_blank
is_expected.not_to be_mandatory_blank_and_visible
end
end
@ -86,27 +86,27 @@ describe Champs::LinkedDropDownListChamp do
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, mandatory: true, drop_down_list_value: value) }
context 'when there is no value' do
it { is_expected.to be_mandatory_and_blank }
it { is_expected.to be_mandatory_blank_and_visible }
end
context 'when there is a primary value' do
before { subject.primary_value = 'Primary' }
context 'when there is no secondary value' do
it { is_expected.to be_mandatory_and_blank }
it { is_expected.to be_mandatory_blank_and_visible }
end
context 'when there is a secondary value' do
before { subject.secondary_value = 'Secondary' }
it { is_expected.not_to be_mandatory_and_blank }
it { is_expected.not_to be_mandatory_blank_and_visible }
end
context 'when there is nothing to select for the secondary value' do
let(:value) { "--A--\nAbbott\nAbelard\n--B--\n--C--\nCynthia" }
before { subject.primary_value = 'B' }
it { is_expected.not_to be_mandatory_and_blank }
it { is_expected.not_to be_mandatory_blank_and_visible }
end
end
end