From f6cc9fca26438b433440b67b38999a44237b50ee Mon Sep 17 00:00:00 2001 From: Vinicius Uehara Date: Sun, 30 Oct 2022 11:47:39 -0300 Subject: [PATCH] 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|