Skip to content

Commit

Permalink
fix importing csv with BOM
Browse files Browse the repository at this point in the history
  • Loading branch information
Fivell committed Jan 19, 2015
1 parent 3785366 commit 2b4221f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/active_admin_import/model.rb
@@ -1,12 +1,15 @@
# encoding: utf-8

module ActiveAdminImport
class Model

include ActiveModel::Model
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks

validates :file, presence: {message: Proc.new { I18n.t('active_admin_import.no_file_error') }},
unless: proc { |me| me.new_record? }
validates :file, presence: {
message: proc { I18n.t("active_admin_import.no_file_error") }
}, unless: proc { |me| me.new_record? }

validate :correct_content_type, if: proc { |me| me.file.present? }
validate :file_contents_present, if: proc { |me| me.file.present? }
Expand All @@ -23,7 +26,6 @@ def initialize(args={})
assign_attributes(default_attributes.merge(args), true)
end


def assign_attributes(args = {}, new_record = false)
@attributes.merge!(args)
@new_record = new_record
Expand Down Expand Up @@ -75,23 +77,21 @@ def file_path
end

def encode_file
data = File.read(file_path).encode(force_encoding, invalid: :replace, undef: :replace)
data = File.read(file_path)
File.open(file_path, 'w') do |f|
f.write(data)
f.write(encode(data))
end
end

def uncompress_file
Zip::File.open(file_path) do |zip_file|
self.file = Tempfile.new("active-admin-import-unzipped")
data = zip_file.entries.select { |f| f.file? }.first.get_input_stream.read
data = data.encode(force_encoding, invalid: :replace, undef: :replace) if self.force_encoding?
self.file << data
self.file.close
end
end


def csv_allowed_types
[
'text/csv',
Expand All @@ -103,7 +103,6 @@ def csv_allowed_types
]
end


def correct_content_type
unless file.blank? || file.is_a?(Tempfile)
errors.add(:file, I18n.t('active_admin_import.file_format_error')) unless csv_allowed_types.include? file_type
Expand Down Expand Up @@ -132,6 +131,11 @@ def define_methods_for(attr_name)
end
end

def encode(data)
data.encode(force_encoding, "binary",
invalid: :replace, undef: :replace, replace: "").
sub("\xEF\xBB\xBF", "")
end

class <<self
def define_set_method(attr_name)
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/files/authors_bom.csv
@@ -0,0 +1,3 @@
Name,Last name,Birthday
John,Doe,1986-05-01
Jane,Roe,1988-11-16
10 changes: 10 additions & 0 deletions spec/import_spec.rb
Expand Up @@ -112,6 +112,16 @@ def upload_file!(name, ext='csv')
end
end

context "BOM" do

it "should import file with many records" do
upload_file!(:authors_bom)
expect(page).to have_content "Successfully imported 2 authors"
expect(Author.count).to eq(2)
end

end

context "with headers" do

it "should import file with many records" do
Expand Down

0 comments on commit 2b4221f

Please sign in to comment.