Skip to content

Commit a9da685

Browse files
committed
Merge pull request #28 from Fivell/win1251
win1251 specs
2 parents 867e7de + 50d055c commit a9da685

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lib/active_admin_import/model.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,16 @@ def define_methods_for(attr_name)
132132
end
133133

134134
def encode(data)
135-
data.encode(force_encoding, "binary",
136-
invalid: :replace, undef: :replace, replace: "").
137-
sub("\xEF\xBB\xBF", "")
135+
data = data.force_encoding("UTF-8")
136+
unless data.valid_encoding?
137+
data = data.encode(force_encoding,
138+
invalid: :replace, undef: :replace, replace: "")
139+
end
140+
begin
141+
data.sub("\xEF\xBB\xBF", "") # bom
142+
rescue ArgumentError => _
143+
data
144+
end
138145
end
139146

140147
class <<self
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Name,Last name,Birthday
2+
Алексей,Панкратов,1986-05-01
3+
Jane,Roe,1988-11-16

spec/import_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ def upload_file!(name, ext='csv')
112112
end
113113
end
114114

115+
context "Win1251" do
116+
117+
before do
118+
upload_file!(:authors_win1251_win_endline)
119+
end
120+
121+
it "should import file with many records" do
122+
expect(page).to have_content "Successfully imported 2 authors"
123+
expect(Author.count).to eq(2)
124+
Author.all.each do |author|
125+
expect(author).to be_valid
126+
expect(author.name).to be_present
127+
expect(author.last_name).to be_present
128+
end
129+
end
130+
131+
end
132+
115133
context "BOM" do
116134

117135
it "should import file with many records" do

0 commit comments

Comments
 (0)