diff --git a/app/views/rails_admin/main/_form_boolean.html.erb b/app/views/rails_admin/main/_form_boolean.html.erb index 87ae0bab16..e9685ff1cb 100644 --- a/app/views/rails_admin/main/_form_boolean.html.erb +++ b/app/views/rails_admin/main/_form_boolean.html.erb @@ -1,14 +1,10 @@ -<% if field.nullable? %> -
- <% {'1': [true, 'btn-outline-success'], '0': [false, 'btn-outline-danger'], '': [nil, 'btn-outline-secondary']}.each do |text, (value, btn_class)| %> - <%= form.radio_button field.method_name, text, field.html_attributes.reverse_merge({ checked: field.form_value == value, required: field.required, class: 'btn-check' }) %> - - <% end %> -
-<% else %> -
- <%= form.send field.view_helper, field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, checked: field.form_value.in?([true, '1']), required: field.required, class: 'form-check-input' }) %> -
+<%-cases={'1': [true, 'btn-outline-success'], '0': [false, 'btn-outline-danger']}%> +<%-cases[''] = [nil, 'btn-outline-secondary'] if field.nullable? %> +
+<% cases.each do |text, (value, btn_class)| %> + <%= form.radio_button field.method_name, text, field.html_attributes.reverse_merge({ checked: field.form_value == value, required: field.required, class: 'btn-check' }) %> + <% end %> +
diff --git a/spec/integration/actions/edit_spec.rb b/spec/integration/actions/edit_spec.rb index f44e655296..9704a65f9a 100644 --- a/spec/integration/actions/edit_spec.rb +++ b/spec/integration/actions/edit_spec.rb @@ -873,7 +873,7 @@ class HelpTest < Tableless record = FieldTest.create visit edit_path(model_name: 'field_test', id: record.id) expect do - check 'field_test[open]' + find('label[for=field_test_open_1]').click click_button 'Save' end.to change { record.reload.open }.from(nil).to(true) end diff --git a/spec/integration/fields/boolean_spec.rb b/spec/integration/fields/boolean_spec.rb index 088a1cdd3e..b9b7075da5 100644 --- a/spec/integration/fields/boolean_spec.rb +++ b/spec/integration/fields/boolean_spec.rb @@ -53,19 +53,27 @@ end end - it 'shows a checkbox' do + it 'shows 2 radio buttons' do visit new_path(model_name: 'field_test') is_expected.to have_content 'New Field test' - is_expected.to have_css '[type="checkbox"][name="field_test[boolean_field]"]' + expect(all('[name="field_test[boolean_field]"]').map { |e| e['value'] }).to eq %w[1 0] end it 'can be updated' do visit edit_path(model_name: 'field_test', id: field_test.id) - find('.boolean_type input').check + + # change the value to true and assert the values + find('.boolean_type label.success').click click_button 'Save and edit' + # validate that the success button rendered and is active + expect(page).to have_selector('.boolean_type input[value="1"][checked]') + # validate the value is true expect(field_test.reload.boolean_field).to be true - find('.boolean_type input').uncheck + + # change the value to false and assert the values + find('.boolean_type label.danger').click click_button 'Save and edit' + expect(page).to have_selector('.boolean_type input[value="0"][checked]') expect(field_test.reload.boolean_field).to be false end end