From 22fc4c4195b771d7df89e95973bba956333836e7 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Mon, 15 Jun 2020 16:29:22 +0200 Subject: [PATCH 1/5] display small selects as radio buttons --- app/models/champs/drop_down_list_champ.rb | 1 + app/models/drop_down_list.rb | 4 ++++ .../editable_champs/_drop_down_list.html.haml | 18 ++++++++++++++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/champs/drop_down_list_champ.rb b/app/models/champs/drop_down_list_champ.rb index df3d14dfc..3fdbe8120 100644 --- a/app/models/champs/drop_down_list_champ.rb +++ b/app/models/champs/drop_down_list_champ.rb @@ -1,2 +1,3 @@ class Champs::DropDownListChamp < Champ + THRESHOLD_NB_OPTIONS_AS_RADIO = 3 end diff --git a/app/models/drop_down_list.rb b/app/models/drop_down_list.rb index 993bf0e04..7cb73ec81 100644 --- a/app/models/drop_down_list.rb +++ b/app/models/drop_down_list.rb @@ -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 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 05b938cfc..78b2df222 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,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 From 342129c8939fec2dde3af98eebe8bfb563f3f762 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 17 Jun 2020 11:14:27 +0200 Subject: [PATCH 2/5] display small multiple selects as checkboxes --- .../champs/multiple_drop_down_list_champ.rb | 2 ++ .../_multiple_drop_down_list.html.haml | 20 +++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index ff120ac23..efd695631 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -1,6 +1,8 @@ class Champs::MultipleDropDownListChamp < Champ before_save :format_before_save + THRESHOLD_NB_OPTIONS_AS_RADIO = 3 + def search_terms selected_options end 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 efbb7dda2..56000873d 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,7 +1,15 @@ - if champ.drop_down_list && champ.drop_down_list.options.any? - = form.select :value, - champ.drop_down_list.options, - { selected: champ.selected_options, - disabled: champ.drop_down_list.disabled_options }, - multiple: true, - class: 'select2' + - enabled_non_empty_options = champ.drop_down_list.enabled_non_empty_options + - if enabled_non_empty_options.size > Champs::MultipleDropDownListChamp::THRESHOLD_NB_OPTIONS_AS_RADIO + = 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 From 5c39f22417f6c6bded08ef81aeb98625e9593b7d Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 24 Jun 2020 09:33:55 +0200 Subject: [PATCH 3/5] =?UTF-8?q?seuil=20d'affichages=20des=20checbox/radio?= =?UTF-8?q?=20=C3=A0=205=20elements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/champs/drop_down_list_champ.rb | 2 +- app/models/champs/multiple_drop_down_list_champ.rb | 2 +- .../dossiers/editable_champs/_multiple_drop_down_list.html.haml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/champs/drop_down_list_champ.rb b/app/models/champs/drop_down_list_champ.rb index 3fdbe8120..c32ff39fc 100644 --- a/app/models/champs/drop_down_list_champ.rb +++ b/app/models/champs/drop_down_list_champ.rb @@ -1,3 +1,3 @@ class Champs::DropDownListChamp < Champ - THRESHOLD_NB_OPTIONS_AS_RADIO = 3 + THRESHOLD_NB_OPTIONS_AS_RADIO = 5 end diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index efd695631..738db3a07 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -1,7 +1,7 @@ class Champs::MultipleDropDownListChamp < Champ before_save :format_before_save - THRESHOLD_NB_OPTIONS_AS_RADIO = 3 + THRESHOLD_NB_OPTIONS_AS_CHECKBOX = 5 def search_terms selected_options 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 56000873d..978ea8e70 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,6 +1,6 @@ - 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_RADIO + - if enabled_non_empty_options.size > Champs::MultipleDropDownListChamp::THRESHOLD_NB_OPTIONS_AS_CHECKBOX = form.select :value, champ.drop_down_list.options, { selected: champ.selected_options, From 465449e6841f9f70dbd03c694066718d34a9763e Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 24 Jun 2020 10:55:53 +0200 Subject: [PATCH 4/5] fix unit tests --- spec/factories/drop_down_list.rb | 4 ++++ spec/factories/procedure.rb | 2 ++ spec/factories/type_de_champ.rb | 6 ++++++ spec/features/users/brouillon_spec.rb | 18 +++++++++++++----- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/spec/factories/drop_down_list.rb b/spec/factories/drop_down_list.rb index 5cc47a7fa..c2763e3c4 100644 --- a/spec/factories/drop_down_list.rb +++ b/spec/factories/drop_down_list.rb @@ -1,5 +1,9 @@ FactoryBot.define do factory :drop_down_list do value { "val1\r\nval2\r\n--separateur--\r\nval3" } + + trait :long do + value { "alpha\r\nbravo\r\n--separateur--\r\ncharly\r\ndelta\r\necho\r\nfox-trot\r\ngolf" } + end end end diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 7fcc26f39..e111638d0 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -233,6 +233,8 @@ FactoryBot.define do end build(:"type_de_champ_#{type_champ}", mandatory: true, libelle: libelle, order_place: index) end + procedure.types_de_champ << build(:type_de_champ_drop_down_list, :long, mandatory: true, libelle: 'simple_choice_drop_down_list_long') + procedure.types_de_champ << build(:type_de_champ_multiple_drop_down_list, :long, mandatory: true, libelle: 'multiple_choice_drop_down_list_long') end end diff --git a/spec/factories/type_de_champ.rb b/spec/factories/type_de_champ.rb index 597d84c9e..ee8b84721 100644 --- a/spec/factories/type_de_champ.rb +++ b/spec/factories/type_de_champ.rb @@ -53,10 +53,16 @@ FactoryBot.define do libelle { 'Menu déroulant' } type_champ { TypeDeChamp.type_champs.fetch(:drop_down_list) } drop_down_list { create(:drop_down_list) } + trait :long do + drop_down_list { create(:drop_down_list, :long) } + end end factory :type_de_champ_multiple_drop_down_list do type_champ { TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) } drop_down_list { create(:drop_down_list) } + trait :long do + drop_down_list { create(:drop_down_list, :long) } + end end factory :type_de_champ_linked_drop_down_list do type_champ { TypeDeChamp.type_champs.fetch(:linked_drop_down_list) } diff --git a/spec/features/users/brouillon_spec.rb b/spec/features/users/brouillon_spec.rb index 8b6a02d2d..d9c126487 100644 --- a/spec/features/users/brouillon_spec.rb +++ b/spec/features/users/brouillon_spec.rb @@ -21,9 +21,12 @@ feature 'The user' do fill_in('email', with: 'loulou@yopmail.com') fill_in('phone', with: '1234567890') choose('Non') - select('val2', from: form_id_for('simple_drop_down_list')) - select('val1', from: form_id_for('multiple_drop_down_list')) - select('val3', from: form_id_for('multiple_drop_down_list')) + choose('val2') + check('val1') + check('val3') + select('bravo', from: form_id_for('simple_choice_drop_down_list_long')) + select('alpha', from: form_id_for('multiple_choice_drop_down_list_long')) + select('charly', from: form_id_for('multiple_choice_drop_down_list_long')) select('AUSTRALIE', from: 'pays') select_champ_geo('regions', 'Ma', 'Martinique') @@ -55,6 +58,8 @@ feature 'The user' do expect(champ_value_for('phone')).to eq('1234567890') expect(champ_value_for('yes_no')).to eq('false') expect(champ_value_for('simple_drop_down_list')).to eq('val2') + expect(champ_value_for('simple_choice_drop_down_list_long')).to eq('bravo') + expect(JSON.parse(champ_value_for('multiple_choice_drop_down_list_long'))).to match(['alpha', 'charly']) expect(JSON.parse(champ_value_for('multiple_drop_down_list'))).to match(['val1', 'val3']) expect(champ_value_for('pays')).to eq('AUSTRALIE') expect(champ_value_for('regions')).to eq('Martinique') @@ -76,8 +81,11 @@ feature 'The user' do expect(page).to have_field('email', with: 'loulou@yopmail.com') expect(page).to have_field('phone', with: '1234567890') expect(page).to have_checked_field('Non') - expect(page).to have_selected_value('simple_drop_down_list', selected: 'val2') - expect(page).to have_selected_value('multiple_drop_down_list', selected: ['val1', 'val3']) + expect(page).to have_checked_field('val2') + expect(page).to have_checked_field('val1') + expect(page).to have_checked_field('val3') + expect(page).to have_selected_value('simple_choice_drop_down_list_long', selected: 'bravo') + expect(page).to have_selected_value('multiple_choice_drop_down_list_long', selected: ['alpha', 'charly']) expect(page).to have_selected_value('pays', selected: 'AUSTRALIE') expect(page).to have_selected_value('regions', selected: 'Martinique') expect(page).to have_selected_value('departements', selected: '02 - Aisne') From 6b3631dbfedab1a4a87a99fda62984942a1ed9b6 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 24 Jun 2020 12:18:46 +0200 Subject: [PATCH 5/5] extract rendering condition --- app/models/champs/drop_down_list_champ.rb | 4 ++++ .../champs/multiple_drop_down_list_champ.rb | 4 ++++ .../editable_champs/_drop_down_list.html.haml | 19 +++++++++---------- .../_multiple_drop_down_list.html.haml | 16 ++++++++-------- 4 files changed, 25 insertions(+), 18 deletions(-) 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 +