Skip to content

Commit d5e504e

Browse files
committed
win1251 real specs
1 parent 50d055c commit d5e504e

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

lib/active_admin_import/model.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def encode_file
8585

8686
def uncompress_file
8787
Zip::File.open(file_path) do |zip_file|
88-
self.file = Tempfile.new("active-admin-import-unzipped")
88+
self.file = Tempfile.new('active-admin-import-unzipped')
8989
data = zip_file.entries.select { |f| f.file? }.first.get_input_stream.read
9090
self.file << data
9191
self.file.close
@@ -132,14 +132,12 @@ def define_methods_for(attr_name)
132132
end
133133

134134
def encode(data)
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
135+
data = data.force_encoding(force_encoding) if force_encoding?
136+
data = data.encode('UTF-8',
137+
invalid: :replace, undef: :replace)
140138
begin
141-
data.sub("\xEF\xBB\xBF", "") # bom
142-
rescue ArgumentError => _
139+
data.sub("\xEF\xBB\xBF", '') # bom
140+
rescue StandardError => _
143141
data
144142
end
145143
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Name,Last name,Birthday
2-
Алексей,Панкратов,1986-05-01
2+
�������,���������,1986-05-01
33
Jane,Roe,1988-11-16

spec/import_spec.rb

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def upload_file!(name, ext='csv')
9696
[:empty, :only_headers].each do |file|
9797
context "when #{file} file" do
9898
it "should render warning" do
99-
10099
upload_file!(file)
101100
expect(page).to have_content I18n.t('active_admin_import.file_empty_error')
102101
expect(Author.count).to eq(0)
@@ -113,6 +112,10 @@ def upload_file!(name, ext='csv')
113112
end
114113

115114
context "Win1251" do
115+
let(:options) do
116+
attributes = { force_encoding: "Windows-1251" }
117+
{ template_object: ActiveAdminImport::Model.new(attributes) }
118+
end
116119

117120
before do
118121
upload_file!(:authors_win1251_win_endline)
@@ -153,20 +156,24 @@ def upload_file!(name, ext='csv')
153156
expect(page).to have_content "Successfully imported 1 author"
154157
expect(Author.count).to eq(1)
155158
end
159+
156160
end
157161

158162

159163
context "without headers" do
160164
context "with known csv headers" do
161-
let(:options) {
162-
{template_object: ActiveAdminImport::Model.new(csv_headers: ['Name', 'Last name', 'Birthday'])}
163-
}
165+
166+
let(:options) do
167+
attributes = { csv_headers: ['Name', 'Last name', 'Birthday'] }
168+
{ template_object: ActiveAdminImport::Model.new(attributes) }
169+
end
164170

165171
it "should import file" do
166172
upload_file!(:authors_no_headers)
167173
expect(page).to have_content "Successfully imported 2 authors"
168174
expect(Author.count).to eq(2)
169175
end
176+
170177
end
171178

172179
context "with unknown csv headers" do
@@ -224,10 +231,10 @@ def upload_file!(name, ext='csv')
224231
end
225232

226233
context "when not allowed" do
227-
let(:options) { {
228-
template_object: ActiveAdminImport::Model.new(allow_archive: false)
229-
} }
230-
234+
let(:options) do
235+
attributes = { allow_archive: false }
236+
{ template_object: ActiveAdminImport::Model.new(attributes) }
237+
end
231238
it "should render error" do
232239
with_zipped_csv(:authors) do
233240
upload_file!(:authors, :zip)
@@ -240,11 +247,9 @@ def upload_file!(name, ext='csv')
240247

241248
context "with different header attribute names" do
242249

243-
let(:options) {
244-
{
245-
headers_rewrites: {:'Second name' => :last_name}
246-
}
247-
}
250+
let(:options) do
251+
{ headers_rewrites: { :'Second name' => :last_name } }
252+
end
248253

249254
it "should import file" do
250255
upload_file!(:author_broken_header)
@@ -254,9 +259,12 @@ def upload_file!(name, ext='csv')
254259
end
255260

256261
context "with semicolons separator" do
257-
let(:options) {
258-
{template_object: ActiveAdminImport::Model.new(csv_options: {col_sep: ";"})}
259-
}
262+
263+
let(:options) do
264+
attributes = { csv_options: { col_sep: ";" } }
265+
{ template_object: ActiveAdminImport::Model.new(attributes) }
266+
end
267+
260268
it "should import file" do
261269
upload_file!(:authors_with_semicolons)
262270
expect(page).to have_content "Successfully imported 2 authors"
@@ -268,12 +276,14 @@ def upload_file!(name, ext='csv')
268276

269277

270278
context "with callback procs options" do
271-
let(:options) { {
272-
before_import: proc { |_|},
273-
after_import: proc { |_|},
274-
before_batch_import: proc { |_|},
275-
after_batch_import: proc { |_|}
276-
} }
279+
let(:options) do
280+
{
281+
before_import: proc { |_| },
282+
after_import: proc { |_| },
283+
before_batch_import: proc { |_| },
284+
after_batch_import: proc { |_| }
285+
}
286+
end
277287

278288

279289
it "should call each callback" do

0 commit comments

Comments
 (0)