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.

Return taxons from all stores if store is blank.

Sebastian Palma4b28b45bff71
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 1b142e690b..4b5ca7cac2 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -326,10 +326,14 @@ module Spree
end
def taxons_for_store(store)
- return if store.blank?
-
- Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do
- taxons.for_store(store)
+ if store.present?
+ Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do
+ taxons.for_store(store)
+ end
+ else
+ Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store_ids.join('/')}") do
+ taxons.for_stores(stores)
+ end
end
end
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 6f8fe6acda..437506ae1b 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -808,18 +808,25 @@ describe Spree::Product, type: :model do
describe '#taxons_for_store' do
let(:store) { create(:store) }
let(:store_2) { create(:store) }
- let(:product) { create(:product, stores: [store, store_2], taxons: [taxon, taxon_2]) }
- let(:taxonomy) { create(:taxonomy, store: store) }
- let(:taxonomy_2) { create(:taxonomy, store: store_2) }
- let(:taxon) { create(:taxon, taxonomy: taxonomy) }
- let(:taxon_2) { create(:taxon, taxonomy: taxonomy_2) }
- let(:taxon_3) { create(:taxon, taxonomy: taxonomy) }
+ let(:taxon) { create(:taxon, taxonomy: create(:taxonomy, store: store)) }
+ let(:taxon_2) { create(:taxon, taxonomy: create(:taxonomy, store: store_2)) }
+ let!(:product) { create(:product, stores: [store, store_2], taxons: [taxon, taxon_2]) }
- it 'returns product taxons for specified store' do
- expect(product.taxons_for_store(store)).to eq([taxon])
- expect(product.taxons_for_store(store_2)).to eq([taxon_2])
+ context 'with a store' do
+ it 'returns product taxons for specified store' do
+ expect(product.taxons_for_store(store)).to eq([taxon])
+ expect(product.taxons_for_store(store_2)).to eq([taxon_2])
+ end
+
+ it { expect(product.taxons_for_store(store)).to be_a(ActiveRecord::Relation) }
end
- it { expect(product.taxons_for_store(store)).to be_a(ActiveRecord::Relation) }
+ context 'without a store' do
+ it 'returns the product taxons for all the stores' do
+ expect(product.taxons_for_store(nil)).to eq([taxon, taxon_2])
+ end
+
+ it { expect(product.taxons_for_store(nil)).to be_a(ActiveRecord::Relation) }
+ end
end
end