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.

refactor presenting Properties Filters

Łukasz Adamczykec008d22c412
diff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rb
index 21501566fd..101d9ab14e 100644
--- a/core/app/models/spree/property.rb
+++ b/core/app/models/spree/property.rb
@@ -19,9 +19,11 @@ module Spree
self.whitelisted_ransackable_attributes = ['presentation']
- def uniq_values
+ def uniq_values(product_properties_scope: nil)
Rails.cache.fetch("property-uniq-values/#{cache_key_with_version}") do
- product_properties.where.not(value: [nil, '']).pluck(:filter_param, :value).uniq
+ properties = product_properties
+ properties = properties.where(id: product_properties_scope) if product_properties_scope.present?
+ properties.where.not(value: [nil, '']).pluck(:filter_param, :value).uniq
end
end
diff --git a/core/app/presenters/spree/filters/property.rb b/core/app/presenters/spree/filters/property.rb
index 1f268c19aa..04d3218ba0 100644
--- a/core/app/presenters/spree/filters/property.rb
+++ b/core/app/presenters/spree/filters/property.rb
@@ -9,7 +9,7 @@ module Spree
delegate_missing_to :property
def uniq_values
- product_properties.pluck(:filter_param).uniq
+ property.uniq_values(product_properties_scope: product_properties)
end
private
diff --git a/core/spec/models/spree/property_spec.rb b/core/spec/models/spree/property_spec.rb
index d9028947ef..923615f771 100644
--- a/core/spec/models/spree/property_spec.rb
+++ b/core/spec/models/spree/property_spec.rb
@@ -17,5 +17,22 @@ describe Spree::Property, type: :model do
end
it { expect(property.uniq_values).to eq([['some-value', 'Some Value'], ['another-10-value', 'Another 10% Value']]) }
+
+ context 'when narrowing the scope of product properties' do
+ let(:product_property_1) { create(:product_property, property: property, value: 'Some 10% Value') }
+ let(:product_property_2) { create(:product_property, property: property, value: 'Some 10% Value') }
+ let(:product_property_3) { create(:product_property, property: property, value: 'Another 20% Value') }
+
+ let(:scope) { [product_property_1, product_property_2, product_property_3] }
+
+ it do
+ expect(property.uniq_values(product_properties_scope: scope)).to eq(
+ [
+ ['some-10-value', 'Some 10% Value'],
+ ['another-20-value', 'Another 20% Value']
+ ]
+ )
+ end
+ end
end
end
diff --git a/frontend/spec/features/products_filtering_spec.rb b/frontend/spec/features/products_filtering_spec.rb
index 85e3cde1d8..b389ef8867 100644
--- a/frontend/spec/features/products_filtering_spec.rb
+++ b/frontend/spec/features/products_filtering_spec.rb
@@ -79,9 +79,7 @@ describe 'Products filtering', :js do
expect(page).to have_selected_filter_with(value: 'M')
expect(page).to have_selected_filter_with(value: 'S')
- expect_working_filters_clearing
-
- click_on_filter 'Manufacturer', value: 'wilson'
+ click_on_filter 'Manufacturer', value: 'Wilson'
expect(page).not_to have_content 'First shirt'
expect(page).to have_content 'Second shirt'
expect(page).to have_selected_filter_with(value: 'WILSON')
@@ -90,7 +88,7 @@ describe 'Products filtering', :js do
expect(page).to have_content 'First shirt'
expect(page).to have_content 'Second shirt'
expect(page).to have_selected_filter_with(value: 'WILSON')
- expect(page).to have_selected_filter_with(value: 'ZETA')
+ expect(page).to have_selected_filter_with(value: 'ALPHA')
click_on_filter 'Brand', value: 'Alpha'
expect(page).not_to have_content 'First shirt'