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.

display only these Properties for filtering that have any Products for a selected Currency and Taxon

Łukasz Adamczyk90e4fb4ef667
diff --git a/core/app/finders/concerns/spree/product_filterable.rb b/core/app/finders/concerns/spree/product_filterable.rb
new file mode 100644
index 0000000000..729b7d5789
--- /dev/null
+++ b/core/app/finders/concerns/spree/product_filterable.rb
@@ -0,0 +1,9 @@
+module Spree
+ module ProductFilterable
+ private
+
+ def find_available(scope, products_scope)
+ scope.filterable.for_products(products_scope)
+ end
+ end
+end
diff --git a/core/app/finders/spree/option_values/find_available.rb b/core/app/finders/spree/option_values/find_available.rb
index 5e4eea8920..7ee16c1977 100644
--- a/core/app/finders/spree/option_values/find_available.rb
+++ b/core/app/finders/spree/option_values/find_available.rb
@@ -1,14 +1,15 @@
module Spree
module OptionValues
class FindAvailable
+ include ProductFilterable
+
def initialize(scope: OptionValue.spree_base_scopes, products_scope: Product.spree_base_scopes)
@scope = scope
@products_scope = products_scope
end
def execute
- filterable_options = scope.filterable
- filterable_options.for_products(products_scope)
+ find_available(scope, products_scope)
end
private
diff --git a/core/app/finders/spree/product_properties/find_available.rb b/core/app/finders/spree/product_properties/find_available.rb
new file mode 100644
index 0000000000..79e497dcb4
--- /dev/null
+++ b/core/app/finders/spree/product_properties/find_available.rb
@@ -0,0 +1,20 @@
+module Spree
+ module ProductProperties
+ class FindAvailable
+ include ProductFilterable
+
+ def initialize(scope: ProductProperty.spree_base_scopes, products_scope: Product.spree_base_scopes)
+ @scope = scope
+ @products_scope = products_scope
+ end
+
+ def execute
+ find_available(scope, products_scope)
+ end
+
+ private
+
+ attr_reader :scope, :products_scope
+ end
+ end
+end
diff --git a/core/app/models/spree/product_property.rb b/core/app/models/spree/product_property.rb
index a4829c4e3b..ad4fcdc91c 100644
--- a/core/app/models/spree/product_property.rb
+++ b/core/app/models/spree/product_property.rb
@@ -15,6 +15,9 @@ module Spree
default_scope { order(:position) }
+ scope :filterable, -> { joins(:property).where(Property.table_name => { filterable: true }) }
+ scope :for_products, ->(products) { joins(:product).merge(products) }
+
self.whitelisted_ransackable_attributes = ['value', 'filter_param']
self.whitelisted_ransackable_associations = ['property']
diff --git a/core/app/presenters/spree/filters/properties.rb b/core/app/presenters/spree/filters/properties.rb
new file mode 100644
index 0000000000..f2157677b2
--- /dev/null
+++ b/core/app/presenters/spree/filters/properties.rb
@@ -0,0 +1,23 @@
+module Spree
+ module Filters
+ class Properties
+ def initialize(product_properties_scope:)
+ @product_properties = product_properties_scope.includes(:property)
+ end
+
+ def to_a
+ grouped_options.map do |property, product_properties|
+ Property.new(property: property, product_properties: product_properties)
+ end
+ end
+
+ private
+
+ attr_reader :product_properties
+
+ def grouped_options
+ product_properties.group_by(&:property)
+ end
+ end
+ end
+end
diff --git a/core/app/presenters/spree/filters/property.rb b/core/app/presenters/spree/filters/property.rb
new file mode 100644
index 0000000000..1f268c19aa
--- /dev/null
+++ b/core/app/presenters/spree/filters/property.rb
@@ -0,0 +1,20 @@
+module Spree
+ module Filters
+ class Property
+ def initialize(property:, product_properties:)
+ @property = property
+ @product_properties = product_properties
+ end
+
+ delegate_missing_to :property
+
+ def uniq_values
+ product_properties.pluck(:filter_param).uniq
+ end
+
+ private
+
+ attr_reader :property, :product_properties
+ end
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/property_factory.rb b/core/lib/spree/testing_support/factories/property_factory.rb
index 084f8116c1..694dea1543 100644
--- a/core/lib/spree/testing_support/factories/property_factory.rb
+++ b/core/lib/spree/testing_support/factories/property_factory.rb
@@ -3,6 +3,10 @@ FactoryBot.define do
name { 'baseball_cap_color' }
presentation { 'cap color' }
+ trait :filterable do
+ filterable { true }
+ end
+
trait :brand do
name { 'brand' }
presentation { 'Brand' }
diff --git a/core/spec/finders/spree/product_properties/find_available_spec.rb b/core/spec/finders/spree/product_properties/find_available_spec.rb
new file mode 100644
index 0000000000..26ef0bbbee
--- /dev/null
+++ b/core/spec/finders/spree/product_properties/find_available_spec.rb
@@ -0,0 +1,52 @@
+require 'spec_helper'
+
+module Spree
+ RSpec.describe ProductProperties::FindAvailable do
+ let(:finder) { described_class.new }
+
+ let(:brand) { create(:property, :brand, :filterable,) }
+ let(:alpha_brand) { create(:product_property, property: brand, value: 'Alpha') }
+ let(:beta_brand) { create(:product_property, property: brand, value: 'Beta') }
+ let(:gamma_brand) { create(:product_property, property: brand, value: 'Gamma') }
+
+ let(:manufacturer) { create(:property, :manufacturer, :filterable,) }
+ let(:wilson_manufacturer) { create(:product_property, property: manufacturer, value: 'Wilson') }
+ let(:jerseys_manufacturer) { create(:product_property, property: manufacturer, value: 'Jerseys') }
+
+ describe '#execute' do
+ subject(:available_properties) { finder.execute }
+
+ it 'finds available Product Properties' do
+ expect(available_properties).to contain_exactly(
+ alpha_brand, beta_brand, gamma_brand,
+ wilson_manufacturer, jerseys_manufacturer
+ )
+ end
+
+ context 'when given a predefined scope' do
+ let(:finder) { described_class.new(scope: scope) }
+ let(:scope) { ProductProperty.where(id: [beta_brand, jerseys_manufacturer]) }
+
+ it 'finds available Product Properties with respect to a predefined scope' do
+ expect(available_properties).to contain_exactly(beta_brand, jerseys_manufacturer)
+ 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) { alpha_brand.product }
+ let(:product_2) { gamma_brand.product }
+ let(:product_3) { wilson_manufacturer.product }
+
+ it 'finds filterable Product Properties with respect to a predefined Products scope' do
+ expect(available_properties).to contain_exactly(
+ alpha_brand, gamma_brand,
+ wilson_manufacturer
+ )
+ end
+ end
+ end
+ end
+end
diff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rb
index ac8f3e8df7..4842b8014e 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_available_option_values).execute
+ option_values = OptionValues::FindAvailable.new(products_scope: products_for_filtering).execute
Filters::Options.new(option_values_scope: option_values).to_a
end
end
@@ -310,9 +310,9 @@ module Spree
def available_properties
@available_properties ||= Rails.cache.fetch("available-properties/#{available_properties_cache_key}") do
- Spree::Property.includes(:product_properties).filterable.to_a
+ product_properties = ProductProperties::FindAvailable.new(products_scope: products_for_filtering).execute
+ Filters::Properties.new(product_properties_scope: product_properties).to_a
end
- @available_properties
end
def spree_social_link(service)
@@ -365,7 +365,7 @@ module Spree
end
end
- def products_for_available_option_values
+ def products_for_filtering
scope = Product.active(current_currency)
scope = scope.in_taxon(@taxon) if @taxon.present?
scope
diff --git a/frontend/app/views/spree/products/_filters_desktop.html.erb b/frontend/app/views/spree/products/_filters_desktop.html.erb
index e1a0c7c5e0..013498df91 100644
--- a/frontend/app/views/spree/products/_filters_desktop.html.erb
+++ b/frontend/app/views/spree/products/_filters_desktop.html.erb
@@ -3,7 +3,7 @@
<%= clear_all_filters_link(permitted_params) if product_filters_present?(permitted_params) %>
<div class="plp-filters-scroller">
- <% @available_option_types.each do |option_type| %>
+ <% available_option_types.each do |option_type| %>
<div class="w-100 card plp-filters-card">
<% ot_downcase_name = option_type.filter_param %>
<% option_type_name = ot_downcase_name.titleize %>