From c5decfc58b706ec096ddd871b070da5af0f4401b Mon Sep 17 00:00:00 2001 From: cyrillefr Date: Wed, 12 Jun 2024 00:31:19 +0200 Subject: [PATCH] Bugfix introduced by last commit - fixed bug created by last commit - refactored to new method respond_with_error - need for 2 cases in request spec: html & turbo --- .../spree/admin/images_controller.rb | 18 ++++++++++-------- spec/requests/admin/images_spec.rb | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/controllers/spree/admin/images_controller.rb b/app/controllers/spree/admin/images_controller.rb index ee11279af4..b58bcf39e5 100644 --- a/app/controllers/spree/admin/images_controller.rb +++ b/app/controllers/spree/admin/images_controller.rb @@ -40,10 +40,7 @@ module Spree redirect_to location_after_save rescue ActiveRecord::RecordInvalid => e - @errors = e.record.errors.map(&:full_message) - respond_to do |format| - format.turbo_stream { render :edit } - end + respond_with_error(e) end def update @@ -55,10 +52,7 @@ module Spree redirect_to location_after_save rescue ActiveRecord::RecordInvalid => e - @errors = e.record.errors.map(&:full_message) - respond_with do |format| - format.turbo_stream { render :edit } - end + respond_with_error(e) end def destroy @@ -108,6 +102,14 @@ module Spree :attachment, :viewable_id, :alt ) end + + def respond_with_error(error) + @errors = error.record.errors.map(&:full_message) + respond_to do |format| + format.html { respond_with(@object) } + format.turbo_stream { render :edit } + end + end end end end diff --git a/spec/requests/admin/images_spec.rb b/spec/requests/admin/images_spec.rb index 5193122060..0fa5a08c76 100644 --- a/spec/requests/admin/images_spec.rb +++ b/spec/requests/admin/images_spec.rb @@ -55,12 +55,26 @@ RSpec.describe "/admin/products/:product_id/images", type: :request do end describe "POST /admin/products/:product_id/images" do + subject { post(spree.admin_product_images_path(product), params:) } + + it_behaves_like "updating images" + end + describe "POST /admin/products/:product_id/images with turbo" do subject { post(spree.admin_product_images_path(product), params:, as: :turbo_stream) } it_behaves_like "updating images" end describe "PATCH /admin/products/:product_id/images/:id" do + let!(:product) { create(:product_with_image) } + subject { + patch(spree.admin_product_image_path(product, product.image), params:) + } + + it_behaves_like "updating images" + end + + describe "PATCH /admin/products/:product_id/images/:id with turbo" do let!(:product) { create(:product_with_image) } subject { patch(spree.admin_product_image_path(product, product.image), params:, as: :turbo_stream)