Skip to content

Commit

Permalink
win1251 specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Fivell committed Jan 19, 2015
1 parent 867e7de commit 50d055c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/active_admin_import/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,16 @@ def define_methods_for(attr_name)
end

def encode(data)
data.encode(force_encoding, "binary",
invalid: :replace, undef: :replace, replace: "").
sub("\xEF\xBB\xBF", "")
data = data.force_encoding("UTF-8")
unless data.valid_encoding?
data = data.encode(force_encoding,
invalid: :replace, undef: :replace, replace: "")
end
begin
data.sub("\xEF\xBB\xBF", "") # bom
rescue ArgumentError => _
data
end
end

class <<self
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/files/authors_win1251_win_endline.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name,Last name,Birthday
Алексей,Панкратов,1986-05-01
Jane,Roe,1988-11-16
18 changes: 18 additions & 0 deletions spec/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ def upload_file!(name, ext='csv')
end
end

context "Win1251" do

before do
upload_file!(:authors_win1251_win_endline)
end

it "should import file with many records" do
expect(page).to have_content "Successfully imported 2 authors"
expect(Author.count).to eq(2)
Author.all.each do |author|
expect(author).to be_valid
expect(author.name).to be_present
expect(author.last_name).to be_present
end
end

end

context "BOM" do

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

0 comments on commit 50d055c

Please sign in to comment.