Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix export crash for models with JSON field #3056

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/rails_admin/config/fields/types/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ class Json < RailsAdmin::Config::Fields::Types::Text
bindings[:view].content_tag(:pre) { formatted_value }.html_safe
end

register_instance_option :export_value do
formatted_value
end

def parse_value(value)
value.present? ? JSON.parse(value) : nil
rescue JSON::ParserError
nil
end

def parse_input(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
"#{value} exported"
end
end

field :json_field, :json do
formatted_value do
'{}'
end
end
end
end

Expand All @@ -46,7 +52,7 @@
click_button 'Export to csv'
csv = CSV.parse page.driver.response.body.force_encoding('utf-8') # comes through as us-ascii on some platforms
expect(csv[0]).to match_array ['Id', 'Created at', 'Updated at', 'Deleted at', 'Name', 'Position',
'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Formation', 'Id [Team]', 'Created at [Team]',
'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Formation', 'Json field', 'Id [Team]', 'Created at [Team]',
'Updated at [Team]', 'Name [Team]', 'Logo url [Team]', 'Team Manager [Team]', 'Ballpark [Team]',
'Mascot [Team]', 'Founded [Team]', 'Wins [Team]', 'Losses [Team]', 'Win percentage [Team]',
'Revenue [Team]', 'Color [Team]', 'Custom field [Team]', 'Main Sponsor [Team]', 'Id [Draft]', 'Created at [Draft]',
Expand Down
10 changes: 10 additions & 0 deletions spec/rails_admin/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def predicates_for(scope)
let(:abstract_model) { RailsAdmin::AbstractModel.new('Player') }

before do
RailsAdmin.config do |c|
c.model Player do
include_all_fields

field :json_field, :json do
queryable true
end
end
end

@players = FactoryGirl.create_list(:player, 3) + [
# Multibyte players
FactoryGirl.create(:player, name: 'Антоха'),
Expand Down
31 changes: 29 additions & 2 deletions spec/rails_admin/config/fields/types/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@
end
end

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

it 'returns correct value for empty json' do
allow(object).to receive(:json_field) { {} }
actual = field.with(bindings).export_value
expect(actual).to match(/{\n+}/)
end

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

describe '#parse_input' do
before :each do
RailsAdmin.config do |config|
Expand All @@ -72,8 +99,8 @@
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)
it 'returns nil with invalid json string' do
expect(field.parse_input(json_field: '{{')).to be_nil
end
end

Expand Down