diff --git a/app/components/dossiers/champs_rows_show_component.rb b/app/components/dossiers/champs_rows_show_component.rb index 5eba03d4a..b4d5f9e27 100644 --- a/app/components/dossiers/champs_rows_show_component.rb +++ b/app/components/dossiers/champs_rows_show_component.rb @@ -26,7 +26,7 @@ class Dossiers::ChampsRowsShowComponent < ApplicationComponent def blank_key(champ) key = ".blank_optional" - key += "_attachment" if champ.type_de_champ.piece_justificative? + key += "_attachment" if champ.type_de_champ.piece_justificative_or_titre_identite? key end diff --git a/app/components/editable_champ/editable_champ_component.rb b/app/components/editable_champ/editable_champ_component.rb index e3fa34628..20f0a7fd6 100644 --- a/app/components/editable_champ/editable_champ_component.rb +++ b/app/components/editable_champ/editable_champ_component.rb @@ -84,6 +84,6 @@ class EditableChamp::EditableChampComponent < ApplicationComponent end def autosave_enabled? - !@champ.carte? && !@champ.block? && @champ.fillable? + !@champ.carte? && !@champ.repetition? && @champ.fillable? end end diff --git a/app/components/types_de_champ_editor/champ_component.rb b/app/components/types_de_champ_editor/champ_component.rb index a0fa6a516..6ed8034e3 100644 --- a/app/components/types_de_champ_editor/champ_component.rb +++ b/app/components/types_de_champ_editor/champ_component.rb @@ -130,7 +130,7 @@ class TypesDeChampEditor::ChampComponent < ApplicationComponent end def has_legacy_number? - revision.types_de_champ.any?(&:legacy_number?) + revision.types_de_champ.any?(&:number?) end def options_for_character_limit 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 8bb488d4a..35b3c3203 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 @@ -68,7 +68,7 @@ .flex.justify-start.fr-mt-1w.flex-gap - - if type_de_champ.drop_down_list? + - if type_de_champ.any_drop_down_list? .flex.column.justify-start.width-33 .cell = form.label :drop_down_options_from_text, "Options de la liste", for: dom_id(type_de_champ, :drop_down_options_from_text) @@ -77,7 +77,7 @@ class: 'fr-input small-margin small width-100', rows: 7, id: dom_id(type_de_champ, :drop_down_options_from_text) - - if type_de_champ.simple_drop_down_list? + - if type_de_champ.drop_down_list? .cell = form.label :drop_down_other, for: dom_id(type_de_champ, :drop_down_other) do Proposer une option « autre » avec un texte libre @@ -91,7 +91,7 @@ .cell.fr-mt-1w = form.label :drop_down_secondary_description, "Description du champ secondaire (optionnel)", for: dom_id(type_de_champ, :drop_down_secondary_description) = form.text_area :drop_down_secondary_description, class: 'fr-input small-margin small width-100', rows: 3, id: dom_id(type_de_champ, :drop_down_secondary_description) - - if type_de_champ.piece_justificative? + - if type_de_champ.piece_justificative_or_titre_identite? .cell = form.label :piece_justificative_template, "Modèle", for: dom_id(type_de_champ, :piece_justificative_template) = render Attachment::EditComponent.new(**piece_justificative_template_options) @@ -120,7 +120,7 @@ Spécifier un nombre maximal conseillé de caractères : = form.select :character_limit, options_for_character_limit, {}, { id: dom_id(type_de_champ, :character_limit), class: 'fr-select' } - - if type_de_champ.block? + - if type_de_champ.repetition? .flex.justify-start.section.fr-ml-1w .editor-block.flex-grow.cell = render TypesDeChampEditor::BlockComponent.new(block: coordinate, coordinates: coordinate.revision_types_de_champ, upper_coordinates: @upper_coordinates) diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb index 3993b19d4..5409d7db2 100644 --- a/app/controllers/root_controller.rb +++ b/app/controllers/root_controller.rb @@ -62,7 +62,7 @@ class RootController < ApplicationController "option C" ] type_de_champ.save - elsif type_de_champ.drop_down_list? + elsif type_de_champ.any_drop_down_list? type_de_champ.drop_down_options = [ "option A", diff --git a/app/graphql/types/champ_descriptor_type.rb b/app/graphql/types/champ_descriptor_type.rb index 9e4a474a9..b19b91555 100644 --- a/app/graphql/types/champ_descriptor_type.rb +++ b/app/graphql/types/champ_descriptor_type.rb @@ -110,13 +110,13 @@ module Types end def champ_descriptors - if type_de_champ.block? + if type_de_champ.repetition? Loaders::Association.for(object.class, revision_types_de_champ: :type_de_champ).load(object) end end def options - if type_de_champ.drop_down_list? + if type_de_champ.any_drop_down_list? type_de_champ.drop_down_options.reject(&:empty?) end end diff --git a/app/helpers/champ_helper.rb b/app/helpers/champ_helper.rb index 449f9b799..c971be6f2 100644 --- a/app/helpers/champ_helper.rb +++ b/app/helpers/champ_helper.rb @@ -12,7 +12,7 @@ module ChampHelper def auto_attach_url(object, procedure_id: nil) if object.is_a?(Champ) champs_piece_justificative_url(object.dossier, object.stable_id, row_id: object.row_id) - elsif object.is_a?(TypeDeChamp) && object.piece_justificative? + elsif object.is_a?(TypeDeChamp) && object.piece_justificative_or_titre_identite? piece_justificative_template_admin_procedure_type_de_champ_url(stable_id: object.stable_id, procedure_id:) elsif object.is_a?(TypeDeChamp) && object.explication? notice_explicative_admin_procedure_type_de_champ_url(stable_id: object.stable_id, procedure_id:) diff --git a/app/models/champ.rb b/app/models/champ.rb index 8efa30748..2aec05e14 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -38,39 +38,21 @@ class Champ < ApplicationRecord :current_section_level, :exclude_from_export?, :exclude_from_view?, - :repetition?, - :block?, - :dossier_link?, - :departement?, - :region?, - :textarea?, - :piece_justificative?, - :titre_identite?, - :header_section?, - :checkbox?, - :simple_drop_down_list?, - :linked_drop_down_list?, :non_fillable?, :fillable?, - :cnaf?, - :dgfip?, - :pole_emploi?, - :mesri?, - :rna?, - :siret?, - :carte?, - :datetime?, :mandatory?, :prefillable?, :refresh_after_update?, :character_limit?, :character_limit, - :yes_no?, :expression_reguliere, :expression_reguliere_exemple_text, :expression_reguliere_error_message, to: :type_de_champ + delegate(*TypeDeChamp.type_champs.values.map { "#{_1}?".to_sym }, to: :type_de_champ) + delegate :piece_justificative_or_titre_identite?, :any_drop_down_list?, to: :type_de_champ + delegate :to_typed_id, :to_typed_id_for_query, to: :type_de_champ, prefix: true delegate :revision, to: :dossier, prefix: true diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 3b6127d3e..6ed3cce75 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -391,7 +391,7 @@ class ProcedureRevision < ApplicationRecord to_type_de_champ.condition&.to_s(to_coordinates.map(&:type_de_champ))) end - if to_type_de_champ.drop_down_list? + if to_type_de_champ.any_drop_down_list? if from_type_de_champ.drop_down_options != to_type_de_champ.drop_down_options changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ, :drop_down_options, @@ -425,7 +425,7 @@ class ProcedureRevision < ApplicationRecord from_type_de_champ.carte_optional_layers, to_type_de_champ.carte_optional_layers) end - elsif to_type_de_champ.piece_justificative? + elsif to_type_de_champ.piece_justificative_or_titre_identite? if from_type_de_champ.checksum_for_attachment(:piece_justificative_template) != to_type_de_champ.checksum_for_attachment(:piece_justificative_template) changes << ProcedureRevisionChange::UpdateChamp.new(from_type_de_champ, :piece_justificative_template, diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 62121d949..a35593258 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -66,7 +66,7 @@ class TypeDeChamp < ApplicationRecord expression_reguliere: STANDARD } - enum type_champs: { + enum type_champ: { engagement_juridique: 'engagement_juridique', header_section: 'header_section', @@ -323,118 +323,10 @@ class TypeDeChamp < ApplicationRecord ]) end - def drop_down_list? - type_champ.in?([ - TypeDeChamp.type_champs.fetch(:drop_down_list), - TypeDeChamp.type_champs.fetch(:multiple_drop_down_list), - TypeDeChamp.type_champs.fetch(:linked_drop_down_list) - ]) - end - - def simple_drop_down_list? - type_champ == TypeDeChamp.type_champs.fetch(:drop_down_list) - end - - def multiple_drop_down_list? - type_champ == TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) - end - - def linked_drop_down_list? - type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list) - end - - def yes_no? - type_champ == TypeDeChamp.type_champs.fetch(:yes_no) - end - - def block? - type_champ == TypeDeChamp.type_champs.fetch(:repetition) - end - - def header_section? - type_champ == TypeDeChamp.type_champs.fetch(:header_section) - end - def exclude_from_view? type_champ == TypeDeChamp.type_champs.fetch(:explication) end - def explication? - type_champ == TypeDeChamp.type_champs.fetch(:explication) - end - - def repetition? - type_champ == TypeDeChamp.type_champs.fetch(:repetition) - end - - def dossier_link? - type_champ == TypeDeChamp.type_champs.fetch(:dossier_link) - end - - def siret? - type_champ == TypeDeChamp.type_champs.fetch(:siret) - end - - def piece_justificative? - type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) || type_champ == TypeDeChamp.type_champs.fetch(:titre_identite) - end - - def legacy_number? - type_champ == TypeDeChamp.type_champs.fetch(:number) - end - - def textarea? - type_champ == TypeDeChamp.type_champs.fetch(:textarea) - end - - def titre_identite? - type_champ == TypeDeChamp.type_champs.fetch(:titre_identite) - end - - def carte? - type_champ == TypeDeChamp.type_champs.fetch(:carte) - end - - def cnaf? - type_champ == TypeDeChamp.type_champs.fetch(:cnaf) - end - - def rna? - type_champ == TypeDeChamp.type_champs.fetch(:rna) - end - - def dgfip? - type_champ == TypeDeChamp.type_champs.fetch(:dgfip) - end - - def pole_emploi? - type_champ == TypeDeChamp.type_champs.fetch(:pole_emploi) - end - - def departement? - type_champ == TypeDeChamp.type_champs.fetch(:departements) - end - - def region? - type_champ == TypeDeChamp.type_champs.fetch(:regions) - end - - def mesri? - type_champ == TypeDeChamp.type_champs.fetch(:mesri) - end - - def datetime? - type_champ == TypeDeChamp.type_champs.fetch(:datetime) - end - - def checkbox? - type_champ == TypeDeChamp.type_champs.fetch(:checkbox) - end - - def expression_reguliere? - type_champ == TypeDeChamp.type_champs.fetch(:expression_reguliere) - end - def public? !private? end @@ -546,11 +438,11 @@ class TypeDeChamp < ApplicationRecord end def options_for_select - if departement? + if departements? APIGeoService.departement_options - elsif region? + elsif regions? APIGeoService.region_options - elsif drop_down_list? + elsif any_drop_down_list? drop_down_options elsif yes_no? Champs::YesNoChamp.options @@ -757,6 +649,21 @@ class TypeDeChamp < ApplicationRecord CHAMP_TYPE_TO_TYPE_CHAMP = type_champs.values.map { [type_champ_to_champ_class_name(_1), _1] }.to_h + def piece_justificative_or_titre_identite? + type_champ.in?([ + TypeDeChamp.type_champs.fetch(:piece_justificative), + TypeDeChamp.type_champs.fetch(:titre_identite) + ]) + end + + def any_drop_down_list? + type_champ.in?([ + TypeDeChamp.type_champs.fetch(:drop_down_list), + TypeDeChamp.type_champs.fetch(:multiple_drop_down_list), + TypeDeChamp.type_champs.fetch(:linked_drop_down_list) + ]) + end + private def castable_on_change?(from_type, to_type) @@ -780,7 +687,7 @@ class TypeDeChamp < ApplicationRecord end def remove_attachment - if !piece_justificative? && piece_justificative_template.attached? + if !piece_justificative_or_titre_identite? && piece_justificative_template.attached? piece_justificative_template.purge_later elsif !explication? && notice_explicative.attached? notice_explicative.purge_later @@ -788,7 +695,7 @@ class TypeDeChamp < ApplicationRecord end def set_drop_down_list_options - if (simple_drop_down_list? || multiple_drop_down_list?) && drop_down_options.empty? + if (drop_down_list? || multiple_drop_down_list?) && drop_down_options.empty? self.drop_down_options = ['Fromage', 'Dessert'] elsif linked_drop_down_list? && drop_down_options.none?(/^--.*--$/) self.drop_down_options = ['--Fromage--', 'bleu de sassenage', 'picodon', '--Dessert--', 'éclair', 'tarte aux pommes'] diff --git a/app/validators/types_de_champ/no_empty_block_validator.rb b/app/validators/types_de_champ/no_empty_block_validator.rb index a41b647c5..6cd39a2e4 100644 --- a/app/validators/types_de_champ/no_empty_block_validator.rb +++ b/app/validators/types_de_champ/no_empty_block_validator.rb @@ -2,7 +2,7 @@ class TypesDeChamp::NoEmptyBlockValidator < ActiveModel::EachValidator def validate_each(procedure, attribute, types_de_champ) - types_de_champ.filter(&:block?).each do |repetition| + types_de_champ.filter(&:repetition?).each do |repetition| validate_block_not_empty(procedure, attribute, repetition) end end diff --git a/app/validators/types_de_champ/no_empty_drop_down_validator.rb b/app/validators/types_de_champ/no_empty_drop_down_validator.rb index 8dda5b772..d1028bce3 100644 --- a/app/validators/types_de_champ/no_empty_drop_down_validator.rb +++ b/app/validators/types_de_champ/no_empty_drop_down_validator.rb @@ -2,7 +2,7 @@ class TypesDeChamp::NoEmptyDropDownValidator < ActiveModel::EachValidator def validate_each(procedure, attribute, types_de_champ) - types_de_champ.filter(&:drop_down_list?).each do |drop_down| + types_de_champ.filter(&:any_drop_down_list?).each do |drop_down| validate_drop_down_not_empty(procedure, attribute, drop_down) end end diff --git a/spec/components/procedures/errors_summary_spec.rb b/spec/components/procedures/errors_summary_spec.rb index 204bea9df..4317998cc 100644 --- a/spec/components/procedures/errors_summary_spec.rb +++ b/spec/components/procedures/errors_summary_spec.rb @@ -60,7 +60,7 @@ describe Procedure::ErrorsSummary, type: :component do let(:validation_context) { :types_de_champ_public_editor } before do - drop_down_public = procedure.draft_revision.types_de_champ_public.find(&:drop_down_list?) + drop_down_public = procedure.draft_revision.types_de_champ_public.find(&:any_drop_down_list?) drop_down_public.update!(drop_down_options: []) subject end diff --git a/spec/controllers/api/v2/graphql_controller_spec.rb b/spec/controllers/api/v2/graphql_controller_spec.rb index aebf599a1..d0712c670 100644 --- a/spec/controllers/api/v2/graphql_controller_spec.rb +++ b/spec/controllers/api/v2/graphql_controller_spec.rb @@ -219,7 +219,7 @@ describe API::V2::GraphqlController do description: tdc.description, required: tdc.mandatory?, champDescriptors: tdc.repetition? ? procedure.active_revision.children_of(tdc.reload).map { { id: _1.to_typed_id, __typename: format_type_champ(_1.type_champ) } } : nil, - options: tdc.drop_down_list? ? tdc.drop_down_options.reject(&:empty?) : nil + options: tdc.any_drop_down_list? ? tdc.drop_down_options.reject(&:empty?) : nil }.compact end, dossiers: { diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index 89ad244ea..f2a6b799f 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -24,7 +24,7 @@ FactoryBot.define do after(:create) do |dossier, evaluator| if evaluator.populate_champs dossier.revision.types_de_champ_public.each do |type_de_champ| - value = if type_de_champ.simple_drop_down_list? + value = if type_de_champ.drop_down_list? type_de_champ.drop_down_options.first elsif type_de_champ.multiple_drop_down_list? type_de_champ.drop_down_options.first(2).to_json @@ -36,7 +36,7 @@ FactoryBot.define do if evaluator.populate_annotations dossier.revision.types_de_champ_private.each do |type_de_champ| - value = if type_de_champ.simple_drop_down_list? + value = if type_de_champ.drop_down_list? type_de_champ.drop_down_options.first elsif type_de_champ.multiple_drop_down_list? type_de_champ.drop_down_options.first(2).to_json diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 9062e9e4f..320fe595e 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -520,7 +520,7 @@ describe Dossier, type: :model do context 'when titre identite' do let(:types_de_champ_public) { [{ type: :titre_identite }] } - let(:champ) { dossier.project_champs_public.find(&:piece_justificative?) } + let(:champ) { dossier.project_champs_public.find(&:titre_identite?) } context 'when not visible' do let(:visible) { false } diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 25784929d..b8724a106 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -407,7 +407,7 @@ describe Procedure do end it 'validates that no drop-down type de champ is empty' do - drop_down = procedure.draft_revision.types_de_champ_public.find(&:drop_down_list?) + drop_down = procedure.draft_revision.types_de_champ_public.find(&:any_drop_down_list?) drop_down.update!(drop_down_options: []) procedure.reload.validate(:publication) @@ -440,7 +440,7 @@ describe Procedure do end it 'validates that no drop-down type de champ is empty' do - drop_down = procedure.draft_revision.types_de_champ_private.find(&:drop_down_list?) + drop_down = procedure.draft_revision.types_de_champ_private.find(&:any_drop_down_list?) drop_down.update!(drop_down_options: []) procedure.reload.validate(:publication) diff --git a/spec/system/administrateurs/procedure_publish_spec.rb b/spec/system/administrateurs/procedure_publish_spec.rb index aac700f8d..6912b9f70 100644 --- a/spec/system/administrateurs/procedure_publish_spec.rb +++ b/spec/system/administrateurs/procedure_publish_spec.rb @@ -71,9 +71,9 @@ describe 'Publishing a procedure', js: true do end before do - drop_down = procedure.draft_revision.types_de_champ_public.find(&:drop_down_list?) + drop_down = procedure.draft_revision.types_de_champ_public.find(&:any_drop_down_list?) drop_down.update!(drop_down_options: []) - drop_down = procedure.draft_revision.types_de_champ_private.find(&:drop_down_list?) + drop_down = procedure.draft_revision.types_de_champ_private.find(&:any_drop_down_list?) drop_down.update!(drop_down_options: []) end diff --git a/spec/system/instructeurs/procedure_filters_spec.rb b/spec/system/instructeurs/procedure_filters_spec.rb index 1cd23ea13..2e3f54a0e 100644 --- a/spec/system/instructeurs/procedure_filters_spec.rb +++ b/spec/system/instructeurs/procedure_filters_spec.rb @@ -122,7 +122,7 @@ describe "procedure filters" do describe 'departements' do let(:types_de_champ_public) { [{ type: :departements }] } scenario "should be able to find by departements with custom enum lookup", js: true do - departement_champ = new_unfollow_dossier.champs.find(&:departement?) + departement_champ = new_unfollow_dossier.champs.find(&:departements?) departement_champ.update!(value: 'Oise', external_id: '60') departement_champ.reload champ_select_value = "#{departement_champ.external_id} – #{departement_champ.value}" @@ -162,7 +162,7 @@ describe "procedure filters" do describe 'region' do let(:types_de_champ_public) { [{ type: :regions }] } scenario "should be able to find by region with custom enum lookup", js: true do - region_champ = new_unfollow_dossier.champs.find(&:region?) + region_champ = new_unfollow_dossier.champs.find(&:regions?) region_champ.update!(value: 'Bretagne', external_id: '53') region_champ.reload