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.

Fix: give every prefixed-ID prefix a single owner (#14699)

Damian Legawiec8905b1bf4919
diff --git a/docs/developer/upgrades/5.6-to-6.0.mdx b/docs/developer/upgrades/5.6-to-6.0.mdx
index bfa7a33e82..8c06eef1aa 100644
--- a/docs/developer/upgrades/5.6-to-6.0.mdx
+++ b/docs/developer/upgrades/5.6-to-6.0.mdx
@@ -403,6 +403,20 @@ one. An order whose every parcel was recalled now reads
`fulfillment_status: unfulfilled` rather than `canceled`; only a canceled
order reads `canceled`.
+## Payment source IDs use the `psrc_` prefix
+
+`Spree::PaymentSource` shared the `ps_` prefix with `Spree::PaymentSession`,
+so the same string could name either record. Payment sources now use
+`psrc_`. The encoded part of the ID doesn't change, only the prefix. The only
+place a payment source's ID shows up is the `source_id` of a payment backed by
+a non-card payment source (wallets, bank redirects and similar). If you stored
+one of those values, swap `ps_` for `psrc_`. Payment session IDs stay `ps_`.
+
+Class-level `decode_prefixed_id` (for example `Spree::Product.decode_prefixed_id`)
+now returns `nil` for an ID with another model's prefix, the same as
+`find_by_prefix_id`. Call `Spree::PrefixedId.decode_prefixed_id` if you
+really need to decode any prefix.
+
## Removed in 6.0
These were deprecated in 5.x and are **gone now** — there is no bridge, so calls raise `NoMethodError`. Most were one-line delegations to a replacement that already exists.
diff --git a/packages/dashboard/src/schemas/commission-rate.test.ts b/packages/dashboard/src/schemas/commission-rate.test.ts
index 45157fd47f..c85847c430 100644
--- a/packages/dashboard/src/schemas/commission-rate.test.ts
+++ b/packages/dashboard/src/schemas/commission-rate.test.ts
@@ -29,7 +29,7 @@ describe('commissionRateToFormValues', () => {
it('seeds seller and category pickers from prefixed top-level ids', () => {
const rate = commissionRateStub([
{
- id: 'crule_1',
+ id: 'comrule_1',
type: 'seller_rule',
commission_rate_id: 'crate_1',
preferences: { seller_ids: [42] },
@@ -41,7 +41,7 @@ describe('commissionRateToFormValues', () => {
category_ids: null,
},
{
- id: 'crule_2',
+ id: 'comrule_2',
type: 'category_rule',
commission_rate_id: 'crate_1',
preferences: { category_ids: [7] },
diff --git a/spree/core/app/models/concerns/spree/prefixed_id.rb b/spree/core/app/models/concerns/spree/prefixed_id.rb
index 44af78b27f..994feb2d61 100644
--- a/spree/core/app/models/concerns/spree/prefixed_id.rb
+++ b/spree/core/app/models/concerns/spree/prefixed_id.rb
@@ -168,8 +168,11 @@ module Spree
Spree::PrefixedId.decode_prefixed_id(prefixed_id)
end
+ # Model-scoped decode: answers nil for another model's prefixed ID, like
+ # +find_by_prefix_id+. Use +Spree::PrefixedId.decode_prefixed_id+ when
+ # any model's ID is acceptable.
def decode_prefixed_id(prefixed_id_string)
- Spree::PrefixedId.decode_prefixed_id(prefixed_id_string)
+ decode_own_prefixed_id(prefixed_id_string)
end
# Find by prefixed ID first, falling back to integer id for backwards compatibility.
diff --git a/spree/core/app/models/spree/commission_rule.rb b/spree/core/app/models/spree/commission_rule.rb
index a346b548a5..32cd834599 100644
--- a/spree/core/app/models/spree/commission_rule.rb
+++ b/spree/core/app/models/spree/commission_rule.rb
@@ -16,7 +16,7 @@ module Spree
class CommissionRule < Spree.base_class
include Spree::PreferenceSchema
- has_prefix_id :crule
+ has_prefix_id :comrule
# Retired rather than deleted, with its rate or on its own as an operator
# edits a rate's conditions. A commission line is a settlement record, and
diff --git a/spree/core/app/models/spree/payment_source.rb b/spree/core/app/models/spree/payment_source.rb
index 3b0499a28e..63348b1572 100644
--- a/spree/core/app/models/spree/payment_source.rb
+++ b/spree/core/app/models/spree/payment_source.rb
@@ -1,7 +1,7 @@
# This model is used to store payment sources for non-credit card payments, eg wallet, account, etc.
module Spree
class PaymentSource < Spree.base_class
- has_prefix_id :ps
+ has_prefix_id :psrc
include Spree::HasCustomFields
include Spree::Metadata
diff --git a/spree/core/spec/models/spree/concerns/prefixed_id_spec.rb b/spree/core/spec/models/spree/concerns/prefixed_id_spec.rb
index 69aea71a60..7d93d376c1 100644
--- a/spree/core/spec/models/spree/concerns/prefixed_id_spec.rb
+++ b/spree/core/spec/models/spree/concerns/prefixed_id_spec.rb
@@ -111,6 +111,32 @@ RSpec.describe Spree::PrefixedId do
end
end
+ describe '.decode_prefixed_id (class-level)' do
+ let(:product) { create(:product) }
+
+ it 'decodes its own prefixed ID' do
+ expect(Spree::Product.decode_prefixed_id(product.prefixed_id)).to eq(product.id)
+ end
+
+ it "rejects another model's prefixed ID with the same numeric payload" do
+ expect(Spree::Variant.decode_prefixed_id(Spree::Product.prefixed_id_for(product.id))).to be_nil
+ end
+ end
+
+ describe 'prefix registry' do
+ it 'gives every model a unique prefix' do
+ Rails.application.eager_load!
+
+ prefixed_roots = ActiveRecord::Base.descendants.select do |klass|
+ klass < Spree::PrefixedId && !klass.abstract_class? && klass._prefix_id_prefix.present?
+ end.map(&:base_class).uniq
+
+ duplicates = prefixed_roots.group_by(&:_prefix_id_prefix).select { |_, classes| classes.many? }
+
+ expect(duplicates.transform_values { |classes| classes.map(&:name).sort }).to eq({})
+ end
+ end
+
describe '.find_by_param' do
it 'finds by prefixed ID' do
variant = create(:variant)