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.

Delete policy slugs completely after deleting a policy (#13191)

Łukasz Adamczyk6878928ea6e2
diff --git a/core/app/models/spree/policy.rb b/core/app/models/spree/policy.rb
index c021db3f72..798481ed50 100644
--- a/core/app/models/spree/policy.rb
+++ b/core/app/models/spree/policy.rb
@@ -45,6 +45,8 @@ module Spree
#
self.whitelisted_ransackable_attributes = %w[name owner_type owner_id]
+ before_destroy :really_destroy_slugs!
+
def page_builder_url
return unless Spree::Core::Engine.routes.url_helpers.respond_to?(:policy_path)
@@ -54,5 +56,9 @@ module Spree
def with_body?
body.present?
end
+
+ def really_destroy_slugs!
+ slugs.with_deleted.each(&:really_destroy!)
+ end
end
end
diff --git a/core/spec/models/spree/policy_spec.rb b/core/spec/models/spree/policy_spec.rb
index cecb406ca4..e7e0c99150 100644
--- a/core/spec/models/spree/policy_spec.rb
+++ b/core/spec/models/spree/policy_spec.rb
@@ -48,6 +48,17 @@ RSpec.describe Spree::Policy, type: :model do
expect(policy.to_param).to eq('new-slug')
expect(policy.friendly_id_config.uses?(:history)).to be true
end
+
+ context 'when the policy is destroyed' do
+ it 'fully destroys the slug' do
+ policy = create(:policy, name: 'Test policy 123', slug: 'test-policy-123')
+ expect(policy.slugs.count).to eq(1)
+
+ policy.destroy
+
+ expect(FriendlyId::Slug.with_deleted.find_by(slug: 'test-policy-123')).to be_blank
+ end
+ end
end
describe 'Translations' do