diff --git a/app/models/product_import/product_importer.rb b/app/models/product_import/product_importer.rb index e33b8bbb9b..7d0758333d 100644 --- a/app/models/product_import/product_importer.rb +++ b/app/models/product_import/product_importer.rb @@ -233,6 +233,8 @@ module ProductImport def rows return [] unless @sheet&.last_row + @sheet.parse(clean: true) + (2..@sheet.last_row).map do |i| @sheet.row(i) end diff --git a/spec/models/product_importer_spec.rb b/spec/models/product_importer_spec.rb index 58027d5748..87eff08c04 100644 --- a/spec/models/product_importer_spec.rb +++ b/spec/models/product_importer_spec.rb @@ -257,6 +257,32 @@ describe ProductImport::ProductImporter do end end + describe "when uploading a spreadsheet with unnecessary leading and trailing whitespace" do + let(:csv_data) { + CSV.generate do |csv| + csv << [" name ", "\nproducer\n", "\rcategory\r", "\ton_hand\t", + " price ", " units ", " unit_type ", " shipping_category "] + csv << [" Good Carrots ", "\n#{enterprise.name}\n", "\rVegetables\r", "\t5\t", + " 3.20 ", " 500 ", " g ", " #{shipping_category.name} "] + end + } + let(:importer) { import_data csv_data } + + it "ignores unnecessary leading and trailing whitespace in headers and rows" do + importer.save_entries + + expect(importer.products_created_count).to eq 1 + + carrots = Spree::Product.find_by(name: 'Good Carrots') + expect(carrots.on_hand).to eq 5 + expect(carrots.price).to eq 3.20 + expect(carrots.primary_taxon.name).to eq "Vegetables" + expect(carrots.shipping_category).to eq shipping_category + expect(carrots.supplier).to eq enterprise + expect(carrots.variants.first.unit_presentation).to eq "500g" + end + end + describe "when shipping category is missing" do let(:csv_data) { CSV.generate do |csv| @@ -724,6 +750,31 @@ describe ProductImport::ProductImporter do expect(visible).to be_truthy end end + + describe "when headers or rows contain unnecessary leading or trailing whitespace" do + let(:csv_data) { + CSV.generate do |csv| + csv << [" name ", "\ndisplay_name\n", "\rdistributor\r", "\tproducer\t", + " on_hand ", " price ", " units "] + csv << [" Oats ", "\nPorridge Oats\n", "\r#{enterprise2.name}\r", "\t#{enterprise.name}\t", + " 900 ", " 1.0 ", " 500 "] + end + } + let(:importer) { import_data csv_data, import_into: 'inventories' } + + it "ignores unnecessary leading and trailing whitespace in headers and rows" do + importer.save_entries + + expect(importer.inventory_created_count).to eq 1 + + override = VariantOverride.where(variant_id: variant2.id, hub_id: enterprise2.id).first + visible = InventoryItem.where(variant_id: variant2.id, + enterprise_id: enterprise2.id).first.visible + + expect(override.count_on_hand).to eq 900 + expect(visible).to be_truthy + end + end end describe "handling enterprise permissions" do