From 1d129459cae1105752e9e223299db0a3dead7645 Mon Sep 17 00:00:00 2001 From: Denys Metelov Date: Thu, 29 Jan 2026 18:31:45 +0100 Subject: [PATCH 1/2] Fix file caching issue when form is resubmitted without file --- lib/active_admin_import/model.rb | 1 + spec/import_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/active_admin_import/model.rb b/lib/active_admin_import/model.rb index f3fde01..f3d4a0e 100644 --- a/lib/active_admin_import/model.rb +++ b/lib/active_admin_import/model.rb @@ -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| diff --git a/spec/import_spec.rb b/spec/import_spec.rb index cc4db50..bb3345c 100644 --- a/spec/import_spec.rb +++ b/spec/import_spec.rb @@ -588,4 +588,26 @@ 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 end From 00c6464c4e41b5c944f8daf4c8f23d55c11e5c6f Mon Sep 17 00:00:00 2001 From: Denys Metelov Date: Thu, 29 Jan 2026 18:32:15 +0100 Subject: [PATCH 2/2] Fix validation order - validate file format before encoding --- lib/active_admin_import/model.rb | 2 +- spec/fixtures/files/author_invalid_format.txt | 2 ++ spec/import_spec.rb | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/files/author_invalid_format.txt diff --git a/lib/active_admin_import/model.rb b/lib/active_admin_import/model.rb index f3d4a0e..716f794 100644 --- a/lib/active_admin_import/model.rb +++ b/lib/active_admin_import/model.rb @@ -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 diff --git a/spec/fixtures/files/author_invalid_format.txt b/spec/fixtures/files/author_invalid_format.txt new file mode 100644 index 0000000..cd4fcd6 --- /dev/null +++ b/spec/fixtures/files/author_invalid_format.txt @@ -0,0 +1,2 @@ +Name,Last name,Birthday +John,Doe,1986-05-01 diff --git a/spec/import_spec.rb b/spec/import_spec.rb index bb3345c..5128da2 100644 --- a/spec/import_spec.rb +++ b/spec/import_spec.rb @@ -610,4 +610,20 @@ def upload_file!(name, ext = 'csv') 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