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
Adds ability to (softly) delete default store (#11513)
Damian Legawiecd91558a95dee
diff --git a/api/spec/controllers/spree/api/v1/stores_controller_spec.rb b/api/spec/controllers/spree/api/v1/stores_controller_spec.rbindex be64fad6f3..e2b0e86bf3 100644--- a/api/spec/controllers/spree/api/v1/stores_controller_spec.rb+++ b/api/spec/controllers/spree/api/v1/stores_controller_spec.rb@@ -118,7 +118,7 @@ module Spreeexpect(store.reload.customer_support_email).to eql '[email protected]'end- context 'deleting a store' do+ xcontext 'deleting a store' doit "will fail if it's the default Store" doapi_delete :destroy, id: store.idexpect(response.status).to eq(422)diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex c6e91b8b16..6f64abfee2 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -60,6 +60,7 @@ module Spreevalidates :digital_asset_authorized_days, numericality: { only_integer: true, greater_than: 0 }validates :code, uniqueness: { conditions: -> { with_deleted } }+ # FIXME: we should remove this condition in v5if !ENV['SPREE_DISABLE_DB_CONNECTION'] &&connected? &&table_exists? &&@@ -81,7 +82,8 @@ module Spreebefore_save :ensure_default_exists_and_is_uniquebefore_save :ensure_supported_currencies, :ensure_supported_locales, :ensure_default_country- before_destroy :validate_not_default+ before_destroy :validate_not_last+ before_destroy :pass_default_flag_to_other_storescope :by_url, ->(url) { where('url like ?', "%#{url}%") }@@ -97,6 +99,8 @@ module SpreeStores::FindCurrent.new(url: url).executeend+ # FIXME: we need to drop `or_initialize` in v5+ # this behaviour is very buggy and unpredictabledef self.defaultRails.cache.fetch('default_store') dowhere(default: true).first_or_initialize@@ -176,6 +180,10 @@ module Spreefavicon_image.variant(resize: '32x32')end+ def can_be_deleted?+ self.class.where.not(id: id).any?+ end+privatedef ensure_default_exists_and_is_unique@@ -202,13 +210,20 @@ module Spreeself.supported_currencies = default_currencyend- def validate_not_default- if default- errors.add(:base, :cannot_destroy_default_store)+ def validate_not_last+ unless can_be_deleted?+ errors.add(:base, :cannot_destroy_only_store)throw(:abort)endend+ def pass_default_flag_to_other_store+ if default? && can_be_deleted?+ self.class.where.not(id: id).first.update!(default: true)+ self.default = false+ end+ end+def clear_cacheRails.cache.delete('default_store')Rails.cache.delete('stores_available_locales')diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 88c2677983..a526baf2a3 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -382,7 +382,7 @@ en:spree/store:attributes:base:- cannot_destroy_default_store: Cannot destroy the default Store.+ cannot_destroy_only_store: Cannot destroy the only Store.spree/store_credit:attributes:amount_used:diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rbindex b5b449b515..aaa480adc2 100644--- a/core/spec/models/spree/store_spec.rb+++ b/core/spec/models/spree/store_spec.rb@@ -621,19 +621,60 @@ describe Spree::Store, type: :model doendend+ describe '#can_be_deleted?' do+ let(:default_store) { Spree::Store.default }++ it 'cannot delete the only store' do+ expect(default_store.can_be_deleted?).to eq(false)+ end++ it 'can delete when there are more than 1 stores' do+ create(:store)+ expect(default_store.can_be_deleted?).to eq(true)+ end+ end+describe 'soft deletion' do- let!(:default_store) { create(:store) }+ let!(:default_store) { described_class.default }+ let(:another_store) { create(:store) }++ context 'default store' do+ context 'with multiple stores' do+ before { another_store }++ it 'can be deleted' do+ expect(default_store.deleted?).to eq(false)+ expect { default_store.destroy }.to change(default_store, :deleted_at)+ expect(default_store.deleted?).to eq(true)+ end++ it 'passes default flag to other store' do+ expect(another_store.default?).to eq(false)+ default_store.destroy+ expect(default_store.default?).to eq(false)+ expect(another_store.reload.default?).to eq(true)+ expect(described_class.default).to eq(another_store)+ end+ end- let(:store) { create(:store) }+ context 'single store' do+ it 'cannot be deleted' do+ expect { default_store.destroy! }.to raise_error(ActiveRecord::RecordNotDestroyed)+ expect(default_store.errors.full_messages.to_sentence).to eq('Cannot destroy the only Store.')+ end+ end+ end- it 'soft-deletes when destroy is called' do- store.destroy!- expect(store.deleted_at).not_to be_nil+ context 'another store' do+ it 'soft-deletes when destroy is called' do+ another_store.destroy!+ expect(another_store.deleted_at).not_to be_nil+ endendcontext 'with associations' dobefore do- store.products << create(:product)+ another_store.products << create(:product)endit "doesn't destroy associations" do