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-1358] As a Admin I should not be able to permanently delete Store (#11315)

Damian Legawiecd477e42ba57b
diff --git a/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb
index 378825428a..8761f122cc 100644
--- a/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb
@@ -37,7 +37,8 @@ describe Spree::Api::V2::Platform::StoreSerializer do
contact_phone: store.contact_phone,
new_order_notifications_email: store.new_order_notifications_email,
seo_robots: store.seo_robots,
- supported_locales: store.supported_locales
+ supported_locales: store.supported_locales,
+ deleted_at: store.deleted_at
},
relationships: {
default_country: {
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 02889e3cac..b90b914a61 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -3,6 +3,8 @@ module Spree
MAILER_LOGO_CONTENT_TYPES = ['image/png', 'image/jpg', 'image/jpeg'].freeze
FAVICON_CONTENT_TYPES = ['image/png', 'image/x-icon', 'image/vnd.microsoft.icon'].freeze
+ acts_as_paranoid
+
has_many :orders, class_name: 'Spree::Order'
has_many :line_items, through: :orders, class_name: 'Spree::LineItem'
has_many :shipments, through: :orders, class_name: 'Spree::Shipment'
@@ -19,7 +21,7 @@ module Spree
has_many :menus, class_name: 'Spree::Menu'
has_many :menu_items, through: :menus, class_name: 'Spree::MenuItem'
- has_many :store_products, class_name: 'Spree::StoreProduct', dependent: :destroy
+ has_many :store_products, class_name: 'Spree::StoreProduct'
has_many :products, through: :store_products, class_name: 'Spree::Product'
has_many :product_properties, through: :products, class_name: 'Spree::ProductProperty'
has_many :variants, through: :products, class_name: 'Spree::Variant', source: :variants_including_master
diff --git a/core/db/migrate/20140309033438_create_store_from_preferences.rb b/core/db/migrate/20140309033438_create_store_from_preferences.rb
index 07a1ef1073..c149203821 100644
--- a/core/db/migrate/20140309033438_create_store_from_preferences.rb
+++ b/core/db/migrate/20140309033438_create_store_from_preferences.rb
@@ -7,6 +7,21 @@ class CreateStoreFromPreferences < ActiveRecord::Migration[4.2]
end
end
+ # workaround for missing deleted_at column added in later migrations
+ unless Spree::Store.column_names.include?('deleted_at')
+ ActiveRecord::Validations::UniquenessParanoiaValidator.module_eval do
+ def build_relation(klass, *args)
+ super
+ end
+ end
+
+ Spree::Store.class_eval do
+ def self.paranoia_scope
+ unscoped
+ end
+ end
+ end
+
preference_store = Spree::Preferences::Store.instance
if store = Spree::Store.where(default: true).first
store.meta_description = preference_store.get('spree/app_configuration/default_meta_description') {}
diff --git a/core/db/migrate/20210608045519_ensure_store_default_country_is_set.rb b/core/db/migrate/20210608045519_ensure_store_default_country_is_set.rb
index 6800f69852..22e7771faf 100644
--- a/core/db/migrate/20210608045519_ensure_store_default_country_is_set.rb
+++ b/core/db/migrate/20210608045519_ensure_store_default_country_is_set.rb
@@ -1,5 +1,14 @@
class EnsureStoreDefaultCountryIsSet < ActiveRecord::Migration[5.2]
def change
+ # workaround for missing deleted_at column added in later migrations
+ unless Spree::Store.column_names.include?('deleted_at')
+ ActiveRecord::Validations::UniquenessParanoiaValidator.module_eval do
+ def build_relation(klass, *args)
+ super
+ end
+ end
+ end
+
Spree::Store.find_each(&:save)
end
end
diff --git a/core/db/migrate/20210831090344_add_deleted_at_to_spree_stores.rb b/core/db/migrate/20210831090344_add_deleted_at_to_spree_stores.rb
new file mode 100644
index 0000000000..7f70289cd8
--- /dev/null
+++ b/core/db/migrate/20210831090344_add_deleted_at_to_spree_stores.rb
@@ -0,0 +1,8 @@
+class AddDeletedAtToSpreeStores < ActiveRecord::Migration[5.2]
+ def change
+ unless column_exists?(:spree_stores, :deleted_at)
+ add_column :spree_stores, :deleted_at, :datetime
+ add_index :spree_stores, :deleted_at
+ end
+ end
+end
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index 90db7b887c..4831b61997 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -615,4 +615,26 @@ describe Spree::Store, type: :model do
end
end
end
+
+ describe 'soft deletion' do
+ let!(:default_store) { create(:store) }
+
+ let(:store) { create(:store) }
+
+ it 'soft-deletes when destroy is called' do
+ store.destroy!
+ expect(store.deleted_at).not_to be_nil
+ end
+
+ context 'with associations' do
+ before do
+ store.products << create(:product)
+ end
+
+ it "doesn't destroy associations" do
+ associations = described_class.reflect_on_all_associations(:has_many)
+ expect(associations.select { |a| a.options[:dependent] }).to be_empty
+ end
+ end
+ end
end