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
add a Finder for Filterable Option Values
Łukasz Adamczyk208bd4dd8ce4
diff --git a/core/app/finders/spree/option_values/find_filterable.rb b/core/app/finders/spree/option_values/find_filterable.rbnew file mode 100644index 0000000000..9735a7cea4--- /dev/null+++ b/core/app/finders/spree/option_values/find_filterable.rb@@ -0,0 +1,20 @@+module Spree+ module OptionValues+ class FindFilterable+ def initialize(scope: OptionValue.spree_base_scopes, products_scope: Product.spree_base_scopes)+ @scope = scope+ @products_scope = products_scope+ end++ def execute+ scope.+ filterable.+ where(variants: { product_id: products_scope.pluck(:id) })+ end++ private++ attr_reader :scope, :products_scope+ end+ end+enddiff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rbindex 64b922b650..49ce619468 100644--- a/core/app/models/spree/option_value.rb+++ b/core/app/models/spree/option_value.rb@@ -11,6 +11,12 @@ module Spreevalidates :presentationend+ scope :filterable, lambda {+ joins(:option_type, :variants).+ where(option_type: { filterable: true }).+ distinct+ }+after_touch :touch_all_variantsdelegate :name, :presentation, to: :option_type, prefix: true, allow_nil: truediff --git a/core/lib/spree/testing_support/factories/options_factory.rb b/core/lib/spree/testing_support/factories/options_factory.rbindex aa775901e9..f93cdd945c 100644--- a/core/lib/spree/testing_support/factories/options_factory.rb+++ b/core/lib/spree/testing_support/factories/options_factory.rb@@ -8,5 +8,15 @@ FactoryBot.define dofactory :option_type, class: Spree::OptionType dosequence(:name) { |n| "foo-size-#{n}" }presentation { 'Size' }++ trait :size do+ name { 'size' }+ presentation { 'Size' }+ end++ trait :color do+ name { 'color' }+ presentation { 'Color' }+ endendenddiff --git a/core/spec/finders/spree/option_values/find_filterable_spec.rb b/core/spec/finders/spree/option_values/find_filterable_spec.rbnew file mode 100644index 0000000000..4014007880--- /dev/null+++ b/core/spec/finders/spree/option_values/find_filterable_spec.rb@@ -0,0 +1,89 @@+require 'spec_helper'++module Spree+ RSpec.describe OptionValues::FindFilterable do+ let(:finder) { described_class.new }++ describe '#execute' do+ let(:filterable_option_values) { finder.execute }++ 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!(:purple_color) { create(:option_value, option_type: color, name: 'purple') }++ let!(:non_filterable_option) { create(:option_type, filterable: false) }+ let!(:non_filterable_value) { create(:option_value, option_type: non_filterable_option) }++ before do+ create(+ :variant,+ option_values: [m_size, red_color],+ product: create(:product, option_types: [color, size])+ )++ create(+ :variant,+ option_values: [s_size, green_color, blue_color],+ product: create(:product, option_types: [color, size])+ )++ create(+ :variant,+ option_values: [s_size, green_color],+ product: create(:product, option_types: [color])+ )++ create(+ :variant,+ option_values: [non_filterable_value],+ product: create(:product, option_types: [non_filterable_option])+ )+ end++ it 'finds filterable Option Values' do+ expect(filterable_option_values).to contain_exactly(+ s_size, m_size,+ red_color, green_color, blue_color+ )+ end++ context 'when given a predefined scope' do+ let(:finder) { described_class.new(scope: scope) }+ let(:scope) { OptionValue.where(id: [s_size, green_color, purple_color, non_filterable_value]) }++ it 'finds filterable Option Values with respect to a predefined scope' do+ expect(filterable_option_values).to contain_exactly(s_size, green_color)+ end+ end++ context 'when given a predefined products scope' do+ let(:finder) { described_class.new(products_scope: products_scope) }+ let(:products_scope) { Product.where(id: [product_1, product_2, product_3]) }++ let(:product_1) { create(:product, option_types: [size]) }+ let(:product_2) { create(:product, option_types: [color]) }+ let(:product_3) { create(:product, option_types: [size, color, non_filterable_option]) }++ before do+ create(:variant, option_values: [xl_size], product: product_1)+ create(:variant, option_values: [red_color, purple_color], product: product_2)+ create(:variant, option_values: [m_size, green_color, non_filterable_value], product: product_3)+ end++ it 'finds filterable Option Values with respect to a predefined Products scope' do+ expect(filterable_option_values).to contain_exactly(+ m_size, xl_size,+ red_color, green_color, purple_color,+ )+ end+ end+ end+ end+end