diff --git a/app/models/champ.rb b/app/models/champ.rb index 1b01faa97b6..69b13de8725 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -57,6 +57,7 @@ class Champ < ApplicationRecord :refresh_after_update?, :character_limit?, :character_limit, + :yes_no?, to: :type_de_champ delegate :to_typed_id, :to_typed_id_for_query, to: :type_de_champ, prefix: true diff --git a/app/models/champs/checkbox_champ.rb b/app/models/champs/checkbox_champ.rb index 3b0819fc0da..577588c5492 100644 --- a/app/models/champs/checkbox_champ.rb +++ b/app/models/champs/checkbox_champ.rb @@ -11,6 +11,10 @@ def legend_label? false end + def self.options + [[I18n.t('activerecord.attributes.type_de_champ.type_champs.checkbox_true'), true], [I18n.t('activerecord.attributes.type_de_champ.type_champs.checkbox_false'), false]] + end + # TODO remove when normalize_checkbox_values is over def true? value_with_legacy == TRUE_VALUE diff --git a/app/models/champs/yes_no_champ.rb b/app/models/champs/yes_no_champ.rb index 3cdf6a1b093..8f976381165 100644 --- a/app/models/champs/yes_no_champ.rb +++ b/app/models/champs/yes_no_champ.rb @@ -18,4 +18,8 @@ def no_input_id def focusable_input_id yes_input_id end + + def self.options + [[I18n.t('activerecord.attributes.type_de_champ.type_champs.yes_no_true'), true], [I18n.t('activerecord.attributes.type_de_champ.type_champs.yes_no_false'), false]] + end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 7bb8a0cb94f..951569c4363 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -296,6 +296,20 @@ def exclude_from_export? ]) end + def choice_type? + type_champ.in?([ + TypeDeChamp.type_champs.fetch(:checkbox), + TypeDeChamp.type_champs.fetch(:drop_down_list), + TypeDeChamp.type_champs.fetch(:multiple_drop_down_list), + TypeDeChamp.type_champs.fetch(:yes_no) + ]) + end + + def self.is_choice_type_from(type_champ) + return false if type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list) # To remove when we stop using linked_drop_down_list + TYPE_DE_CHAMP_TO_CATEGORIE[type_champ.to_sym] == CHOICE || type_champ.in?([TypeDeChamp.type_champs.fetch(:departements), TypeDeChamp.type_champs.fetch(:regions)]) + end + def drop_down_list? type_champ.in?([ TypeDeChamp.type_champs.fetch(:drop_down_list), @@ -312,6 +326,10 @@ 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 @@ -462,7 +480,7 @@ def current_section_level(revision) end def self.filter_hash_type(type_champ) - if type_champ.in?([TypeDeChamp.type_champs.fetch(:departements), TypeDeChamp.type_champs.fetch(:regions)]) + if is_choice_type_from(type_champ) :enum else :text @@ -482,6 +500,14 @@ def options_for_select APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } elsif region? APIGeoService.regions.map { [_1[:name], _1[:code]] } + elsif choice_type? + if drop_down_list? + drop_down_list_enabled_non_empty_options + elsif yes_no? + Champs::YesNoChamp.options + elsif checkbox? + Champs::CheckboxChamp.options + end end end diff --git a/app/models/types_de_champ/checkbox_type_de_champ.rb b/app/models/types_de_champ/checkbox_type_de_champ.rb index 717cb92cf46..aa60db19dc5 100644 --- a/app/models/types_de_champ/checkbox_type_de_champ.rb +++ b/app/models/types_de_champ/checkbox_type_de_champ.rb @@ -1,2 +1,11 @@ class TypesDeChamp::CheckboxTypeDeChamp < TypesDeChamp::TypeDeChampBase + def filter_to_human(filter_value) + if filter_value == "true" + I18n.t('activerecord.attributes.type_de_champ.type_champs.checkbox_true') + elsif filter_value == "false" + I18n.t('activerecord.attributes.type_de_champ.type_champs.checkbox_false') + else + filter_value + end + end end diff --git a/app/models/types_de_champ/yes_no_type_de_champ.rb b/app/models/types_de_champ/yes_no_type_de_champ.rb index 3c17d27ff72..25dae97bcf5 100644 --- a/app/models/types_de_champ/yes_no_type_de_champ.rb +++ b/app/models/types_de_champ/yes_no_type_de_champ.rb @@ -1,9 +1,9 @@ class TypesDeChamp::YesNoTypeDeChamp < TypesDeChamp::CheckboxTypeDeChamp def filter_to_human(filter_value) if filter_value == "true" - "oui" + I18n.t('activerecord.attributes.type_de_champ.type_champs.yes_no_true') elsif filter_value == "false" - "non" + I18n.t('activerecord.attributes.type_de_champ.type_champs.yes_no_false') else filter_value end diff --git a/config/locales/models/type_de_champ/en.yml b/config/locales/models/type_de_champ/en.yml index 0429c3f656d..baa3f77c793 100644 --- a/config/locales/models/type_de_champ/en.yml +++ b/config/locales/models/type_de_champ/en.yml @@ -38,10 +38,14 @@ en: piece_justificative: 'Supporting document' titre_identite: 'Identity title' checkbox: 'Checkbox' + checkbox_true: 'checked' + checkbox_false: 'not checked' drop_down_list: 'Dropdown list' multiple_drop_down_list: 'Multiple dropdown list' linked_drop_down_list: 'Linked dropdown list' yes_no: 'Yes/No' + yes_no_true: 'yes' + yes_no_false: 'no' annuaire_education: 'Schooling directory' rna: 'RNA' carte: 'Card' @@ -56,5 +60,3 @@ en: attributes: header_section_level: gap_error: "An header section with %{level} is missing." - - diff --git a/config/locales/models/type_de_champ/fr.yml b/config/locales/models/type_de_champ/fr.yml index ff0d3ab06e6..810bb873632 100644 --- a/config/locales/models/type_de_champ/fr.yml +++ b/config/locales/models/type_de_champ/fr.yml @@ -38,10 +38,14 @@ fr: piece_justificative: 'Pièce justificative' titre_identite: 'Titre identité' checkbox: 'Case à cocher seule' + checkbox_true: 'coché' + checkbox_false: 'non coché' drop_down_list: 'Choix simple' multiple_drop_down_list: 'Choix multiple' linked_drop_down_list: 'Deux menus déroulants liés' yes_no: 'Oui/Non' + yes_no_true: 'oui' + yes_no_false: 'non' annuaire_education: 'Annuaire de l’éducation' rna: 'RNA' carte: 'Carte' diff --git a/spec/system/instructeurs/procedure_filters_spec.rb b/spec/system/instructeurs/procedure_filters_spec.rb index f4b0376605a..4e75963244d 100644 --- a/spec/system/instructeurs/procedure_filters_spec.rb +++ b/spec/system/instructeurs/procedure_filters_spec.rb @@ -1,6 +1,6 @@ describe "procedure filters" do let(:instructeur) { create(:instructeur) } - let(:procedure) { create(:procedure, :published, :with_type_de_champ, :with_departement, :with_region, instructeurs: [instructeur]) } + let(:procedure) { create(:procedure, :published, :with_type_de_champ, :with_departement, :with_region, :with_drop_down_list, instructeurs: [instructeur]) } let!(:type_de_champ) { procedure.active_revision.types_de_champ_public.first } let!(:new_unfollow_dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:en_instruction)) } let!(:champ) { Champ.find_by(type_de_champ_id: type_de_champ.id, dossier_id: new_unfollow_dossier.id) } @@ -95,12 +95,20 @@ click_button "Ajouter le filtre" expect(page).to have_no_css("select#field", visible: true) - # use enum filter + # use statut dropdown filter click_on 'Sélectionner un filtre' select "Statut", from: "Colonne" find("select#value", visible: false) select 'En construction', from: "Valeur" click_button "Ajouter le filtre" + expect(page).to have_no_css("select#field", visible: true) + + # use choice dropdown filter + click_on 'Sélectionner un filtre' + select "Choix unique", from: "Colonne" + find("select#value", visible: false) + select 'val1', from: "Valeur" + click_button "Ajouter le filtre" end describe 'with a vcr cached cassette' do