From 4a38d7ef57402181dd54ff6b58ae8de8e86bd08e Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Thu, 12 Sep 2024 03:47:53 +0500 Subject: [PATCH 1/4] 12626: add explaination for clone failure --- .../admin/products_v3_controller.rb | 26 +++++++++++++++++-- config/locales/en.yml | 1 + spec/system/admin/products_v3/actions_spec.rb | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index 24ed032dcf..f9e7ca2e8d 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -8,6 +8,16 @@ module Admin before_action :init_filters_params before_action :init_pagination_params + VIEW_FIELD_NAME_MAPPER = { + name: I18n.t("admin.products_page.columns.name"), + sku: I18n.t('admin.products_page.columns.sku'), + variant_unit: I18n.t('admin.products_page.columns.unit_scale'), + unit_presentation: I18n.t('admin.products_page.columns.unit'), + supplier_id: I18n.t('admin.products_page.columns.producer'), + primary_taxon_id: I18n.t('admin.products_page.columns.category'), + tax_category_id: I18n.t('admin.products_page.columns.tax_category') + }.freeze + def index fetch_products render "index", locals: { producers:, categories:, tax_category_options:, flash: } @@ -84,8 +94,8 @@ module Admin @producer_options = producers @category_options = categories @tax_category_options = tax_category_options - rescue ActiveRecord::ActiveRecordError => _e - flash.now[:error] = t('.error') + rescue ActiveRecord::ActiveRecordError => e + flash.now[:error] = clone_error_message(e) status = :unprocessable_entity @product_index = "-1" # Create a unique enough index end @@ -209,6 +219,18 @@ module Admin params.permit(products: ::PermittedAttributes::Product.attributes) .to_h.with_indifferent_access end + + def clone_error_message(error) + case error + when ActiveRecord::RecordInvalid + invalid_field_names = error.record.errors.attribute_names + .map{ |field_name| VIEW_FIELD_NAME_MAPPER[field_name] }.join(', ') + + t('.invalid_fields_error', invalid_field_names:) + else + t('.error') + end + end end end # rubocop:enable Metrics/ClassLength diff --git a/config/locales/en.yml b/config/locales/en.yml index f34c6fc2c3..668eff9b17 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -937,6 +937,7 @@ en: clone: success: Successfully cloned the product error: Unable to clone the product + invalid_fields_error: "Product being cloned has invalid %{invalid_field_names}" product_import: title: Product Import file_not_found: File not found or could not be opened diff --git a/spec/system/admin/products_v3/actions_spec.rb b/spec/system/admin/products_v3/actions_spec.rb index e864218c1d..176afcd7ee 100644 --- a/spec/system/admin/products_v3/actions_spec.rb +++ b/spec/system/admin/products_v3/actions_spec.rb @@ -269,7 +269,7 @@ RSpec.describe 'As an enterprise user, I can manage my products' do click_product_clone "Apples" - expect(page).to have_content "Unable to clone the product" + expect(page).to have_content "Product being cloned has invalid Unit scale" within "table.products" do # Products does not include the cloned product. From c08683412c2426d7a36d30457651288075e47085 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Fri, 13 Sep 2024 01:43:37 +0500 Subject: [PATCH 2/4] 12626: add a fallback message --- app/controllers/admin/products_v3_controller.rb | 4 ++-- config/locales/en.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index f9e7ca2e8d..e2b6e4a9ad 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -223,10 +223,10 @@ module Admin def clone_error_message(error) case error when ActiveRecord::RecordInvalid - invalid_field_names = error.record.errors.attribute_names + field_names = error.record.errors.attribute_names .map{ |field_name| VIEW_FIELD_NAME_MAPPER[field_name] }.join(', ') - t('.invalid_fields_error', invalid_field_names:) + field_names.present? ? t('.invalid_fields_error', field_names:) : t('.error') else t('.error') end diff --git a/config/locales/en.yml b/config/locales/en.yml index 668eff9b17..9b7ff82838 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -937,7 +937,7 @@ en: clone: success: Successfully cloned the product error: Unable to clone the product - invalid_fields_error: "Product being cloned has invalid %{invalid_field_names}" + invalid_fields_error: "Product being cloned has invalid %{field_names}" product_import: title: Product Import file_not_found: File not found or could not be opened From 2a4d275f4b2113975739f1a75565977b188f2e2d Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Sat, 14 Sep 2024 02:44:43 +0500 Subject: [PATCH 3/4] 12626: use rails default error messages --- app/controllers/admin/products_v3_controller.rb | 15 +-------------- config/locales/en.yml | 2 +- spec/system/admin/products_v3/actions_spec.rb | 2 +- 3 files changed, 3 insertions(+), 16 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index e2b6e4a9ad..7436ceab99 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -8,16 +8,6 @@ module Admin before_action :init_filters_params before_action :init_pagination_params - VIEW_FIELD_NAME_MAPPER = { - name: I18n.t("admin.products_page.columns.name"), - sku: I18n.t('admin.products_page.columns.sku'), - variant_unit: I18n.t('admin.products_page.columns.unit_scale'), - unit_presentation: I18n.t('admin.products_page.columns.unit'), - supplier_id: I18n.t('admin.products_page.columns.producer'), - primary_taxon_id: I18n.t('admin.products_page.columns.category'), - tax_category_id: I18n.t('admin.products_page.columns.tax_category') - }.freeze - def index fetch_products render "index", locals: { producers:, categories:, tax_category_options:, flash: } @@ -223,10 +213,7 @@ module Admin def clone_error_message(error) case error when ActiveRecord::RecordInvalid - field_names = error.record.errors.attribute_names - .map{ |field_name| VIEW_FIELD_NAME_MAPPER[field_name] }.join(', ') - - field_names.present? ? t('.invalid_fields_error', field_names:) : t('.error') + error.record.errors.full_messages.to_sentence else t('.error') end diff --git a/config/locales/en.yml b/config/locales/en.yml index 9b7ff82838..1900bd01c5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -70,7 +70,7 @@ en: price: "Price" primary_taxon_id: "Product Category" shipping_category_id: "Shipping Category" - variant_unit: "Variant Unit" + variant_unit: "Unit Scale" variant_unit_name: "Variant Unit Name" unit_value: "Unit value" spree/variant: diff --git a/spec/system/admin/products_v3/actions_spec.rb b/spec/system/admin/products_v3/actions_spec.rb index 176afcd7ee..839c47f690 100644 --- a/spec/system/admin/products_v3/actions_spec.rb +++ b/spec/system/admin/products_v3/actions_spec.rb @@ -269,7 +269,7 @@ RSpec.describe 'As an enterprise user, I can manage my products' do click_product_clone "Apples" - expect(page).to have_content "Product being cloned has invalid Unit scale" + expect(page).to have_content "Unit Scale can't be blank" within "table.products" do # Products does not include the cloned product. From 9d5806b85881b87ea32db3bf6bf1c6cf740afef4 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Sat, 14 Sep 2024 18:10:58 +0500 Subject: [PATCH 4/4] 12626: remove invalid_fields_error locale --- config/locales/en.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index 1900bd01c5..3d23129e5e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -937,7 +937,6 @@ en: clone: success: Successfully cloned the product error: Unable to clone the product - invalid_fields_error: "Product being cloned has invalid %{field_names}" product_import: title: Product Import file_not_found: File not found or could not be opened