From 95b83a57c19ec9b5a44acde0d296df0836cc668b Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 30 Mar 2017 15:24:40 +0200 Subject: [PATCH] Checkbox should be checked when value is 'on' --- .../description/champs/_checkbox.html.haml | 3 +-- spec/factories/type_de_champ_public.rb | 4 +++ .../_render_list_champs.html.haml_spec.rb | 27 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 spec/views/users/description/champs/_render_list_champs.html.haml_spec.rb diff --git a/app/views/users/description/champs/_checkbox.html.haml b/app/views/users/description/champs/_checkbox.html.haml index bae7b55ad..db7928656 100644 --- a/app/views/users/description/champs/_checkbox.html.haml +++ b/app/views/users/description/champs/_checkbox.html.haml @@ -1,2 +1 @@ -%input{type: 'hidden', name:"champs['#{champ.id}']", id: "champs_#{champ.id}", value: ''} -%input{type: 'checkbox', style:'margin-left: 15px;', name:"champs['#{champ.id}']", id: "champs_#{champ.id}", checked: ('checked' if champ.value == 'on')} +%input{type: 'checkbox', style:'margin-left: 15px;', name:"champs['#{champ.id}']", id: "champs_#{champ.id}", checked: ('checked' if champ.object.value == 'on')} diff --git a/spec/factories/type_de_champ_public.rb b/spec/factories/type_de_champ_public.rb index 937921d0d..3dad7603b 100644 --- a/spec/factories/type_de_champ_public.rb +++ b/spec/factories/type_de_champ_public.rb @@ -5,5 +5,9 @@ FactoryGirl.define do type_champ 'text' order_place 1 mandatory false + + trait :checkbox do + type_champ 'checkbox' + end end end diff --git a/spec/views/users/description/champs/_render_list_champs.html.haml_spec.rb b/spec/views/users/description/champs/_render_list_champs.html.haml_spec.rb new file mode 100644 index 000000000..a60d0e86b --- /dev/null +++ b/spec/views/users/description/champs/_render_list_champs.html.haml_spec.rb @@ -0,0 +1,27 @@ +describe 'users/description/champs/render_list_champs.html.haml', type: :view do + let(:type_champ) { create(:type_de_champ_public, :checkbox) } + + context "with a checkbox champ with value equals nil" do + let!(:champ_checkbox_checked) { create(:champ, type_de_champ: type_champ, value: nil) } + + before do + render 'users/description/champs/render_list_champs.html.haml', champs: Champ.all, order_place: 0 + end + + it 'should not render a checked checkbox' do + expect(rendered).not_to have_css('input[type=checkbox][checked]') + end + end + + context "with a checkbox champ with value equals 'on'" do + let!(:champ_checkbox_checked) { create(:champ, type_de_champ: type_champ, value: 'on') } + + before do + render 'users/description/champs/render_list_champs.html.haml', champs: Champ.all, order_place: 0 + end + + it 'should render a checked checkbox' do + expect(rendered).to have_css('input[type=checkbox][checked]') + end + end +end