Skip to content

armgit / spree

Public Imported from GitHub
0

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.

[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.rb
index 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 Spree
module Admin
class ImagesController < ResourceController
- include Spree::Admin::ProductConcern
-
- belongs_to 'spree/product', find_by: :slug
-
+ before_action :load_product
before_action :load_edit_data, except: :index
create.before :set_viewable
@@ -13,24 +10,44 @@ module Spree
private
def location_after_destroy
- spree.admin_product_images_url(parent)
+ spree.admin_product_images_url(@product)
end
def location_after_save
- spree.admin_product_images_url(parent)
+ spree.admin_product_images_url(@product)
end
def 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])
end
def 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
+ end
end
end
end
diff --git a/backend/spec/controllers/spree/admin/images_controller_spec.rb b/backend/spec/controllers/spree/admin/images_controller_spec.rb
index 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 Spree
let(:store) { Spree::Store.default }
let(:product) { create(:product, stores: [store]) }
+ let(:product_in_other_store) { create(:product, stores: [create(:store)]) }
describe '#index' do
let!(: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' do
get :index, params: { product_id: product.slug }
expect(assigns(:collection)).to include image_1
expect(assigns(:collection)).to include image_2
- expect(assigns(:collection)).not_to include image_3
+ expect(assigns(:collection)).not_to include image_4
end
end
@@ -43,6 +45,41 @@ module Spree
end
end
+ 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' do
let(:other_product) { create(:product, stores: [store]) }
let(:image) { create(:image, viewable: other_product) }
@@ -57,8 +94,12 @@ module Spree
context 'cannot destroy image of product from different store' do
let(: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')
+ end
end
end
@@ -82,6 +123,42 @@ module Spree
end
end
+ 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' do
let(:other_product) { create(:product, stores: [store]) }
let(:image) { create(:image, viewable: other_product) }
@@ -97,7 +174,12 @@ module Spree
context 'cannot destroy image of product from different store' do
let(: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')
+ end
end
end
end