Skip to content

Commit 5ccf95c

Browse files
committed
Allow batch_slice_columns to work when batching
Currently the `use_indexes` are set correctly only for the first batch.
1 parent 7e17eb6 commit 5ccf95c

3 files changed

Lines changed: 38 additions & 19 deletions

File tree

lib/active_admin_import/importer.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def batch_replace(header_key, options)
7070
end
7171
end
7272

73-
# Use it when CSV file contain redundant columns
73+
# Use this method when CSV file contains unnecessary columns
7474
#
7575
# Example:
7676
#
@@ -81,16 +81,22 @@ def batch_replace(header_key, options)
8181
# end
8282
#
8383
def batch_slice_columns(slice_columns)
84-
use_indexes = []
85-
headers.values.each_with_index do |val, index|
86-
use_indexes << index if val.in?(slice_columns)
84+
# Only set @use_indexes for the first batch so that @use_indexes are in correct
85+
# position for subsequent batches
86+
unless defined?(@use_indexes)
87+
@use_indexes = []
88+
headers.values.each_with_index do |val, index|
89+
@use_indexes << index if val.in?(slice_columns)
90+
end
91+
return csv_lines if @use_indexes.empty?
92+
93+
# slice CSV headers
94+
@headers = headers.to_a.values_at(*@use_indexes).to_h
8795
end
88-
return csv_lines if use_indexes.empty?
89-
# slice CSV headers
90-
@headers = headers.to_a.values_at(*use_indexes).to_h
96+
9197
# slice CSV values
9298
csv_lines.map! do |line|
93-
line.values_at(*use_indexes)
99+
line.values_at(*@use_indexes)
94100
end
95101
end
96102

spec/fixtures/files/authors.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Name,Last name,Birthday
2-
John,Doe,1986-05-01
3-
Jane,Roe,1988-11-16
1+
Birthday,Name,Last name
2+
1986-05-01,John,Doe
3+
1988-11-16,Jane,Roe

spec/import_spec.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,25 +434,38 @@ def upload_file!(name, ext = 'csv')
434434
end
435435

436436
context "with slice_columns option" do
437+
let(:batch_size) { 2 }
438+
437439
before do
438440
add_author_resource template_object: ActiveAdminImport::Model.new,
439441
before_batch_import: lambda { |importer|
440442
importer.batch_slice_columns(slice_columns)
441-
}
443+
},
444+
batch_size: batch_size
442445
visit "/admin/authors/import"
443446
upload_file!(:authors)
444447
end
445448

446449
context "slice last column and superfluous column" do
447450
let(:slice_columns) { %w(name last_name not_existing_column) }
448451

449-
it "should not fill `birthday` column" do
450-
expect(Author.pluck(:name, :last_name, :birthday)).to match_array(
451-
[
452-
["Jane", "Roe", nil],
453-
["John", "Doe", nil]
454-
]
455-
)
452+
shared_examples_for "birthday column removed" do
453+
it "should not fill `birthday` column" do
454+
expect(Author.pluck(:name, :last_name, :birthday)).to match_array(
455+
[
456+
["Jane", "Roe", nil],
457+
["John", "Doe", nil]
458+
]
459+
)
460+
end
461+
end
462+
463+
it_behaves_like "birthday column removed"
464+
465+
context "when doing more than one batch" do
466+
let(:batch_size) { 1 }
467+
468+
it_behaves_like "birthday column removed"
456469
end
457470
end
458471

0 commit comments

Comments
 (0)