Improuve champ blank check

This commit is contained in:
Paul Chavard 2021-07-21 10:45:59 +02:00
parent 38b73f0fee
commit ac0f50b488
6 changed files with 39 additions and 18 deletions

View file

@ -95,14 +95,7 @@ class Champ < ApplicationRecord
end end
def blank? def blank?
case type_de_champ.type_champ value.blank?
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
end end
def search_terms def search_terms

View file

@ -109,6 +109,10 @@ class Champs::CarteChamp < Champ
nil nil
end end
def blank?
geo_areas.blank?
end
private private
def selection_utilisateur_legacy_geometry def selection_utilisateur_legacy_geometry

View file

@ -62,6 +62,10 @@ class Champs::MultipleDropDownListChamp < Champ
enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX
end end
def blank?
selected_options.blank?
end
private private
def format_before_save def format_before_save

View file

@ -30,8 +30,8 @@ class Champs::RepetitionChamp < Champ
end end
end end
def mandatory_and_blank? def blank?
mandatory? && champs.empty? champs.empty?
end end
def search_terms def search_terms

View file

@ -176,7 +176,11 @@ FactoryBot.define do
factory :champ_repetition, class: 'Champs::RepetitionChamp' do factory :champ_repetition, class: 'Champs::RepetitionChamp' do
type_de_champ { association :type_de_champ_repetition, procedure: dossier.procedure } 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 types_de_champ = champ_repetition.type_de_champ.types_de_champ
existing_type_de_champ_text = types_de_champ.find { |tdc| tdc.libelle == 'Nom' } existing_type_de_champ_text = types_de_champ.find { |tdc| tdc.libelle == 'Nom' }
type_de_champ_text = existing_type_de_champ_text || build( 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.type_de_champ.types_de_champ << [type_de_champ_text, type_de_champ_number]
champ_repetition.champs << [ evaluator.rows.times do |row|
build(:champ_text, dossier: champ_repetition.dossier, row: 0, type_de_champ: type_de_champ_text, parent: champ_repetition), champ_repetition.champs << [
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: row, type_de_champ: type_de_champ_text, 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: row, type_de_champ: type_de_champ_number, parent: champ_repetition)
build(:champ_number, dossier: champ_repetition.dossier, row: 1, type_de_champ: type_de_champ_number, parent: champ_repetition) ]
] end
end end
trait :without_champs do trait :without_champs do

View file

@ -11,16 +11,32 @@ shared_examples 'champ_spec' do
context 'when carte mandatory and blank' do context 'when carte mandatory and blank' do
let(:type_de_champ) { build(:type_de_champ_carte, mandatory: mandatory) } 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) } it { expect(champ.mandatory_and_blank?).to be(true) }
end end
context 'when multiple_drop_down_list mandatory and blank' do 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(: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) { '[]' } let(:value) { '[]' }
it { expect(champ.mandatory_and_blank?).to be(true) } it { expect(champ.mandatory_and_blank?).to be(true) }
end 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 context 'when not blank' do
let(:value) { 'yop' } let(:value) { 'yop' }
it { expect(champ.mandatory_and_blank?).to be(false) } it { expect(champ.mandatory_and_blank?).to be(false) }