Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1757 from linchus/master
Add json field datatype
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'rails_admin/config/fields/types/text' | ||
|
||
module RailsAdmin | ||
module Config | ||
module Fields | ||
module Types | ||
class Json < RailsAdmin::Config::Fields::Types::Text | ||
# Register field type for the type loader | ||
RailsAdmin::Config::Fields::Types.register(self) | ||
|
||
register_instance_option :formatted_value do | ||
value.present? ? JSON.pretty_generate(value) : nil | ||
end | ||
|
||
def parse_input(params) | ||
if params[name].is_a?(::String) | ||
params[name] = (params[name].blank? ? nil : JSON.parse(params[name])) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'spec_helper' | ||
|
||
describe RailsAdmin::Config::Fields::Types::Json do | ||
describe '#parse_input' do | ||
let(:field) { RailsAdmin.config(FieldTest).fields.detect { |f| f.name == :json_field } } | ||
before :each do | ||
RailsAdmin.config do |config| | ||
config.model FieldTest do | ||
field :json_field, :json | ||
end | ||
end | ||
end | ||
|
||
it 'parse valid json string' do | ||
data = {string: 'string', integer: 1, array: [1, 2, 3], object: {bla: 'foo'}}.as_json | ||
expect(field.parse_input(json_field: data.to_json)).to eq data | ||
end | ||
|
||
it 'raise JSON::ParserError with invalid json string' do | ||
expect { field.parse_input(json_field: '{{') }.to raise_error(JSON::ParserError) | ||
end | ||
|
||
end | ||
end |
acd360c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seriously can't wait for this! 👍