Skip to content

Commit 5232574

Browse files
author
artemlutsenko
committed
Fix error when passed empty empty csv and model has force_encoding=:auto
1 parent 9ca311a commit 5232574

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/active_admin_import/model.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ def file_path
103103

104104
def encode_file
105105
data = File.read(file_path)
106+
return if data.empty?
107+
106108
File.open(file_path, 'w') do |f|
107109
f.write(encode(data))
108110
end

spec/import_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,22 @@ def upload_file!(name, ext = 'csv')
424424
expect(Author.count).to eq(2)
425425
end
426426
end
427+
428+
context 'with empty csv and auto detect encoding' do
429+
let(:options) do
430+
attributes = { force_encoding: :auto }
431+
{ template_object: ActiveAdminImport::Model.new(attributes) }
432+
end
433+
434+
before do
435+
upload_file!(:empty)
436+
end
437+
438+
it 'should render warning' do
439+
expect(page).to have_content I18n.t('active_admin_import.file_empty_error')
440+
expect(Author.count).to eq(0)
441+
end
442+
end
427443
end
428444

429445
context 'with callback procs options' do

0 commit comments

Comments
 (0)