Skip to content

Commit

Permalink
12626: add explaination for clone failure
Browse files Browse the repository at this point in the history
  • Loading branch information
chahmedejaz committed Sep 11, 2024
1 parent 6849155 commit 4a38d7e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
26 changes: 24 additions & 2 deletions app/controllers/admin/products_v3_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/system/admin/products_v3/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4a38d7e

Please sign in to comment.