From ab443fa50f5ab22ab27a85b0f9b3acfdab878ea3 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 22 Oct 2025 15:33:29 +1100 Subject: [PATCH] Refactor the clone template to use local variable and add define locals on the template --- .../admin/products_v3_controller.rb | 22 ++++++++++--------- .../_product_variant_row.html.haml | 2 +- .../admin/products_v3/clone.turbo_stream.haml | 14 +++++++----- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index 7656c8709f..c503b5e899 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -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 diff --git a/app/views/admin/products_v3/_product_variant_row.html.haml b/app/views/admin/products_v3/_product_variant_row.html.haml index 722abb2af7..616eb8268f 100644 --- a/app/views/admin/products_v3/_product_variant_row.html.haml +++ b/app/views/admin/products_v3/_product_variant_row.html.haml @@ -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", diff --git a/app/views/admin/products_v3/clone.turbo_stream.haml b/app/views/admin/products_v3/clone.turbo_stream.haml index 94d39c1acd..a4ba827897 100644 --- a/app/views/admin/products_v3/clone.turbo_stream.haml +++ b/app/views/admin/products_v3/clone.turbo_stream.haml @@ -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