Merge pull request #7353 from betagouv/remove_tdcs_on_validator
Refactor(revision) : utilise children_of sur les validateurs de répétition
This commit is contained in:
commit
edfd9f79dc
2 changed files with 19 additions and 6 deletions
|
@ -8,7 +8,7 @@ class TypesDeChamp::NoEmptyRepetitionValidator < ActiveModel::EachValidator
|
|||
private
|
||||
|
||||
def validate_repetition_not_empty(procedure, attribute, repetition)
|
||||
if repetition.types_de_champ.blank?
|
||||
if procedure.draft_revision.children_of(repetition).empty?
|
||||
procedure.errors.add(
|
||||
attribute,
|
||||
procedure.errors.generate_message(attribute, :empty_repetition, { value: repetition.libelle })
|
||||
|
|
|
@ -297,14 +297,22 @@ describe Procedure do
|
|||
end
|
||||
|
||||
describe 'draft_types_de_champ validations' do
|
||||
let(:repetition) { build(:type_de_champ_repetition, libelle: 'Enfants') }
|
||||
let(:repetition) { repetition = procedure.types_de_champ.find(&:repetition?) }
|
||||
let(:text_field) { build(:type_de_champ_text) }
|
||||
let(:invalid_repetition_error_message) { 'Le champ « Enfants » doit comporter au moins un champ répétable' }
|
||||
|
||||
let(:drop_down) { build(:type_de_champ_drop_down_list, :without_selectable_values, libelle: 'Civilité') }
|
||||
let(:invalid_drop_down_error_message) { 'Le champ « Civilité » doit comporter au moins un choix sélectionnable' }
|
||||
|
||||
let(:procedure) { create(:procedure, types_de_champ: [repetition, drop_down]) }
|
||||
let(:procedure) { create(:procedure, :with_repetition) }
|
||||
let(:draft) { procedure.draft_revision }
|
||||
|
||||
before do
|
||||
draft.revision_types_de_champ.create(type_de_champ: drop_down, position: 100)
|
||||
|
||||
repetition.update(libelle: 'Enfants')
|
||||
draft.children_of(repetition).destroy_all
|
||||
end
|
||||
|
||||
context 'on a draft procedure' do
|
||||
it 'doesn’t validate the types de champs' do
|
||||
|
@ -320,8 +328,10 @@ describe Procedure do
|
|||
procedure.validate
|
||||
expect(procedure.errors.full_messages_for(:draft_types_de_champ)).to include(invalid_repetition_error_message)
|
||||
|
||||
text_field.order_place = repetition.types_de_champ.size
|
||||
procedure.draft_types_de_champ.find(&:repetition?).types_de_champ << text_field
|
||||
new_draft = procedure.draft_revision
|
||||
|
||||
parent_coordinate = new_draft.revision_types_de_champ.find_by(type_de_champ: repetition)
|
||||
new_draft.revision_types_de_champ.create(type_de_champ: create(:type_de_champ), position: 0, parent: parent_coordinate)
|
||||
|
||||
procedure.validate
|
||||
expect(procedure.errors.full_messages_for(:draft_types_de_champ)).not_to include(invalid_repetition_error_message)
|
||||
|
@ -350,7 +360,10 @@ describe Procedure do
|
|||
end
|
||||
|
||||
context 'when the champ is private' do
|
||||
let(:procedure) { create(:procedure, types_de_champ_private: [repetition, drop_down]) }
|
||||
before do
|
||||
repetition.update(private: true)
|
||||
drop_down.update(private: true)
|
||||
end
|
||||
|
||||
let(:invalid_repetition_error_message) { 'L’annotation privée « Enfants » doit comporter au moins un champ répétable' }
|
||||
let(:invalid_drop_down_error_message) { 'L’annotation privée « Civilité » doit comporter au moins un choix sélectionnable' }
|
||||
|
|
Loading…
Reference in a new issue