diff --git a/app/models/product_import/product_importer.rb b/app/models/product_import/product_importer.rb index f83f2102a2..85600d0407 100644 --- a/app/models/product_import/product_importer.rb +++ b/app/models/product_import/product_importer.rb @@ -238,16 +238,20 @@ module ProductImport end [] rescue CSV::MalformedCSVError => e - # NOTE the init_product_importer method calls build_entries and - # buils_all_entries resulting in the error being raised twice - unless errors.added?(:importer, I18n.t('admin.product_import.model.malformed_csv', - error_message: e.message)) - errors.add(:importer, I18n.t('admin.product_import.model.malformed_csv', - error_message: e.message)) - end + add_malformed_csv_error e.message [] end + # This error is raised twice because init_product_importer calls both + # build_entries and buils_all_entries + def add_malformed_csv_error(error_message) + unless errors.added?(:importer, I18n.t('admin.product_import.model.malformed_csv', + error_message: error_message)) + errors.add(:importer, I18n.t('admin.product_import.model.malformed_csv', + error_message: error_message)) + end + end + def build_entries_in_range # In the JS, start and end are calculated like this: # start = (batchIndex * $scope.batchSize) + 1 diff --git a/spec/models/product_importer_spec.rb b/spec/models/product_importer_spec.rb index 6f4903ba86..5ef78629e2 100644 --- a/spec/models/product_importer_spec.rb +++ b/spec/models/product_importer_spec.rb @@ -170,7 +170,7 @@ describe ProductImport::ProductImporter do end describe "when uploading a spreadsheet with some malformed data" do - # Use a simple string as CVS.generate will do some escaping + # Use a simple string as CSV.generate will do some escaping let(:csv_data) { csv = "name,producer,category,on_hand,price,units,unit_type,shipping_category\n" csv += "Good Carrots,#{enterprise.name},Vegetables,5,3.20,500,g,#{shipping_category.name}\n" @@ -178,8 +178,8 @@ describe ProductImport::ProductImporter do } let(:importer) { import_data csv_data } - # NOTE an unquoted \n will create a non valid line which will fail entry validation hence why we are only testing with \r - it "should raise an unquoted field error if data include unquoted filed with \r character" do + # an unquoted \n will create a non valid line which will fail entry validation hence why we are only testing with \r + it "should raise an unquoted field error if data include unquoted field with \r character" do expect(importer.errors.messages.values).to include( [I18n.t('admin.product_import.model.malformed_csv', error_message: "Unquoted fields do not allow \\r or \\n (line 3).")] )