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.

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.rb
index 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 Spree
expect(store.reload.customer_support_email).to eql '[email protected]'
end
- context 'deleting a store' do
+ xcontext 'deleting a store' do
it "will fail if it's the default Store" do
api_delete :destroy, id: store.id
expect(response.status).to eq(422)
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index c6e91b8b16..6f64abfee2 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -60,6 +60,7 @@ module Spree
validates :digital_asset_authorized_days, numericality: { only_integer: true, greater_than: 0 }
validates :code, uniqueness: { conditions: -> { with_deleted } }
+ # FIXME: we should remove this condition in v5
if !ENV['SPREE_DISABLE_DB_CONNECTION'] &&
connected? &&
table_exists? &&
@@ -81,7 +82,8 @@ module Spree
before_save :ensure_default_exists_and_is_unique
before_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_store
scope :by_url, ->(url) { where('url like ?', "%#{url}%") }
@@ -97,6 +99,8 @@ module Spree
Stores::FindCurrent.new(url: url).execute
end
+ # FIXME: we need to drop `or_initialize` in v5
+ # this behaviour is very buggy and unpredictable
def self.default
Rails.cache.fetch('default_store') do
where(default: true).first_or_initialize
@@ -176,6 +180,10 @@ module Spree
favicon_image.variant(resize: '32x32')
end
+ def can_be_deleted?
+ self.class.where.not(id: id).any?
+ end
+
private
def ensure_default_exists_and_is_unique
@@ -202,13 +210,20 @@ module Spree
self.supported_currencies = default_currency
end
- 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)
end
end
+ 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_cache
Rails.cache.delete('default_store')
Rails.cache.delete('stores_available_locales')
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 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.rb
index 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 do
end
end
+ 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
+ end
end
context 'with associations' do
before do
- store.products << create(:product)
+ another_store.products << create(:product)
end
it "doesn't destroy associations" do