-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support nullable boolean field. Closes #3145
- Loading branch information
Showing
8 changed files
with
120 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
.checkbox | ||
%label{ style: 'display: block;' } | ||
= 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}) | ||
- if field.nullable? | ||
.btn-group{'data-toggle': 'buttons'} | ||
- {'1': true, '0': false, '': nil}.each do |text, value| | ||
%label.btn.btn-default{class: [field.css_classes[value], ("active" if field.form_value == value)]} | ||
= form.radio_button field.method_name, text, field.html_attributes.reverse_merge({ checked: field.form_value == value, required: field.required}) | ||
= field.labels[value].html_safe | ||
- else | ||
.checkbox | ||
%label{ style: 'display: block;' } | ||
= 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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe 'Boolean field', type: :request do | ||
subject { page } | ||
let(:field_test) { FactoryBot.create :field_test } | ||
|
||
context 'if nullable' do | ||
before do | ||
RailsAdmin.config FieldTest do | ||
field :boolean_field | ||
end | ||
end | ||
|
||
it 'shows 3 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', ''] | ||
end | ||
|
||
it 'can be updated' do | ||
visit edit_path(model_name: 'field_test', id: field_test.id) | ||
find('.boolean_type .icon-ok').sibling('input').click | ||
click_button 'Save and edit' | ||
expect(field_test.reload.boolean_field).to be true | ||
find('.boolean_type .icon-remove').sibling('input').click | ||
click_button 'Save and edit' | ||
expect(field_test.reload.boolean_field).to be false | ||
find('.boolean_type .icon-minus').sibling('input').click | ||
click_button 'Save and edit' | ||
expect(field_test.reload.boolean_field).to be nil | ||
end | ||
end | ||
|
||
context 'if not nullable' do | ||
before do | ||
RailsAdmin.config FieldTest do | ||
field :boolean_field do | ||
nullable false | ||
end | ||
end | ||
end | ||
|
||
it 'shows a checkbox' 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]"]' | ||
end | ||
|
||
it 'can be updated' do | ||
visit edit_path(model_name: 'field_test', id: field_test.id) | ||
find('.boolean_type input').check | ||
click_button 'Save and edit' | ||
expect(field_test.reload.boolean_field).to be true | ||
find('.boolean_type input').uncheck | ||
click_button 'Save and edit' | ||
expect(field_test.reload.boolean_field).to be false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters