From 462cc41caa425064d402cc08e2195805798418b5 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 29 Sep 2022 17:42:53 +0200 Subject: [PATCH] refactor(block): use block? instead of repetition? --- .../editable_champ/editable_champ_component.rb | 4 ++-- .../editable_champ_component.html.haml | 4 ++-- .../champ_component/champ_component.html.haml | 2 +- app/controllers/users/dossiers_controller.rb | 2 +- app/graphql/types/champ_descriptor_type.rb | 2 +- .../controllers/autosave_controller.ts | 2 +- app/models/champ.rb | 1 + app/models/dossier.rb | 2 +- app/models/dossier_preloader.rb | 2 +- app/models/procedure.rb | 4 ++-- app/models/type_de_champ.rb | 10 +++++++--- .../types_de_champ/no_empty_block_validator.rb | 18 ++++++++++++++++++ .../no_empty_repetition_validator.rb | 18 ------------------ 13 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 app/validators/types_de_champ/no_empty_block_validator.rb delete mode 100644 app/validators/types_de_champ/no_empty_repetition_validator.rb diff --git a/app/components/editable_champ/editable_champ_component.rb b/app/components/editable_champ/editable_champ_component.rb index f7d96fd3c..c9f8e6d41 100644 --- a/app/components/editable_champ/editable_champ_component.rb +++ b/app/components/editable_champ/editable_champ_component.rb @@ -20,12 +20,12 @@ class EditableChamp::EditableChampComponent < ApplicationComponent { class: "editable-champ-#{@champ.type_champ} #{'hidden' if !@champ.visible?}", id: @champ.input_group_id, - data: { controller: stimulus_controller } + data: { controller: stimulus_controller, block: @champ.block? } } end def stimulus_controller - if !@champ.repetition? && @champ.fillable? + if !@champ.block? && @champ.fillable? # This is an editable champ. Lets find what controllers it might need. controllers = [] diff --git a/app/components/editable_champ/editable_champ_component/editable_champ_component.html.haml b/app/components/editable_champ/editable_champ_component/editable_champ_component.html.haml index 733b8c6d5..f38fa4396 100644 --- a/app/components/editable_champ/editable_champ_component/editable_champ_component.html.haml +++ b/app/components/editable_champ/editable_champ_component/editable_champ_component.html.haml @@ -1,5 +1,5 @@ .editable-champ{ html_options } - - if @champ.repetition? + - if @champ.block? %h3.header-subsection= @champ.libelle - if @champ.description.present? %p.notice= string_to_html(@champ.description, false) @@ -9,5 +9,5 @@ - if @champ.type_champ == "titre_identite" %p.notice Carte nationale d’identité (uniquement le recto), passeport, titre de séjour ou autre justificatif d’identité. Formats acceptés : jpg/png - = @form.hidden_field :id, value: @champ.id, data: @champ.repetition? ? { id: true } : {} + = @form.hidden_field :id, value: @champ.id, data: @champ.block? ? { id: true } : {} = render component_class.new(form: @form, champ: @champ, seen_at: @seen_at) diff --git a/app/components/types_de_champ_editor/champ_component/champ_component.html.haml b/app/components/types_de_champ_editor/champ_component/champ_component.html.haml index 99c488d63..fd5305464 100644 --- a/app/components/types_de_champ_editor/champ_component/champ_component.html.haml +++ b/app/components/types_de_champ_editor/champ_component/champ_component.html.haml @@ -71,7 +71,7 @@ = form.label name, for: dom_id(type_de_champ, "layer_#{name}") do = form.check_box name, checked: checked, class: 'small-margin small', id: dom_id(type_de_champ, "layer_#{name}") = t(".layers.#{name}") - - if type_de_champ.repetition? + - if type_de_champ.block? .flex.justify-start.section.ml-1 .editor-block.flex-grow.cell = render TypesDeChampEditor::BlockComponent.new(block: coordinate, coordinates: coordinate.revision_types_de_champ) diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index d8b7392d2..d260b1deb 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -428,7 +428,7 @@ module Users @dossier.assign_attributes(champs_params[:dossier]) # FIXME: in some cases a removed repetition bloc row is submitted. # In this case it will be treated as a new record, and the action will fail. - @dossier.champs.filter(&:repetition?).each do |champ| + @dossier.champs.filter(&:block?).each do |champ| champ.champs = champ.champs.filter(&:persisted?) end if @dossier.champs.any?(&:changed_for_autosave?) diff --git a/app/graphql/types/champ_descriptor_type.rb b/app/graphql/types/champ_descriptor_type.rb index 9d01a8ae8..6aaff96b1 100644 --- a/app/graphql/types/champ_descriptor_type.rb +++ b/app/graphql/types/champ_descriptor_type.rb @@ -18,7 +18,7 @@ module Types field :options, [String], "List des options d’un champ avec selection.", null: true def champ_descriptors - if object.type_de_champ.repetition? + if object.type_de_champ.block? Loaders::Association.for(object.class, revision_types_de_champ: :type_de_champ).load(object) end end diff --git a/app/javascript/controllers/autosave_controller.ts b/app/javascript/controllers/autosave_controller.ts index daded6e18..ded2863da 100644 --- a/app/javascript/controllers/autosave_controller.ts +++ b/app/javascript/controllers/autosave_controller.ts @@ -200,7 +200,7 @@ export class AutosaveController extends ApplicationController { ) ].filter((element) => !element.disabled); - const parent = this.element.closest('.editable-champ-repetition'); + const parent = this.element.closest('[data-block]'); if (parent) { return [ ...inputs, diff --git a/app/models/champ.rb b/app/models/champ.rb index 41fd80a34..a6fb66b2b 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -45,6 +45,7 @@ class Champ < ApplicationRecord :exclude_from_export?, :exclude_from_view?, :repetition?, + :block?, :dossier_link?, :titre_identite?, :header_section?, diff --git a/app/models/dossier.rb b/app/models/dossier.rb index a610a9f82..2927b9ae7 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -984,7 +984,7 @@ class Dossier < ApplicationRecord end def check_mandatory_champs - (champs + champs.filter(&:repetition?).filter(&:visible?).flat_map(&:champs)) + (champs + champs.filter(&:block?).filter(&:visible?).flat_map(&:champs)) .filter(&:mandatory_blank_and_visible?) .map do |champ| "Le champ #{champ.libelle.truncate(200)} doit être rempli." diff --git a/app/models/dossier_preloader.rb b/app/models/dossier_preloader.rb index a3db99f91..e643db873 100644 --- a/app/models/dossier_preloader.rb +++ b/app/models/dossier_preloader.rb @@ -87,7 +87,7 @@ class DossierPreloader end # Load children champs - champs.filter(&:repetition?).each do |parent_champ| + champs.filter(&:block?).each do |parent_champ| champs = children_by_parent[parent_champ.id] || [] parent_champ.association(:dossier).target = dossier diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 747b9c810..c90c4b886 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -261,11 +261,11 @@ class Procedure < ApplicationRecord validates :administrateurs, presence: true validates :lien_site_web, presence: true, if: :publiee? validates :draft_types_de_champ, - 'types_de_champ/no_empty_repetition': true, + 'types_de_champ/no_empty_block': true, 'types_de_champ/no_empty_drop_down': true, if: :validate_for_publication? validates :draft_types_de_champ_private, - 'types_de_champ/no_empty_repetition': true, + 'types_de_champ/no_empty_block': true, 'types_de_champ/no_empty_drop_down': true, if: :validate_for_publication? validate :check_juridique diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 1e82d5607..641b2ee0d 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -132,7 +132,7 @@ class TypeDeChamp < ApplicationRecord before_validation :check_mandatory before_save :remove_piece_justificative_template, if: -> { type_champ_changed? } before_validation :remove_drop_down_list, if: -> { type_champ_changed? } - before_save :remove_repetition, if: -> { type_champ_changed? } + before_save :remove_block, if: -> { type_champ_changed? } after_save if: -> { @remove_piece_justificative_template } do piece_justificative_template.purge_later @@ -220,6 +220,10 @@ class TypeDeChamp < ApplicationRecord type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list) end + def block? + type_champ == TypeDeChamp.type_champs.fetch(:repetition) + end + def header_section? type_champ == TypeDeChamp.type_champs.fetch(:header_section) end @@ -405,8 +409,8 @@ class TypeDeChamp < ApplicationRecord end end - def remove_repetition - if !repetition? && procedure.present? + def remove_block + if !block? && procedure.present? procedure .draft_revision # action occurs only on draft .remove_children_of(self) diff --git a/app/validators/types_de_champ/no_empty_block_validator.rb b/app/validators/types_de_champ/no_empty_block_validator.rb new file mode 100644 index 000000000..e1ea17739 --- /dev/null +++ b/app/validators/types_de_champ/no_empty_block_validator.rb @@ -0,0 +1,18 @@ +class TypesDeChamp::NoEmptyBlockValidator < ActiveModel::EachValidator + def validate_each(procedure, attribute, types_de_champ) + types_de_champ.filter(&:block?).each do |repetition| + validate_block_not_empty(procedure, attribute, repetition) + end + end + + private + + def validate_block_not_empty(procedure, attribute, parent) + if procedure.draft_revision.children_of(parent).empty? + procedure.errors.add( + attribute, + procedure.errors.generate_message(attribute, :empty_repetition, { value: parent.libelle }) + ) + end + end +end diff --git a/app/validators/types_de_champ/no_empty_repetition_validator.rb b/app/validators/types_de_champ/no_empty_repetition_validator.rb deleted file mode 100644 index a2dc247c3..000000000 --- a/app/validators/types_de_champ/no_empty_repetition_validator.rb +++ /dev/null @@ -1,18 +0,0 @@ -class TypesDeChamp::NoEmptyRepetitionValidator < ActiveModel::EachValidator - def validate_each(procedure, attribute, types_de_champ) - types_de_champ.filter(&:repetition?).each do |repetition| - validate_repetition_not_empty(procedure, attribute, repetition) - end - end - - private - - def validate_repetition_not_empty(procedure, attribute, repetition) - if procedure.draft_revision.children_of(repetition).empty? - procedure.errors.add( - attribute, - procedure.errors.generate_message(attribute, :empty_repetition, { value: repetition.libelle }) - ) - end - end -end