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 the Spree::Product.for_filters scope

Łukasz Adamczyk8574d47da8c6
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 76009d8951..166600e43a 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -233,6 +233,13 @@ module Spree
end
search_scopes << :active
+ def self.for_filters(currency, taxon = nil)
+ scope = active(currency)
+ scope = scope.in_taxon(taxon) if taxon.present?
+ scope
+ end
+ search_scopes << :for_filters
+
def self.for_user(user = nil)
if user.try(:has_spree_role?, 'admin')
with_deleted
diff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rb
index 98e4e801f2..cf35330f6e 100644
--- a/core/spec/models/spree/product/scopes_spec.rb
+++ b/core/spec/models/spree/product/scopes_spec.rb
@@ -37,6 +37,32 @@ describe 'Product scopes', type: :model do
end
end
+ describe '.for_filters' do
+ subject { Spree::Product.method(:for_filters) }
+
+ let(:taxon_1) { create(:taxon) }
+ let(:taxon_2) { create(:taxon) }
+
+ let!(:product_1) { create(:product, currency: 'GBP', taxons: [taxon_1]) }
+ let!(:product_2) { create(:product, currency: 'GBP', taxons: [taxon_2]) }
+
+ before do
+ create(:product, currency: 'USD', taxons: [create(:taxon)])
+ end
+
+ context 'when giving a taxon' do
+ it { expect(subject.call('GBP', taxon_1)).to contain_exactly(product_1) }
+ end
+
+ context 'when giving no taxon' do
+ it { expect(subject.call('GBP')).to contain_exactly(product_1, product_2) }
+ end
+
+ context 'when giving a currency with no products' do
+ it { expect(subject.call('PLN')).to be_empty }
+ end
+ end
+
context 'A product assigned to parent and child taxons' do
before do
@taxonomy = create(:taxonomy)
diff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rb
index 4842b8014e..efda927fa0 100644
--- a/frontend/app/helpers/spree/frontend_helper.rb
+++ b/frontend/app/helpers/spree/frontend_helper.rb
@@ -299,7 +299,7 @@ module Spree
def available_option_types
@available_option_types ||= Rails.cache.fetch("available-option-types/#{available_option_types_cache_key}") do
- option_values = OptionValues::FindAvailable.new(products_scope: products_for_filtering).execute
+ option_values = OptionValues::FindAvailable.new(products_scope: products_for_filters).execute
Filters::Options.new(option_values_scope: option_values).to_a
end
end
@@ -310,7 +310,7 @@ module Spree
def available_properties
@available_properties ||= Rails.cache.fetch("available-properties/#{available_properties_cache_key}") do
- product_properties = ProductProperties::FindAvailable.new(products_scope: products_for_filtering).execute
+ product_properties = ProductProperties::FindAvailable.new(products_scope: products_for_filters).execute
Filters::Properties.new(product_properties_scope: product_properties).to_a
end
end
@@ -365,10 +365,8 @@ module Spree
end
end
- def products_for_filtering
- scope = Product.active(current_currency)
- scope = scope.in_taxon(@taxon) if @taxon.present?
- scope
+ def products_for_filters
+ Product.for_filters(current_currency, @taxon)
end
end
end