Refactor the clone template to use local variable

and add define locals on the template
This commit is contained in:
Gaetan Craig-Riou
2025-10-22 15:33:29 +11:00
parent 25d55fec24
commit ab443fa50f
3 changed files with 21 additions and 17 deletions

View File

@@ -34,7 +34,7 @@ module Admin
render "index", status: :unprocessable_entity,
locals: {
producer_options:, categories:, tax_category_options:, available_tags:,
flash:
allowed_producers:, flash:
}
end
end
@@ -80,27 +80,29 @@ module Admin
end
def clone
@product = Spree::Product.find(params[:id])
authorize! :clone, @product
product = Spree::Product.find(params[:id])
authorize! :clone, product
status = :ok
begin
@cloned_product = @product.duplicate
cloned_product = product.duplicate
flash.now[:success] = t('.success')
@product_index = "-#{@cloned_product.id}"
@producer_options = producer_options
@category_options = categories
@tax_category_options = tax_category_options
product_index = "-#{cloned_product.id}"
rescue ActiveRecord::ActiveRecordError => e
flash.now[:error] = clone_error_message(e)
status = :unprocessable_entity
@product_index = "-1" # Create a unique enough index
product_index = "-1" # Create a unique enough index
end
respond_with do |format|
format.turbo_stream { render :clone, status: }
format.turbo_stream {
render :clone, status:,
locals: { product:, cloned_product:, product_index:, producer_options:,
category_options: categories, tax_category_options:,
allowed_producers: }
}
end
end

View File

@@ -1,4 +1,4 @@
-# locals: (form:, product:, product_index:, producer_options:, category_options:, tax_category_options:, allowed_producers:)
-# locals: (form:, product:, product_index:, producer_options:, category_options:, tax_category_options:, allowed_producers:, should_slide_in: false)
= form.fields_for("products", product, index: product_index) do |product_form|
%tbody.relaxed{ id: dom_id(product), data: { 'record-id': product_form.object.id,
controller: "nested-form product",

View File

@@ -1,16 +1,18 @@
-# locals: (product:, cloned_product:, product_index:, producer_options:, category_options: category, tax_category_options:, allowed_producers:)
- unless flash[:error]
- product_body = nil
- form_with do |form|
- product_body = render(partial: 'product_variant_row',
locals: { form:,
product: @cloned_product,
product_index: @product_index,
producer_options: @producer_options,
category_options: @category_options,
tax_category_options: @tax_category_options,
product: cloned_product,
product_index:,
producer_options:,
category_options:,
tax_category_options:,
allowed_producers:,
should_slide_in: true },
formats: :html)
= turbo_stream.after dom_id(@product) do
= turbo_stream.after dom_id(product) do
= product_body
= turbo_stream.append "flashes" do