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');
break;
case 'drop_down_list':
case 'multiple_drop_down_list':
parent.children(".drop_down_list").addClass('show_inline');
break;
case 'explication':

View file

@ -2,6 +2,19 @@ class DropDownList < ActiveRecord::Base
belongs_to :type_de_champ
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

View file

@ -12,6 +12,7 @@ class TypeDeChamp < ActiveRecord::Base
address: 'address',
yes_no: 'yes_no',
drop_down_list: 'drop_down_list',
multiple_drop_down_list: 'multiple_drop_down_list',
pays: 'pays',
regions: 'regions',
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|
.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
%h4 Libellé
= ff.text_field :libelle, class: 'form-control libelle', placeholder: 'Libellé'
@ -12,15 +14,15 @@
%h4 Description
= 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
= 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
- hide_mandatory = (ff.object.object.class == TypeDeChampPrivate || ff.object.object.type_champ == 'explication')
.form-group.mandatory{ style: hide_mandatory ? 'visibility: hidden;' : '' }
- hide_mandatory = (ff.object.object.class == TypeDeChampPrivate || type_champ == 'explication')
.form-group.mandatory{ style: hide_mandatory ? 'visibility: hidden;' : nil }
%h4 Obligatoire ?
.center
= ff.check_box :mandatory, placeholder: 'Obligatoire ?'

View file

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

View file

@ -26,7 +26,7 @@
- elsif champ.type_champ == 'yes_no'
= 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 }
- elsif champ.type_champ == 'pays'

View file

@ -23,3 +23,4 @@ fr:
engagement: 'Engagement'
header_section: 'Titre de section'
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) }
end
let(:dropdownlist) { create :drop_down_list, value: value }
describe '#options' do
let(:value) { "Cohésion sociale
Dév.Eco / Emploi
Cadre de vie / Urb.
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
let(:value) { "Cohésion sociale
@ -26,7 +27,37 @@ Cadre de vie / Urb.
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