From cfb03fc747ac579635fc8c0babaa875edb1a7575 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 19 Sep 2024 11:40:42 +0200 Subject: [PATCH] remove enabled_non_empty_options indirection --- .../editable_champ/drop_down_list_component.rb | 4 ++-- .../drop_down_list_component.html.haml | 4 ++-- .../multiple_drop_down_list_component.rb | 2 +- .../multiple_drop_down_list_component.html.haml | 2 +- app/models/champs/drop_down_list_champ.rb | 12 ++++-------- app/models/champs/multiple_drop_down_list_champ.rb | 12 ++++-------- 6 files changed, 14 insertions(+), 22 deletions(-) diff --git a/app/components/editable_champ/drop_down_list_component.rb b/app/components/editable_champ/drop_down_list_component.rb index 3b3be5a1d..d6397b31c 100644 --- a/app/components/editable_champ/drop_down_list_component.rb +++ b/app/components/editable_champ/drop_down_list_component.rb @@ -23,7 +23,7 @@ class EditableChamp::DropDownListComponent < EditableChamp::EditableChampBaseCom def contains_long_option? max_length = 100 - @champ.enabled_non_empty_options.any? { _1.size > max_length } + @champ.drop_down_list_enabled_non_empty_options.any? { _1.size > max_length } end def react_props @@ -31,7 +31,7 @@ class EditableChamp::DropDownListComponent < EditableChamp::EditableChampBaseCom class: 'fr-mt-1w', name: @form.field_name(:value), selected_key: @champ.selected, - items: @champ.enabled_non_empty_options(other: true).map { _1.is_a?(Array) ? _1 : [_1, _1] }, + items: @champ.drop_down_list_enabled_non_empty_options(other: true).map { _1.is_a?(Array) ? _1 : [_1, _1] }, empty_filter_key: @champ.drop_down_other? ? Champs::DropDownListChamp::OTHER : nil, 'aria-describedby': @champ.describedby_id, 'aria-labelledby': @champ.labelledby_id) diff --git a/app/components/editable_champ/drop_down_list_component/drop_down_list_component.html.haml b/app/components/editable_champ/drop_down_list_component/drop_down_list_component.html.haml index 3ce73c3ee..607250d8a 100644 --- a/app/components/editable_champ/drop_down_list_component/drop_down_list_component.html.haml +++ b/app/components/editable_champ/drop_down_list_component/drop_down_list_component.html.haml @@ -1,6 +1,6 @@ - if @champ.render_as_radios? .fr-fieldset__content - - @champ.enabled_non_empty_options.each_with_index do |option, index| + - @champ.drop_down_list_enabled_non_empty_options.each_with_index do |option, index| .fr-radio-group = @form.radio_button :value, option, id: dom_id(@champ, "radio_option_#{index}") %label.fr-label{ for: dom_id(@champ, "radio_option_#{index}") } @@ -22,7 +22,7 @@ = render ReactComponent.new "ComboBox/SingleComboBox", **react_props - else = @form.select :value, - @champ.enabled_non_empty_options(other: true), + @champ.drop_down_list_enabled_non_empty_options(other: true), { selected: @champ.selected, include_blank: true }, required: @champ.required?, id: @champ.input_id, diff --git a/app/components/editable_champ/multiple_drop_down_list_component.rb b/app/components/editable_champ/multiple_drop_down_list_component.rb index 0269ca782..737e8720b 100644 --- a/app/components/editable_champ/multiple_drop_down_list_component.rb +++ b/app/components/editable_champ/multiple_drop_down_list_component.rb @@ -16,7 +16,7 @@ class EditableChamp::MultipleDropDownListComponent < EditableChamp::EditableCham class: 'fr-mt-1w', name: @form.field_name(:value, multiple: true), selected_keys: @champ.selected_options, - items: @champ.enabled_non_empty_options, + items: @champ.drop_down_list_enabled_non_empty_options, value_separator: false, 'aria-label': @champ.libelle, 'aria-describedby': @champ.describedby_id, diff --git a/app/components/editable_champ/multiple_drop_down_list_component/multiple_drop_down_list_component.html.haml b/app/components/editable_champ/multiple_drop_down_list_component/multiple_drop_down_list_component.html.haml index cb5954738..ca15f04f0 100644 --- a/app/components/editable_champ/multiple_drop_down_list_component/multiple_drop_down_list_component.html.haml +++ b/app/components/editable_champ/multiple_drop_down_list_component/multiple_drop_down_list_component.html.haml @@ -1,5 +1,5 @@ - if @champ.render_as_checkboxes? - = @form.collection_check_boxes :value, @champ.enabled_non_empty_options, :to_s, :to_s do |b| + = @form.collection_check_boxes :value, @champ.drop_down_list_enabled_non_empty_options, :to_s, :to_s do |b| - capture do .fr-fieldset__element .fr-checkbox-group diff --git a/app/models/champs/drop_down_list_champ.rb b/app/models/champs/drop_down_list_champ.rb index 122151adb..0e26e7c97 100644 --- a/app/models/champs/drop_down_list_champ.rb +++ b/app/models/champs/drop_down_list_champ.rb @@ -9,11 +9,11 @@ class Champs::DropDownListChamp < Champ validate :value_is_in_options, if: -> { !(value.blank? || drop_down_other?) && validate_champ_value_or_prefill? } def render_as_radios? - enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_RADIO + drop_down_list_enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_RADIO end def render_as_combobox? - enabled_non_empty_options.size >= THRESHOLD_NB_OPTIONS_AS_AUTOCOMPLETE + drop_down_list_enabled_non_empty_options.size >= THRESHOLD_NB_OPTIONS_AS_AUTOCOMPLETE end def html_label? @@ -28,12 +28,8 @@ class Champs::DropDownListChamp < Champ other? ? OTHER : value end - def enabled_non_empty_options(other: false) - drop_down_list_enabled_non_empty_options(other:) - end - def other? - drop_down_other? && (other || (value.present? && enabled_non_empty_options.exclude?(value))) + drop_down_other? && (other || (value.present? && drop_down_list_enabled_non_empty_options.exclude?(value))) end def value=(value) @@ -71,7 +67,7 @@ class Champs::DropDownListChamp < Champ private def value_is_in_options - return if enabled_non_empty_options.include?(value) + return if drop_down_list_enabled_non_empty_options.include?(value) errors.add(:value, :not_in_options) end diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index 081c6547f..fc1e34799 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -3,10 +3,6 @@ class Champs::MultipleDropDownListChamp < Champ validate :values_are_in_options, if: -> { value.present? && validate_champ_value_or_prefill? } - def enabled_non_empty_options - drop_down_list_enabled_non_empty_options - end - THRESHOLD_NB_OPTIONS_AS_CHECKBOX = 5 def search_terms @@ -18,7 +14,7 @@ class Champs::MultipleDropDownListChamp < Champ end def render_as_checkboxes? - enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX + drop_down_list_enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX end def html_label? @@ -51,7 +47,7 @@ class Champs::MultipleDropDownListChamp < Champ end def focusable_input_id - render_as_checkboxes? ? checkbox_id(enabled_non_empty_options.first) : input_id + render_as_checkboxes? ? checkbox_id(drop_down_list_enabled_non_empty_options.first) : input_id end def checkbox_id(value) @@ -67,7 +63,7 @@ class Champs::MultipleDropDownListChamp < Champ end def unselected_options - enabled_non_empty_options - selected_options + drop_down_list_enabled_non_empty_options - selected_options end def value=(value) @@ -97,7 +93,7 @@ class Champs::MultipleDropDownListChamp < Champ def values_are_in_options json = selected_options.compact_blank return if json.empty? - return if (json - enabled_non_empty_options).empty? + return if (json - drop_down_list_enabled_non_empty_options).empty? errors.add(:value, :not_in_options) end