12626: add explaination for clone failure

This commit is contained in:
Ahmed Ejaz
2024-09-12 03:47:53 +05:00
parent 68491559f3
commit 4a38d7ef57
3 changed files with 26 additions and 3 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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.