diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index 24ed032dcf6..f9e7ca2e8d8 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -8,6 +8,16 @@ class ProductsV3Controller < Spree::Admin::BaseController 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 @@ def clone @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 @@ def products_bulk_params 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 f34c6fc2c32..668eff9b173 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 e864218c1d7..176afcd7eea 100644 --- a/spec/system/admin/products_v3/actions_spec.rb +++ b/spec/system/admin/products_v3/actions_spec.rb @@ -269,7 +269,7 @@ def expect_other_columns_visible 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.