diff --git a/app/models/champs/drop_down_list_champ.rb b/app/models/champs/drop_down_list_champ.rb index c32ff39fc..e7f684efc 100644 --- a/app/models/champs/drop_down_list_champ.rb +++ b/app/models/champs/drop_down_list_champ.rb @@ -1,3 +1,7 @@ class Champs::DropDownListChamp < Champ THRESHOLD_NB_OPTIONS_AS_RADIO = 5 + + def render_as_radios? + drop_down_list.enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_RADIO + end end diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index 738db3a07..5babf5c9a 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -23,6 +23,10 @@ class Champs::MultipleDropDownListChamp < Champ value.present? ? selected_options.join(', ') : nil end + def render_as_checkboxes? + drop_down_list.enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX + end + private def format_before_save diff --git a/app/views/shared/dossiers/editable_champs/_drop_down_list.html.haml b/app/views/shared/dossiers/editable_champs/_drop_down_list.html.haml index 78b2df222..9aebdc575 100644 --- a/app/views/shared/dossiers/editable_champs/_drop_down_list.html.haml +++ b/app/views/shared/dossiers/editable_champs/_drop_down_list.html.haml @@ -1,15 +1,14 @@ - if champ.drop_down_list && champ.drop_down_list.options.any? - - enabled_non_empty_options = champ.drop_down_list.enabled_non_empty_options - - if enabled_non_empty_options.size > Champs::DropDownListChamp::THRESHOLD_NB_OPTIONS_AS_RADIO + - if champ.render_as_radios? + %fieldset.radios + %legend.mandatory-explanation + Sélectionnez une des valeurs + - champ.drop_down_list.enabled_non_empty_options.each do |option| + %label + = form.radio_button :value, option + = option + - else = form.select :value, champ.drop_down_list.options, disabled: champ.drop_down_list.disabled_options, required: champ.mandatory? - - else - %fieldset.radios - %legend.mandatory-explanation - Sélectionnez une des valeurs - - enabled_non_empty_options.each do |option| - %label - = form.radio_button :value, option - = option diff --git a/app/views/shared/dossiers/editable_champs/_multiple_drop_down_list.html.haml b/app/views/shared/dossiers/editable_champs/_multiple_drop_down_list.html.haml index 978ea8e70..7d85f0805 100644 --- a/app/views/shared/dossiers/editable_champs/_multiple_drop_down_list.html.haml +++ b/app/views/shared/dossiers/editable_champs/_multiple_drop_down_list.html.haml @@ -1,15 +1,15 @@ - if champ.drop_down_list && champ.drop_down_list.options.any? - - enabled_non_empty_options = champ.drop_down_list.enabled_non_empty_options - - if enabled_non_empty_options.size > Champs::MultipleDropDownListChamp::THRESHOLD_NB_OPTIONS_AS_CHECKBOX + - if champ.render_as_checkboxes? + - champ.drop_down_list.enabled_non_empty_options.each do |option| + .editable-champ.editable-champ-checkbox + %label + = form.check_box :value, { multiple: true, checked: champ&.value&.include?(option) }, option, nil + = option + - else = form.select :value, champ.drop_down_list.options, { selected: champ.selected_options, disabled: champ.drop_down_list.disabled_options }, multiple: true, class: 'select2' - - else - - enabled_non_empty_options.each do |option| - .editable-champ.editable-champ-checkbox - %label - = form.check_box :value, { multiple: true, checked: champ&.value&.include?(option) }, option, nil - = option +