From 7caa0fff440f6209313bd0f3bb7a78f4837e71d1 Mon Sep 17 00:00:00 2001 From: Gabriele Tassoni Date: Tue, 28 Mar 2023 12:33:23 +0200 Subject: [PATCH 1/5] Use the same widget which works for nullable booleans also for non nullable, to preserve UI consistency for boolean fields --- .../rails_admin/main/_form_boolean.html.erb | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) 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 %> +
From 321b3dea74ea5a40f5de296c5230cb0905625fad Mon Sep 17 00:00:00 2001 From: Gabriele Tassoni Date: Tue, 28 Mar 2023 12:52:07 +0200 Subject: [PATCH 2/5] Add Spec Test for boolean if not nulable --- spec/integration/fields/boolean_spec.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/spec/integration/fields/boolean_spec.rb b/spec/integration/fields/boolean_spec.rb index 088a1cdd3e..98fff435aa 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 ['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 From 80fa4845addea4b1c0b583b4a6f793b75504026f Mon Sep 17 00:00:00 2001 From: Gabriele Tassoni Date: Tue, 28 Mar 2023 12:54:15 +0200 Subject: [PATCH 3/5] Fix Rubocop complaint with array of words usage --- spec/integration/fields/boolean_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/integration/fields/boolean_spec.rb b/spec/integration/fields/boolean_spec.rb index 98fff435aa..512a215d77 100644 --- a/spec/integration/fields/boolean_spec.rb +++ b/spec/integration/fields/boolean_spec.rb @@ -56,7 +56,7 @@ it 'shows 2 radio buttons' do visit new_path(model_name: 'field_test') is_expected.to have_content 'New Field test' - expect(all('[name="field_test[boolean_field]"]').map { |e| e['value'] }).to eq ['1', '0'] + expect(all('[name="field_test[boolean_field]"]').map { |e| e['value'] }).to eq %w( 1 0 ) end it 'can be updated' do From 568b1899180a3d7419211a1462fb5d0a2c2a34df Mon Sep 17 00:00:00 2001 From: Gabriele Tassoni Date: Tue, 28 Mar 2023 12:57:21 +0200 Subject: [PATCH 4/5] Fix Rubocop Style/PercentLiteralDelimiters and Layout/SpaceInsidePercentLiteralDelimiters --- spec/integration/fields/boolean_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/integration/fields/boolean_spec.rb b/spec/integration/fields/boolean_spec.rb index 512a215d77..b9b7075da5 100644 --- a/spec/integration/fields/boolean_spec.rb +++ b/spec/integration/fields/boolean_spec.rb @@ -56,7 +56,7 @@ it 'shows 2 radio buttons' do visit new_path(model_name: 'field_test') is_expected.to have_content 'New Field test' - expect(all('[name="field_test[boolean_field]"]').map { |e| e['value'] }).to eq %w( 1 0 ) + expect(all('[name="field_test[boolean_field]"]').map { |e| e['value'] }).to eq %w[1 0] end it 'can be updated' do From e09d5507d6d00175579ad5ef2b815b7a2b8906ea Mon Sep 17 00:00:00 2001 From: Gabriele Tassoni Date: Tue, 28 Mar 2023 13:17:30 +0200 Subject: [PATCH 5/5] Update test for nullable fields editability --- spec/integration/actions/edit_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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