Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make empty string to nil conversion behavior configurable
By default, empty strings passed into nullable columns get converted to nils. You can disable that conversion by setting `treat_empty_as_nil false`. Refs. #2099, Fixes #3172
- Loading branch information
Showing
23 changed files
with
140 additions
and
78 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
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
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,19 @@ | ||
require 'rails_admin/config/fields/base' | ||
|
||
module RailsAdmin | ||
module Config | ||
module Fields | ||
module Types | ||
class StringLike < RailsAdmin::Config::Fields::Base | ||
register_instance_option :treat_empty_as_nil? do | ||
properties.try(:nullable?) | ||
end | ||
|
||
def parse_input(params) | ||
params[name] = params[name].presence if params.key?(name) && treat_empty_as_nil? | ||
end | ||
end | ||
end | ||
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
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
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,83 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe RailsAdmin::Config::Fields::Types::StringLike do | ||
describe '#treat_empty_as_nil?', active_record: true do | ||
context 'with a nullable field' do | ||
subject do | ||
RailsAdmin.config('Team').fields.detect do |f| | ||
f.name == :name | ||
end.with(object: Team.new) | ||
end | ||
|
||
it 'is true' do | ||
expect(subject.treat_empty_as_nil?).to be true | ||
end | ||
end | ||
|
||
context 'with a non-nullable field' do | ||
subject do | ||
RailsAdmin.config('Team').fields.detect do |f| | ||
f.name == :manager | ||
end.with(object: Team.new) | ||
end | ||
|
||
it 'is false' do | ||
expect(subject.treat_empty_as_nil?).to be false | ||
end | ||
end | ||
end | ||
|
||
describe '#parse_input' do | ||
subject do | ||
RailsAdmin.config('FieldTest').fields.detect do |f| | ||
f.name == :string_field | ||
end.with(object: FieldTest.new) | ||
end | ||
|
||
context 'with treat_empty_as_nil being true' do | ||
before do | ||
RailsAdmin.config FieldTest do | ||
field :string_field do | ||
treat_empty_as_nil true | ||
end | ||
end | ||
end | ||
|
||
context 'when value is empty' do | ||
let(:params) { {string_field: ''} } | ||
|
||
it 'makes the value nil' do | ||
subject.parse_input(params) | ||
expect(params.key?(:string_field)).to be true | ||
expect(params[:string_field]).to be nil | ||
end | ||
end | ||
|
||
context 'when value does not exist in params' do | ||
let(:params) { {} } | ||
|
||
it 'does not touch params' do | ||
subject.parse_input(params) | ||
expect(params.key?(:string_field)).to be false | ||
end | ||
end | ||
end | ||
|
||
context 'with treat_empty_as_nil being false' do | ||
before do | ||
RailsAdmin.config FieldTest do | ||
field :string_field do | ||
treat_empty_as_nil false | ||
end | ||
end | ||
end | ||
let(:params) { {string_field: ''} } | ||
|
||
it 'keeps the value untouched' do | ||
subject.parse_input(params) | ||
expect(params.key?(:string_field)).to be true | ||
expect(params[:string_field]).to eq '' | ||
end | ||
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
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