Skip to content

Commit

Permalink
support for tsv files added
Browse files Browse the repository at this point in the history
  • Loading branch information
Fivell committed Mar 18, 2017
1 parent 206d7e9 commit 885e742
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/active_admin_import/dsl.rb
Expand Up @@ -94,6 +94,7 @@ def active_admin_import(options = {}, &block)
end
rescue ActiveRecord::Import::MissingColumnError, NoMethodError, ActiveRecord::StatementInvalid, CSV::MalformedCSVError => e
Rails.logger.error(I18n.t('active_admin_import.file_error', message: e.message))
Rails.logger.error(e.backtrace.join("\n"))
flash[:error] = I18n.t('active_admin_import.file_error', message: e.message[0..200])
end
redirect_to options[:back]
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin_import/importer.rb
Expand Up @@ -119,7 +119,7 @@ def detect_csv_options
model.csv_options
else
options[:csv_options] || {}
end.reject { |_, value| value.blank? }
end.reject { |_, value| value.nil? || value == "" }
end
end
end
4 changes: 4 additions & 0 deletions lib/active_admin_import/model.rb
Expand Up @@ -21,6 +21,10 @@ module CONST
application/csv
application/vnd.ms-excel
application/vnd.msexcel
text/tsv
text/x-tsv
text/tab-separated-values
text/x-tab-separated-values
).freeze
end

Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/files/authors_with_tabs.tsv
@@ -0,0 +1,3 @@
Name Last name Birthday
John Doe 1986-05-01
Jane Roe 1988-11-16
13 changes: 13 additions & 0 deletions spec/import_spec.rb
Expand Up @@ -397,6 +397,19 @@ def upload_file!(name, ext = 'csv')
expect(Author.count).to eq(2)
end
end

context 'with tab separator' do
let(:options) do
attributes = { csv_options: { col_sep: "\t" } }
{ template_object: ActiveAdminImport::Model.new(attributes) }
end

it 'should import file' do
upload_file!(:authors_with_tabs, 'tsv')
expect(page).to have_content 'Successfully imported 2 authors'
expect(Author.count).to eq(2)
end
end
end

context 'with callback procs options' do
Expand Down

0 comments on commit 885e742

Please sign in to comment.