display small selects as radio buttons

This commit is contained in:
clemkeirua 2020-06-15 16:29:22 +02:00
parent debbb21440
commit 22fc4c4195
3 changed files with 19 additions and 4 deletions

View file

@ -1,2 +1,3 @@
class Champs::DropDownListChamp < Champ
THRESHOLD_NB_OPTIONS_AS_RADIO = 3
end

View file

@ -8,6 +8,10 @@ class DropDownList < ApplicationRecord
result.blank? ? [] : [''] + result
end
def enabled_non_empty_options
(options - disabled_options).reject(&:empty?)
end
def disabled_options
options.filter { |v| (v =~ /^--.*--$/).present? }
end

View file

@ -1,5 +1,15 @@
- if champ.drop_down_list && champ.drop_down_list.options.any?
= form.select :value,
champ.drop_down_list.options,
disabled: champ.drop_down_list.disabled_options,
required: champ.mandatory?
- 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
= 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