extract rendering condition

This commit is contained in:
clemkeirua 2020-06-24 12:18:46 +02:00
parent 465449e684
commit 6b3631dbfe
4 changed files with 25 additions and 18 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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