Skip to content
Draft
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
3 changes: 2 additions & 1 deletion lib/active_admin_import/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module CONST
validate :file_contents_present, if: ->(me) { me.file.present? }

before_validation :unzip_file, if: ->(me) { me.archive? && me.allow_archive? }
before_validation :encode_file, if: ->(me) { me.force_encoding? && me.file.present? }
after_validation :encode_file, if: ->(me) { me.force_encoding? && me.file.present? }

attr_reader :attributes

Expand All @@ -48,6 +48,7 @@ def initialize(args = {})
end

def assign_attributes(args = {}, new_record = false)
args[:file] = nil unless args.key?(:file)
@attributes.merge!(args)
@new_record = new_record
args.keys.each do |key|
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/files/author_invalid_format.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Name,Last name,Birthday
John,Doe,1986-05-01
38 changes: 38 additions & 0 deletions spec/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,42 @@ def upload_file!(name, ext = 'csv')
expect { add_author_resource(options) }.to raise_error(ArgumentError)
end
end

context 'when submitting empty form after validation error' do
let(:options) { {} }

before do
add_author_resource(options)
visit '/admin/authors/import'
end

it 'should NOT reuse cached file from previous submission' do
expect do
upload_file!(:author_broken_header)
expect(page).to have_content("can't write unknown attribute")
end.not_to change { Author.count }

# Second submission without selecting a file
expect do
find_button('Import').click
expect(page).to have_content(I18n.t('active_admin_import.no_file_error'))
end.not_to change { Author.count }
end
end

context 'when importing file with invalid format and auto force_encoding' do
let(:options) { { template_object: ActiveAdminImport::Model.new(force_encoding: :auto) } }

before do
add_author_resource(options)
visit '/admin/authors/import'
end

it 'should reject invalid file format before encoding' do
expect do
upload_file!(:author_invalid_format, 'txt')
expect(page).to have_content I18n.t('active_admin_import.file_format_error')
end.not_to change { Author.count }
end
end
end
Loading