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.

Add `Product#master_id`

Damian Legawiecf45ec1448fc3
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index c3a0372d5e..34becdeb08 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -254,6 +254,9 @@ module Spree
delegate :display_amount, :display_price, :has_default_price?, :track_inventory?,
:display_compare_at_price, :images, to: :default_variant
+ # Rails doesn't provide _id methods for has_one associations by default
+ delegate :id, to: :master, prefix: true, allow_nil: true
+
alias master_images images
state_machine :status, initial: :draft do
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index b2e31e7804..280d53eef9 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -191,6 +191,18 @@ describe Spree::Product, type: :model do
end
end
+ describe '#master_id' do
+ it 'returns the id of the master variant' do
+ expect(product.master_id).to eq(product.master.id)
+ end
+
+ it 'returns nil when master is not present' do
+ product_without_master = build(:product, stores: [store])
+ product_without_master.master = nil
+ expect(product_without_master.master_id).to be_nil
+ end
+ end
+
context 'product has no variants' do
describe '#destroy' do
it 'sets deleted_at value' do