Skip to content

Use the same widget which works for nullable booleans also for non nullable #3614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions app/views/rails_admin/main/_form_boolean.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<% if field.nullable? %>
<div class="btn-group" role="group">
<% {'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' }) %>
<label for="<%= form.object_name %>_<%= field.method_name %>_<%= text %>" class="<%= field.css_classes[value] %> btn <%= btn_class %>">
<%= field.labels[value].html_safe %>
</label>
<% end %>
</div>
<% else %>
<div class="col-form-label">
<%= 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' }) %>
</div>
<%-cases={'1': [true, 'btn-outline-success'], '0': [false, 'btn-outline-danger']}%>
<%-cases[''] = [nil, 'btn-outline-secondary'] if field.nullable? %>
<div class="btn-group" role="group">
<% 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' }) %>
<label for="<%= form.object_name %>_<%= field.method_name %>_<%= text %>" class="<%= field.css_classes[value] %> btn <%= btn_class %>">
<%= field.labels[value].html_safe %>
</label>
<% end %>
</div>
2 changes: 1 addition & 1 deletion spec/integration/actions/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions spec/integration/fields/boolean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down