From 38519b2baeabab27fa9c7097d9130d461f0da5a7 Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Fri, 7 Jun 2019 02:59:31 +0800 Subject: [PATCH] Simplify building of product import entries for range --- app/models/product_import/product_importer.rb | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/app/models/product_import/product_importer.rb b/app/models/product_import/product_importer.rb index 5d8a3176d1..6d5e864393 100644 --- a/app/models/product_import/product_importer.rb +++ b/app/models/product_import/product_importer.rb @@ -238,18 +238,14 @@ module ProductImport end def build_entries_in_range - start_line = @import_settings[:start] - end_line = @import_settings[:end] + # In the JS, start and end are calculated like this: + # start = (batchIndex * $scope.batchSize) + 1 + # end = (batchIndex + 1) * $scope.batchSize + start_data_index = @import_settings[:start] - 1 + end_data_index = @import_settings[:end] - 1 - (start_line..end_line).each do |i| - line_number = i + 1 - row = @sheet.row(line_number) - row_data = Hash[[headers, row].transpose] - entry = SpreadsheetEntry.new(row_data) - entry.line_number = line_number - @entries.push entry - break if @sheet.last_row == line_number - end + data_rows = rows[start_data_index..end_data_index] + @entries = build_entries_from_rows(data_rows, start_data_index) end def build_entries @@ -267,11 +263,11 @@ module ProductImport File.delete(@file) end - def build_entries_from_rows(rows) + def build_entries_from_rows(rows, offset = 0) rows.each_with_index.inject([]) do |entries, (row, i)| row_data = Hash[[headers, row].transpose] entry = SpreadsheetEntry.new(row_data) - entry.line_number = i + 2 + entry.line_number = offset + i + 2 entries.push entry end end