Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
[SD-1460] Fix - Deleting Image Belongs To Product and Variant in Admin Panel (#11332)
Matthew Kennedy5941de4ca50b
diff --git a/backend/app/controllers/spree/admin/images_controller.rb b/backend/app/controllers/spree/admin/images_controller.rbindex d765abb033..50a1123968 100644--- a/backend/app/controllers/spree/admin/images_controller.rb+++ b/backend/app/controllers/spree/admin/images_controller.rb@@ -1,10 +1,7 @@module Spreemodule Adminclass ImagesController < ResourceController- include Spree::Admin::ProductConcern-- belongs_to 'spree/product', find_by: :slug-+ before_action :load_productbefore_action :load_edit_data, except: :indexcreate.before :set_viewable@@ -13,24 +10,44 @@ module Spreeprivatedef location_after_destroy- spree.admin_product_images_url(parent)+ spree.admin_product_images_url(@product)enddef location_after_save- spree.admin_product_images_url(parent)+ spree.admin_product_images_url(@product)enddef load_edit_data- @variants = parent.variants.map do |variant|+ @variants = @product.variants.map do |variant|[variant.sku_and_options_text, variant.id]end- @variants.insert(0, [Spree.t(:all), parent.master.id])+ @variants.insert(0, [Spree.t(:all), @product.master_id])enddef set_viewable@image.viewable_type = 'Spree::Variant'@image.viewable_id = params[:image][:viewable_id]end++ def load_product+ @product = scope.friendly.find(params[:product_id])+ end++ def scope+ current_store.products+ end++ def collection_url+ spree.admin_product_images_url+ end++ def modle_class+ Spree::Image+ end++ def collection+ @collection ||= load_product.variant_images+ endendendenddiff --git a/backend/spec/controllers/spree/admin/images_controller_spec.rb b/backend/spec/controllers/spree/admin/images_controller_spec.rbindex 8e77da7027..a9789abd8c 100644--- a/backend/spec/controllers/spree/admin/images_controller_spec.rb+++ b/backend/spec/controllers/spree/admin/images_controller_spec.rb@@ -7,17 +7,19 @@ module Spreelet(:store) { Spree::Store.default }let(:product) { create(:product, stores: [store]) }+ let(:product_in_other_store) { create(:product, stores: [create(:store)]) }describe '#index' dolet!(:image_1) { create(:image, viewable: product.master) }let!(:image_2) { create(:image, viewable: product.master) }let!(:image_3) { create(:image, viewable: create(:variant)) }+ let!(:image_4) { create(:image, viewable: product_in_other_store.master) }it 'assigns the images for a requested product' doget :index, params: { product_id: product.slug }expect(assigns(:collection)).to include image_1expect(assigns(:collection)).to include image_2- expect(assigns(:collection)).not_to include image_3+ expect(assigns(:collection)).not_to include image_4endend@@ -43,6 +45,41 @@ module Spreeendend+ context 'can destroy image belonging to a variant not master_variant' do+ let(:variant) { create(:variant) }+ let(:product) { variant.product }+ let(:image) { create(:image, viewable: variant) }++ before do+ product.update(stores: [store])+ product.save+ product.reload+ end++ it do+ send_request+ expect(flash[:error]).not_to eq('Image is not found')+ expect(flash[:success]).to eq('Image has been successfully removed!')+ end+ end++ context 'can not destroy image belonging to a variant not master_variant but also belongs to other store' do+ let(:variant) { create(:variant) }+ let(:product) { variant.product }+ let(:image) { create(:image, viewable: variant) }++ before do+ product.update(stores: [create(:store)])+ product.save+ product.reload+ end++ it do+ send_request+ expect(flash[:error]).to eq('Image is not found')+ end+ end+context 'cannot destroy image of other product' dolet(:other_product) { create(:product, stores: [store]) }let(:image) { create(:image, viewable: other_product) }@@ -57,8 +94,12 @@ module Spreecontext 'cannot destroy image of product from different store' dolet(:product) { create(:product, stores: [create(:store)]) }+ before { send_request }- it { expect { send_request }.to raise_error(ActiveRecord::RecordNotFound) }+ it do+ expect(send_request).to redirect_to(spree.admin_product_images_path(product))+ expect(flash[:error]).to eq('Image is not found')+ endendend@@ -82,6 +123,42 @@ module Spreeendend+ context 'can destroy image belonging to a variant not master_variant' do+ let(:variant) { create(:variant) }+ let(:product) { variant.product }+ let(:image) { create(:image, viewable: variant) }++ before do+ product.update(stores: [store])+ product.save+ product.reload+ end++ it do+ send_request+ expect(flash[:error]).not_to eq('Image is not found')+ expect(flash[:success]).to eq('Image has been successfully removed!')+ expect(send_request).to redirect_to(spree.admin_product_images_path(product))+ end+ end++ context 'can not destroy image belonging to a variant not master_variant but also belongs to other store' do+ let(:variant) { create(:variant) }+ let(:product) { variant.product }+ let(:image) { create(:image, viewable: variant) }++ before do+ product.update(stores: [create(:store)])+ product.save+ product.reload+ end++ it do+ send_request+ expect(flash[:error]).to eq('Image is not found')+ end+ end+context 'cannot destroy image of other product' dolet(:other_product) { create(:product, stores: [store]) }let(:image) { create(:image, viewable: other_product) }@@ -97,7 +174,12 @@ module Spreecontext 'cannot destroy image of product from different store' dolet(:product) { create(:product, stores: [create(:store)]) }- it { expect { send_request }.to raise_error(ActiveRecord::RecordNotFound) }+ before { send_request }++ it do+ expect(send_request).to redirect_to(spree.admin_product_images_path(product))+ expect(flash[:error]).to eq('Image is not found')+ endendendend