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