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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
use OptionValues::FindAvailable for displaying Available Options for filtering Products
Łukasz Adamczyk85eb8c6b6b2a
diff --git a/core/app/finders/spree/option_values/find_available.rb b/core/app/finders/spree/option_values/find_available.rbnew file mode 100644index 0000000000..8f20edd91c--- /dev/null+++ b/core/app/finders/spree/option_values/find_available.rb@@ -0,0 +1,38 @@+module Spree+ module OptionValues+ class FindAvailable+ def initialize(scope: OptionValue.spree_base_scopes, currency: nil, taxon: nil)+ @scope = scope+ @currency = currency+ @taxon = taxon+ end++ def execute+ filterable_finder.execute+ end++ private++ attr_reader :scope, :currency, :taxon++ def filterable_finder+ FindFilterable.new(scope: scope, products_scope: products_scope)+ end++ def products_scope+ products = by_currency(Product.spree_base_scopes)+ by_taxon(products)+ end++ def by_currency(products)+ products.active(currency)+ end++ def by_taxon(products)+ return products if taxon.nil?++ products.in_taxon(taxon)+ end+ end+ end+enddiff --git a/core/app/finders/spree/option_values/find_filterable.rb b/core/app/finders/spree/option_values/find_filterable.rbindex 260a2e79d3..4a33213ffa 100644--- a/core/app/finders/spree/option_values/find_filterable.rb+++ b/core/app/finders/spree/option_values/find_filterable.rb@@ -9,7 +9,7 @@ module Spreedef executescope.filterable.- where(variants: { product_id: products_ids })+ where(Variant.table_name => { product_id: products_ids })endprivatediff --git a/core/spec/finders/spree/option_values/find_available_spec.rb b/core/spec/finders/spree/option_values/find_available_spec.rbnew file mode 100644index 0000000000..347faa60c2--- /dev/null+++ b/core/spec/finders/spree/option_values/find_available_spec.rb@@ -0,0 +1,75 @@+require 'spec_helper'++module Spree+ RSpec.describe OptionValues::FindAvailable do+ let(:taxon) { create(:taxon) }+ let(:gbp) { 'GBP' }++ let!(:size) { create(:option_type, :size) }+ let!(:s_size) { create(:option_value, option_type: size, name: 's') }+ let!(:m_size) { create(:option_value, option_type: size, name: 'm') }+ let!(:xl_size) { create(:option_value, option_type: size, name: 'xl') }++ let!(:color) { create(:option_type, :color) }+ let!(:red_color) { create(:option_value, option_type: color, name: 'red') }+ let!(:green_color) { create(:option_value, option_type: color, name: 'green') }+ let!(:blue_color) { create(:option_value, option_type: color, name: 'blue') }++ let!(:length) { create(:option_type, filterable: false) }+ let!(:mini) { create(:option_value, option_type: length) }++ before do+ product_1 = create(:product, option_types: [color, size], taxons: [taxon], currency: gbp)+ create(:variant, option_values: [s_size, green_color], product: product_1)++ product_2 = create(:product, option_types: [color, size], taxons: [taxon], currency: gbp)+ create(:variant, option_values: [red_color, m_size], product: product_2)++ product_3 = create(:product, option_types: [size, length], taxons: [taxon], currency: gbp)+ create(:variant, option_values: [s_size, mini], product: product_3)++ product_4 = create(:product, option_types: [color], taxons: [create(:taxon)], currency: gbp)+ create(:variant, option_values: [blue_color], product: product_4)++ product_5 = create(:product, option_types: [size], taxons: [taxon], currency: 'PLN')+ create(:variant, option_values: [xl_size], product: product_5)+ end++ describe '#execute' do+ subject(:available_options) { finder.execute }++ context 'when taxon and currency are given' do+ let(:finder) { described_class.new(taxon: taxon, currency: gbp) }++ it 'finds available Option Values' do+ expect(available_options).to contain_exactly(+ s_size, m_size,+ red_color, green_color+ )+ end+ end++ context 'when no taxon is given' do+ let(:finder) { described_class.new(currency: gbp) }++ it 'finds available Option Values in all Taxons' do+ expect(available_options).to contain_exactly(+ s_size, m_size,+ red_color, green_color, blue_color+ )+ end+ end++ context 'when no currency is given' do+ let(:finder) { described_class.new(taxon: taxon) }++ it 'finds available Option Values for all Currencies' do+ expect(available_options).to contain_exactly(+ s_size, m_size, xl_size,+ red_color, green_color+ )+ end+ end+ end+ end+enddiff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rbindex e2d39dbd04..d867606aa5 100644--- a/frontend/app/helpers/spree/frontend_helper.rb+++ b/frontend/app/helpers/spree/frontend_helper.rb@@ -299,18 +299,9 @@ module Spreedef available_option_types@available_option_types ||= Rails.cache.fetch("available-option-types/#{available_option_types_cache_key}") do- # TODO: extract this to a finder- products_scope = Product.active(current_currency)-- if @taxon.present?- products_scope = products_scope.in_taxon(@taxon)- end-- option_values = OptionValues::FindFilterable.new(products_scope: products_scope).execute-+ option_values = OptionValues::FindAvailable.new(taxon: @taxon, currency: current_currency).executeFilters::Options.new(option_values_scope: option_values).to_aend- @available_option_typesenddef available_properties_cache_key