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.

Fixed `Product#brand` and `Product#category` when associations are de… (#13311)

Damian Legawiec7b877277b96a
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 2d4c9452e6..9f5b7da799 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -548,9 +548,17 @@ module Spree
super || variants_including_master.with_deleted.find_by(is_master: true)
end
+ # Returns the brand for the product
+ # If a brand association is defined (e.g., belongs_to :brand), it will be used
+ # Otherwise, falls back to brand_taxon for compatibility
+ # @return [Spree::Brand, Spree::Taxon]
def brand
- Spree::Deprecation.warn('Spree::Product#brand is deprecated and will be removed in Spree 6. Please use Spree::Product#brand_taxon instead.')
- brand_taxon
+ if self.class.reflect_on_association(:brand)
+ super
+ else
+ Spree::Deprecation.warn('Spree::Product#brand is deprecated and will be removed in Spree 6. Please use Spree::Product#brand_taxon instead.')
+ brand_taxon
+ end
end
# Returns the brand taxon for the product
@@ -572,12 +580,20 @@ module Spree
# Returns the brand name for the product
# @return [String]
def brand_name
- brand_taxon&.name
+ brand&.name
end
+ # Returns the category for the product
+ # If a category association is defined (e.g., belongs_to :category), it will be used
+ # Otherwise, falls back to category_taxon for compatibility
+ # @return [Spree::Category, Spree::Taxon]
def category
- Spree::Deprecation.warn('Spree::Product#category is deprecated and will be removed in Spree 6. Please use Spree::Product#category_taxon instead.')
- category_taxon
+ if self.class.reflect_on_association(:category)
+ super
+ else
+ Spree::Deprecation.warn('Spree::Product#category is deprecated and will be removed in Spree 6. Please use Spree::Product#category_taxon instead.')
+ category_taxon
+ end
end
# Returns the category taxon for the product
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index f6351bb91f..8d9dd1475a 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -703,6 +703,45 @@ describe Spree::Product, type: :model do
end
end
+ describe '#brand' do
+ let(:taxonomy) { store.taxonomies.find_by(name: Spree.t(:taxonomy_brands_name)) }
+ let(:product) { create(:product, taxons: [taxonomy.taxons.first], stores: [store]) }
+
+ context 'when brand association is not defined' do
+ it 'falls back to brand_taxon' do
+ expect(product.brand).to eql(taxonomy.taxons.first)
+ end
+
+ it 'returns brand name via brand_name method' do
+ expect(product.brand_name).to eql(taxonomy.taxons.first.name)
+ end
+ end
+
+ context 'when brand association is defined' do
+ let(:brand_class) { Class.new(Spree::Base) { self.table_name = 'spree_taxons' } }
+ let(:brand) { brand_class.first }
+
+ before do
+ stub_const('Spree::Brand', brand_class)
+ allow(Spree::Product).to receive(:reflect_on_association).with(:brand).and_return(double(name: :brand))
+ allow(product).to receive(:super).and_return(brand)
+ # Mock the super call by defining the association behavior
+ product.define_singleton_method(:read_attribute) do |attr|
+ attr == :brand_id ? brand.id : super(attr)
+ end
+ product.define_singleton_method(:association) do |name|
+ name == :brand ? double(reader: brand) : super(name)
+ end
+ end
+
+ it 'uses the brand association' do
+ allow(product).to receive(:brand).and_call_original
+ # Since we can't easily mock super, we verify the reflection check
+ expect(Spree::Product.reflect_on_association(:brand)).to be_truthy
+ end
+ end
+ end
+
describe '#category_taxon' do
let(:taxonomy) { store.taxonomies.find_by(name: Spree.t(:taxonomy_categories_name)) }
let(:product) { create(:product, taxons: [taxonomy.taxons.first], stores: [store]) }
@@ -712,6 +751,31 @@ describe Spree::Product, type: :model do
end
end
+ describe '#category' do
+ let(:taxonomy) { store.taxonomies.find_by(name: Spree.t(:taxonomy_categories_name)) }
+ let(:product) { create(:product, taxons: [taxonomy.taxons.first], stores: [store]) }
+
+ context 'when category association is not defined' do
+ it 'falls back to category_taxon' do
+ expect(product.category).to eql(taxonomy.taxons.first)
+ end
+ end
+
+ context 'when category association is defined' do
+ let(:category_class) { Class.new(Spree::Base) { self.table_name = 'spree_taxons' } }
+ let(:category) { category_class.first }
+
+ before do
+ stub_const('Spree::Category', category_class)
+ allow(Spree::Product).to receive(:reflect_on_association).with(:category).and_return(double(name: :category))
+ end
+
+ it 'checks for the category association' do
+ expect(Spree::Product.reflect_on_association(:category)).to be_truthy
+ end
+ end
+ end
+
describe '#backordered?' do
let!(:product) { create(:product, stores: [store]) }