select_multiple: add multiple_drop_down_list type

This commit is contained in:
Simon Lehericey 2017-03-15 17:47:26 +01:00
parent 7596c2424e
commit 657ce53a60
9 changed files with 66 additions and 29 deletions

View file

@ -36,6 +36,7 @@ function on_change_type_de_champ_select (){
parent.addClass('header_section'); parent.addClass('header_section');
break; break;
case 'drop_down_list': case 'drop_down_list':
case 'multiple_drop_down_list':
parent.children(".drop_down_list").addClass('show_inline'); parent.children(".drop_down_list").addClass('show_inline');
break; break;
case 'explication': case 'explication':

View file

@ -2,6 +2,19 @@ class DropDownList < ActiveRecord::Base
belongs_to :type_de_champ belongs_to :type_de_champ
def options def options
value.split(/[\r\n]|[\r]|[\n]|[\n\r]/).reject(&:empty?) result = value.split(/[\r\n]|[\r]|[\n]|[\n\r]/).reject(&:empty?)
result.blank? ? [] : [''] + result
end
def disabled_options
options.select{ |v| !(v =~ /^--.*--$/).nil? }
end
def selected_options(champ)
champ.value.blank? ? [] : multiple ? JSON.parse(champ.value) : [champ.value]
end
def multiple
type_de_champ.type_champ == 'multiple_drop_down_list'
end end
end end

View file

@ -12,6 +12,7 @@ class TypeDeChamp < ActiveRecord::Base
address: 'address', address: 'address',
yes_no: 'yes_no', yes_no: 'yes_no',
drop_down_list: 'drop_down_list', drop_down_list: 'drop_down_list',
multiple_drop_down_list: 'multiple_drop_down_list',
pays: 'pays', pays: 'pays',
regions: 'regions', regions: 'regions',
departements: 'departements', departements: 'departements',

View file

@ -1,5 +1,7 @@
= f.fields_for @types_de_champ_facade.fields_for_var, types_de_champ, remote: true do |ff| = f.fields_for @types_de_champ_facade.fields_for_var, types_de_champ, remote: true do |ff|
.form-inline{ class: (ff.object.object.type_champ == 'header_section' ? 'header_section' : '') } - type_champ = ff.object.object.type_champ
.form-inline{ class: (type_champ == 'header_section' ? 'header_section' : nil) }
.form-group.libelle .form-group.libelle
%h4 Libellé %h4 Libellé
= ff.text_field :libelle, class: 'form-control libelle', placeholder: 'Libellé' = ff.text_field :libelle, class: 'form-control libelle', placeholder: 'Libellé'
@ -12,15 +14,15 @@
%h4 Description %h4 Description
= ff.text_area :description, class: 'form-control description', placeholder: 'Description', rows: 3 = ff.text_area :description, class: 'form-control description', placeholder: 'Description', rows: 3
.form-group.drop_down_list{ class: (ff.object.object.type_champ == 'drop_down_list' ? 'show_inline' : ''), style: 'margin-right: 5px' } .form-group.drop_down_list{ class: (%w(drop_down_list multiple_drop_down_list).include?(type_champ) ? 'show_inline' : nil), style: 'margin-right: 5px' }
%h4 Liste déroulante %h4 Liste déroulante
= ff.fields_for :drop_down_list_attributes, ff.object.object.drop_down_list do |fff| = ff.fields_for :drop_down_list_attributes, ff.object.object.drop_down_list do |fff|
= fff.text_area :value, class: 'form-control drop_down_list', placeholder: "Ecrire une valeur par ligne.\nEcrire --valeur-- pour un séparateur.", rows: 3, cols: 30 ~ fff.text_area :value, class: 'form-control drop_down_list', placeholder: "Ecrire une valeur par ligne.\nEcrire --valeur-- pour un séparateur.", rows: 3, cols: 30
= fff.hidden_field :id = fff.hidden_field :id
- hide_mandatory = (ff.object.object.class == TypeDeChampPrivate || ff.object.object.type_champ == 'explication') - hide_mandatory = (ff.object.object.class == TypeDeChampPrivate || type_champ == 'explication')
.form-group.mandatory{ style: hide_mandatory ? 'visibility: hidden;' : '' } .form-group.mandatory{ style: hide_mandatory ? 'visibility: hidden;' : nil }
%h4 Obligatoire ? %h4 Obligatoire ?
.center .center
= ff.check_box :mandatory, placeholder: 'Obligatoire ?' = ff.check_box :mandatory, placeholder: 'Obligatoire ?'

View file

@ -1,2 +1,2 @@
- unless champ.drop_down_list.nil? - unless champ.drop_down_list.nil?
= render partial: 'users/description/champs/drop_down_template', locals: {values: champ.drop_down_list.options, champ: champ} = render partial: 'users/description/champs/drop_down_template', locals: { drop_down_list: champ.drop_down_list, champ: champ }

View file

@ -1,18 +1,6 @@
%select{ name:"champs['#{champ.id}']", - unless drop_down_list.options.blank?
id: "champs_#{champ.id}" } = select_tag("champs['#{champ.id}']",
options_for_select(drop_down_list.options, selected: drop_down_list.selected_options(champ),
- unless values.blank? disabled: drop_down_list.disabled_options),
%option multiple: drop_down_list.multiple,
= '' class: drop_down_list.multiple ? 'select2' : nil)
- values.each do |option|
- if (option=~ /^--.*--$/).nil?
- if champ.value == option
%option{selected:''}
= option
- else
%option
= option
-else
%option{disabled:''}
= option

View file

@ -26,7 +26,7 @@
- elsif champ.type_champ == 'yes_no' - elsif champ.type_champ == 'yes_no'
= render partial: 'users/description/champs/yes_no', locals: { champ: champ } = render partial: 'users/description/champs/yes_no', locals: { champ: champ }
- elsif champ.type_champ == 'drop_down_list' - elsif %w(drop_down_list multiple_drop_down_list).include?(champ.type_champ)
= render partial: 'users/description/champs/drop_down_list', locals: { champ: champ } = render partial: 'users/description/champs/drop_down_list', locals: { champ: champ }
- elsif champ.type_champ == 'pays' - elsif champ.type_champ == 'pays'

View file

@ -23,3 +23,4 @@ fr:
engagement: 'Engagement' engagement: 'Engagement'
header_section: 'Titre de section' header_section: 'Titre de section'
explication: 'Explication' explication: 'Explication'
multiple_drop_down_list: 'Menu déroulant à choix multiples'

View file

@ -9,15 +9,16 @@ describe DropDownList do
it { is_expected.to belong_to(:type_de_champ) } it { is_expected.to belong_to(:type_de_champ) }
end end
let(:dropdownlist) { create :drop_down_list, value: value }
describe '#options' do describe '#options' do
let(:value) { "Cohésion sociale let(:value) { "Cohésion sociale
Dév.Eco / Emploi Dév.Eco / Emploi
Cadre de vie / Urb. Cadre de vie / Urb.
Pilotage / Ingénierie Pilotage / Ingénierie
" } " }
let(:dropdownlist) { create :drop_down_list, value: value }
it { expect(dropdownlist.options).to eq ["Cohésion sociale", "Dév.Eco / Emploi", "Cadre de vie / Urb.", "Pilotage / Ingénierie"] } it { expect(dropdownlist.options).to eq ['', 'Cohésion sociale', 'Dév.Eco / Emploi', 'Cadre de vie / Urb.', 'Pilotage / Ingénierie'] }
context 'when one value is empty' do context 'when one value is empty' do
let(:value) { "Cohésion sociale let(:value) { "Cohésion sociale
@ -26,7 +27,37 @@ Cadre de vie / Urb.
Pilotage / Ingénierie Pilotage / Ingénierie
" } " }
it { expect(dropdownlist.options).to eq ["Cohésion sociale", "Cadre de vie / Urb.", "Pilotage / Ingénierie"] } it { expect(dropdownlist.options).to eq ['', 'Cohésion sociale', 'Cadre de vie / Urb.', 'Pilotage / Ingénierie'] }
end
end
describe 'disabled_options' do
let(:value) { "tip
--top--
--troupt--
ouaich" }
it { expect(dropdownlist.disabled_options).to match(['--top--', '--troupt--']) }
end
describe 'selected_options' do
let(:dropdownlist) do
create(:drop_down_list, type_de_champ: type_de_champ)
end
context 'when multiple' do
let(:type_de_champ) { TypeDeChamp.new(type_champ: 'multiple_drop_down_list') }
let(:champ) { Champ.new(value: '["1","2"]') }
it { expect(dropdownlist.selected_options(champ)).to match(['1', '2']) }
end
context 'when simple' do
let(:type_de_champ) { TypeDeChamp.new(type_champ: 'drop_down_list') }
let(:champ) { Champ.new(value: '1') }
it { expect(dropdownlist.selected_options(champ)).to match(['1']) }
end end
end end
end end