Skip to content

Commit 885e742

Browse files
committed
support for tsv files added
1 parent 206d7e9 commit 885e742

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

lib/active_admin_import/dsl.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def active_admin_import(options = {}, &block)
9494
end
9595
rescue ActiveRecord::Import::MissingColumnError, NoMethodError, ActiveRecord::StatementInvalid, CSV::MalformedCSVError => e
9696
Rails.logger.error(I18n.t('active_admin_import.file_error', message: e.message))
97+
Rails.logger.error(e.backtrace.join("\n"))
9798
flash[:error] = I18n.t('active_admin_import.file_error', message: e.message[0..200])
9899
end
99100
redirect_to options[:back]

lib/active_admin_import/importer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def detect_csv_options
119119
model.csv_options
120120
else
121121
options[:csv_options] || {}
122-
end.reject { |_, value| value.blank? }
122+
end.reject { |_, value| value.nil? || value == "" }
123123
end
124124
end
125125
end

lib/active_admin_import/model.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ module CONST
2121
application/csv
2222
application/vnd.ms-excel
2323
application/vnd.msexcel
24+
text/tsv
25+
text/x-tsv
26+
text/tab-separated-values
27+
text/x-tab-separated-values
2428
).freeze
2529
end
2630

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Name Last name Birthday
2+
John Doe 1986-05-01
3+
Jane Roe 1988-11-16

spec/import_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,19 @@ def upload_file!(name, ext = 'csv')
397397
expect(Author.count).to eq(2)
398398
end
399399
end
400+
401+
context 'with tab separator' do
402+
let(:options) do
403+
attributes = { csv_options: { col_sep: "\t" } }
404+
{ template_object: ActiveAdminImport::Model.new(attributes) }
405+
end
406+
407+
it 'should import file' do
408+
upload_file!(:authors_with_tabs, 'tsv')
409+
expect(page).to have_content 'Successfully imported 2 authors'
410+
expect(Author.count).to eq(2)
411+
end
412+
end
400413
end
401414

402415
context 'with callback procs options' do

0 commit comments

Comments
 (0)