Skip to content

Commit

Permalink
Merge pull request #2973 from lazebny/fix-json-field-tag-pre-in-edit-…
Browse files Browse the repository at this point in the history
…form

Fix json field tag <pre> in edit form
  • Loading branch information
mshibuya authored Jan 2, 2018
2 parents 57961eb + 39691fb commit 0ad6ce1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/rails_admin/config/fields/types/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ class Json < RailsAdmin::Config::Fields::Types::Text
RailsAdmin::Config::Fields::Types.register(:jsonb, self)

register_instance_option :formatted_value do
if value.present?
bindings[:view].content_tag(:pre) { JSON.pretty_generate(value) }.html_safe
end
value.present? ? JSON.pretty_generate(value) : nil
end

register_instance_option :pretty_value do
bindings[:view].content_tag(:pre) { formatted_value }.html_safe
end

def parse_value(value)
Expand Down
49 changes: 49 additions & 0 deletions spec/rails_admin/config/fields/types/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,55 @@

describe RailsAdmin::Config::Fields::Types::Json do
let(:field) { RailsAdmin.config(FieldTest).fields.detect { |f| f.name == :json_field } }
let(:object) { FieldTest.new }
let(:bindings) do
{
object: object,
view: ApplicationController.new.view_context,
}
end

describe '#formatted_value' do
before do
RailsAdmin.config do |config|
config.model FieldTest do
field :json_field, :json
end
end
end

it 'retuns correct value' do
allow(object).to receive(:json_field) { {sample_key: "sample_value"} }
actual = field.with(bindings).formatted_value
expected = [
"{",
" \"sample_key\": \"sample_value\"",
"}",
].join("\n")
expect(actual).to eq(expected)
end
end

describe '#pretty_value' do
before do
RailsAdmin.config do |config|
config.model FieldTest do
field :json_field, :json
end
end
end

it 'retuns correct value' do
allow(object).to receive(:json_field) { {sample_key: "sample_value"} }
actual = field.with(bindings).pretty_value
expected = [
"<pre>{",
" &quot;sample_key&quot;: &quot;sample_value&quot;",
"}</pre>",
].join("\n")
expect(actual).to eq(expected)
end
end

describe '#parse_input' do
before :each do
Expand Down

0 comments on commit 0ad6ce1

Please sign in to comment.