From 1f498e60524aee2a3a6a662bf8b9bcf12863d4f4 Mon Sep 17 00:00:00 2001 From: Vinicius Uehara Date: Sun, 30 Oct 2022 11:46:50 -0300 Subject: [PATCH 1/4] Improve shipping category not found error message --- app/models/product_import/entry_validator.rb | 2 +- config/locales/en.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index a759898e32..e08360113e 100644 --- a/app/models/product_import/entry_validator.rb +++ b/app/models/product_import/entry_validator.rb @@ -299,7 +299,7 @@ module ProductImport entry.public_send("#{type}_category_id=", index[category]) else mark_as_invalid(entry, attribute: "#{type}_category", - error: I18n.t('admin.product_import.model.not_found')) + error: I18n.t('admin.product_import.model.category_not_found')) end end diff --git a/config/locales/en.yml b/config/locales/en.yml index b618b535b6..23413e087a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -706,6 +706,7 @@ en: conditional_blank: can't be blank if unit_type is blank no_product: did not match any products in the database not_found: not found in database + category_not_found: doesn't match allowed categories. See the correct categories to choose from on the product import page, or check that there's no mispelling. not_updatable: cannot be updated on existing products via product import blank: can't be blank products_no_permission: you do not have permission to manage products for this enterprise From f6cc9fca26438b433440b67b38999a44237b50ee Mon Sep 17 00:00:00 2001 From: Vinicius Uehara Date: Sun, 30 Oct 2022 11:47:39 -0300 Subject: [PATCH 2/4] Prioritize attribute errors over product validations With product validations being prioritized, custom and insightful error messages were being overwriten by general and ambiguous model validations, which were confusing users --- .../product_import/spreadsheet_entry.rb | 2 +- spec/models/product_importer_spec.rb | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/product_import/spreadsheet_entry.rb b/app/models/product_import/spreadsheet_entry.rb index 14759b6ea7..b64314ecc5 100644 --- a/app/models/product_import/spreadsheet_entry.rb +++ b/app/models/product_import/spreadsheet_entry.rb @@ -68,7 +68,7 @@ module ProductImport def invalid_attributes invalid_attrs = {} - errors = @product_validations ? self.errors.messages.merge(@product_validations.messages) : self.errors.messages + errors = @product_validations ? @product_validations.messages.merge(self.errors.messages) : self.errors.messages errors.each do |attr, message| invalid_attrs[attr.to_s] = "#{attr.to_s.capitalize} #{message.first}" end diff --git a/spec/models/product_importer_spec.rb b/spec/models/product_importer_spec.rb index 3c6915297e..5188846632 100644 --- a/spec/models/product_importer_spec.rb +++ b/spec/models/product_importer_spec.rb @@ -272,6 +272,26 @@ describe ProductImport::ProductImporter do end end + describe "when shipping category is not found" do + let(:csv_data) { + CSV.generate do |csv| + csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type", + "variant_unit_name", "on_demand", "shipping_category"] + csv << ["Shipping Test", enterprise.name, "Vegetables", "5", "3.20", "500", "g", "", nil, + "not_found"] + end + } + let(:importer) { import_data csv_data } + + it "raises an error" do + importer.validate_entries + entries = JSON.parse(importer.entries_json) + error = entries['2']['errors']['shipping_category'] + + expect(error).to include "Shipping_category doesn't match allowed categories" + end + end + describe "when enterprises are not valid" do let(:csv_data) { CSV.generate do |csv| From 6da442eb97cfa8b4e4249a5ad30510f655dc3f83 Mon Sep 17 00:00:00 2001 From: Vinicius Uehara Date: Sun, 30 Oct 2022 21:42:48 -0300 Subject: [PATCH 3/4] Fix trailing whitespace --- spec/models/product_importer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/product_importer_spec.rb b/spec/models/product_importer_spec.rb index 5188846632..3b415bf84a 100644 --- a/spec/models/product_importer_spec.rb +++ b/spec/models/product_importer_spec.rb @@ -286,7 +286,7 @@ describe ProductImport::ProductImporter do it "raises an error" do importer.validate_entries entries = JSON.parse(importer.entries_json) - error = entries['2']['errors']['shipping_category'] + error = entries['2']['errors']['shipping_category'] expect(error).to include "Shipping_category doesn't match allowed categories" end From 5b44dc96097ec47baeedd6c55d13b8319640fd37 Mon Sep 17 00:00:00 2001 From: Konrad Date: Thu, 10 Nov 2022 20:53:30 +0100 Subject: [PATCH 4/4] Corrected missspelling of 'mispelling' in en.yml --- config/locales/en.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index 23413e087a..7eb7d0d0e9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -706,7 +706,7 @@ en: conditional_blank: can't be blank if unit_type is blank no_product: did not match any products in the database not_found: not found in database - category_not_found: doesn't match allowed categories. See the correct categories to choose from on the product import page, or check that there's no mispelling. + category_not_found: doesn't match allowed categories. See the correct categories to choose from on the product import page, or check that there's no misspelling. not_updatable: cannot be updated on existing products via product import blank: can't be blank products_no_permission: you do not have permission to manage products for this enterprise