Skip to content

Commit 8291903

Browse files
committed
Fix export crash for JSON fields
NoMethodError: undefined method `content_tag' for nil:NilClass <class:Json>'
1 parent a8d2924 commit 8291903

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

lib/rails_admin/config/fields/types/json.rb

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class Json < RailsAdmin::Config::Fields::Types::Text
1717
bindings[:view].content_tag(:pre) { formatted_value }.html_safe
1818
end
1919

20+
register_instance_option :export_value do
21+
formatted_value
22+
end
23+
2024
def parse_value(value)
2125
value.present? ? JSON.parse(value) : nil
2226
rescue JSON::ParserError

spec/integration/basic/export/rails_admin_basic_export_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
click_button 'Export to csv'
4747
csv = CSV.parse page.driver.response.body.force_encoding('utf-8') # comes through as us-ascii on some platforms
4848
expect(csv[0]).to match_array ['Id', 'Created at', 'Updated at', 'Deleted at', 'Name', 'Position',
49-
'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Formation', 'Id [Team]', 'Created at [Team]',
49+
'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Formation', 'Schedule',
50+
'Id [Team]', 'Created at [Team]',
5051
'Updated at [Team]', 'Name [Team]', 'Logo url [Team]', 'Team Manager [Team]', 'Ballpark [Team]',
5152
'Mascot [Team]', 'Founded [Team]', 'Wins [Team]', 'Losses [Team]', 'Win percentage [Team]',
5253
'Revenue [Team]', 'Color [Team]', 'Custom field [Team]', 'Main Sponsor [Team]', 'Id [Draft]', 'Created at [Draft]',

spec/rails_admin/config/fields/types/json_spec.rb

+29-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,33 @@
5858
end
5959
end
6060

61+
describe '#export_value' do
62+
before do
63+
RailsAdmin.config do |config|
64+
config.model FieldTest do
65+
field :json_field, :json
66+
end
67+
end
68+
end
69+
70+
it 'returns correct value for empty json' do
71+
allow(object).to receive(:json_field) { {} }
72+
actual = field.with(bindings).export_value
73+
expect(actual).to match(/{\n+}/)
74+
end
75+
76+
it 'returns correct value' do
77+
allow(object).to receive(:json_field) { {sample_key: "sample_value"} }
78+
actual = field.with(bindings).export_value
79+
expected = [
80+
"{",
81+
" \"sample_key\": \"sample_value\"",
82+
"}",
83+
].join("\n")
84+
expect(actual).to eq(expected)
85+
end
86+
end
87+
6188
describe '#parse_input' do
6289
before :each do
6390
RailsAdmin.config do |config|
@@ -72,8 +99,8 @@
7299
expect(field.parse_input(json_field: data.to_json)).to eq data
73100
end
74101

75-
it 'raise JSON::ParserError with invalid json string' do
76-
expect { field.parse_input(json_field: '{{') }.to raise_error(JSON::ParserError)
102+
it 'return nil with invalid json string' do
103+
expect(field.parse_input(json_field: '{{')).to be_nil
77104
end
78105
end
79106

0 commit comments

Comments
 (0)