Skip to content

Commit 9fd8d03

Browse files
committed
Merge commit 'b0fa4c74ea13582227d4b22cbcfa16c3ad37f46d' from pull request #3056
2 parents 59478af + b0fa4c7 commit 9fd8d03

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 0 deletions
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
end

spec/integration/basic/export/rails_admin_basic_export_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
"#{value} exported"
3838
end
3939
end
40+
41+
field :json_field, :json do
42+
formatted_value do
43+
'{}'
44+
end
45+
end
4046
end
4147
end
4248

@@ -46,7 +52,7 @@
4652
click_button 'Export to csv'
4753
csv = CSV.parse page.driver.response.body.force_encoding('utf-8') # comes through as us-ascii on some platforms
4854
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]',
55+
'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Formation', 'Json field', 'Id [Team]', 'Created at [Team]',
5056
'Updated at [Team]', 'Name [Team]', 'Logo url [Team]', 'Team Manager [Team]', 'Ballpark [Team]',
5157
'Mascot [Team]', 'Founded [Team]', 'Wins [Team]', 'Losses [Team]', 'Win percentage [Team]',
5258
'Revenue [Team]', 'Color [Team]', 'Custom field [Team]', 'Main Sponsor [Team]', 'Id [Draft]', 'Created at [Draft]',

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

Lines changed: 27 additions & 0 deletions
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|

0 commit comments

Comments
 (0)