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.

Hold the seller boundary from both sides

Damian Legawieca09290046dab
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 7e2bedd12a..09dab589ef 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -143,7 +143,10 @@ module Spree
# Guards every write path (including raw prefixed-id assignment) against
# linking another store's profile.
validate :delivery_profile_must_belong_to_store, if: :delivery_profile_id_changed?
- validate :vendor_must_belong_to_store, if: :vendor_id_changed?
+ # Either side moving breaks the pair, and a product's store is not frozen
+ # after create, so watching only the vendor would let a move carry the old
+ # store's seller along.
+ validate :vendor_must_belong_to_store, if: -> { vendor_id_changed? || store_id_changed? }
# Every product has at least one variant. `default_variant` is the "face" of
# the product (price display, default add-to-cart, property delegation).
diff --git a/spree/core/spec/models/spree/vendor_spec.rb b/spree/core/spec/models/spree/vendor_spec.rb
index b6a7838beb..ab8eff0a21 100644
--- a/spree/core/spec/models/spree/vendor_spec.rb
+++ b/spree/core/spec/models/spree/vendor_spec.rb
@@ -134,6 +134,18 @@ describe Spree::Vendor do
expect(build(:product, store: store, vendor: create(:vendor, :approved, store: store))).to be_valid
end
+ # A product's store is not frozen after create, so the pair can also be
+ # broken from the other side — by moving the product and leaving the
+ # seller behind.
+ it 'refuses moving to a store the seller does not belong to' do
+ product = create(:product, store: store, vendor: create(:vendor, :approved, store: store))
+
+ product.store = create(:store)
+
+ expect(product).not_to be_valid
+ expect(product.errors[:vendor]).to be_present
+ end
+
it 'keeps them when the vendor is destroyed' do
vendor = create(:vendor, :approved)
product = create(:product, store: store, vendor: vendor)