Skip to content

Commit 2b4221f

Browse files
committed
fix importing csv with BOM
1 parent 3785366 commit 2b4221f

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

lib/active_admin_import/model.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
# encoding: utf-8
2+
13
module ActiveAdminImport
24
class Model
35

46
include ActiveModel::Model
57
include ActiveModel::Validations
68
include ActiveModel::Validations::Callbacks
79

8-
validates :file, presence: {message: Proc.new { I18n.t('active_admin_import.no_file_error') }},
9-
unless: proc { |me| me.new_record? }
10+
validates :file, presence: {
11+
message: proc { I18n.t("active_admin_import.no_file_error") }
12+
}, unless: proc { |me| me.new_record? }
1013

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

26-
2729
def assign_attributes(args = {}, new_record = false)
2830
@attributes.merge!(args)
2931
@new_record = new_record
@@ -75,23 +77,21 @@ def file_path
7577
end
7678

7779
def encode_file
78-
data = File.read(file_path).encode(force_encoding, invalid: :replace, undef: :replace)
80+
data = File.read(file_path)
7981
File.open(file_path, 'w') do |f|
80-
f.write(data)
82+
f.write(encode(data))
8183
end
8284
end
8385

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

94-
9595
def csv_allowed_types
9696
[
9797
'text/csv',
@@ -103,7 +103,6 @@ def csv_allowed_types
103103
]
104104
end
105105

106-
107106
def correct_content_type
108107
unless file.blank? || file.is_a?(Tempfile)
109108
errors.add(:file, I18n.t('active_admin_import.file_format_error')) unless csv_allowed_types.include? file_type
@@ -132,6 +131,11 @@ def define_methods_for(attr_name)
132131
end
133132
end
134133

134+
def encode(data)
135+
data.encode(force_encoding, "binary",
136+
invalid: :replace, undef: :replace, replace: "").
137+
sub("\xEF\xBB\xBF", "")
138+
end
135139

136140
class <<self
137141
def define_set_method(attr_name)
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ def upload_file!(name, ext='csv')
112112
end
113113
end
114114

115+
context "BOM" do
116+
117+
it "should import file with many records" do
118+
upload_file!(:authors_bom)
119+
expect(page).to have_content "Successfully imported 2 authors"
120+
expect(Author.count).to eq(2)
121+
end
122+
123+
end
124+
115125
context "with headers" do
116126

117127
it "should import file with many records" do

0 commit comments

Comments
 (0)