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
adjust caching of Property#uniq_values
Łukasz Adamczykcf50dc1942bc
diff --git a/core/app/models/spree/product_property.rb b/core/app/models/spree/product_property.rbindex ad4fcdc91c..25e5d91ee3 100644--- a/core/app/models/spree/product_property.rb+++ b/core/app/models/spree/product_property.rb@@ -8,7 +8,7 @@ module Spreewith_options inverse_of: :product_properties dobelongs_to :product, touch: true, class_name: 'Spree::Product'- belongs_to :property, class_name: 'Spree::Property'+ belongs_to :property, touch: true, class_name: 'Spree::Property'endvalidates :property, presence: truediff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rbindex 101d9ab14e..05459dbcdf 100644--- a/core/app/models/spree/property.rb+++ b/core/app/models/spree/property.rb@@ -20,7 +20,7 @@ module Spreeself.whitelisted_ransackable_attributes = ['presentation']def uniq_values(product_properties_scope: nil)- Rails.cache.fetch("property-uniq-values/#{cache_key_with_version}") do+ with_uniq_values_cache_key(product_properties_scope) doproperties = product_propertiesproperties = properties.where(id: product_properties_scope) if product_properties_scope.present?properties.where.not(value: [nil, '']).pluck(:filter_param, :value).uniq@@ -32,5 +32,12 @@ module Spreedef touch_all_productsproducts.update_all(updated_at: Time.current)end++ def with_uniq_values_cache_key(product_properties_scope, &block)+ return block.call if product_properties_scope.present?++ uniq_values_cache_key = ['property-uniq-values', cache_key_with_version]+ Rails.cache.fetch(uniq_values_cache_key) { block.call }+ endendenddiff --git a/core/spec/models/spree/property_spec.rb b/core/spec/models/spree/property_spec.rbindex 923615f771..b113fae3f2 100644--- a/core/spec/models/spree/property_spec.rb+++ b/core/spec/models/spree/property_spec.rb@@ -19,19 +19,54 @@ describe Spree::Property, type: :model doit { 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!(: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+ let(:scope_uniq_values) do+ [+ ['some-10-value', 'Some 10% Value'],+ ['another-20-value', 'Another 20% Value']+ ]+ end++ it { expect(property.uniq_values(product_properties_scope: scope)).to eq(scope_uniq_values) }+ end++ context 'when caching' do+ it 'correctly returns uniq values' do+ expect(property.uniq_values).to eq([['some-value', 'Some Value'], ['another-10-value', 'Another 10% Value']])++ product_property_1 = create(:product_property, property: property, value: 'Some 20% Value')+ expect(property.uniq_values).to eq(+ [+ ['some-value', 'Some Value'],+ ['another-10-value', 'Another 10% Value'],+ ['some-20-value', 'Some 20% Value']+ ]+ )++ product_property_2 = create(:product_property, property: property, value: 'Another 20% Value')+ product_property_3 = create(:product_property, property: property, value: 'Another 30% Value')++ scope = [product_property_1, product_property_2]+ other_scope = [product_property_2, product_property_3]+expect(property.uniq_values(product_properties_scope: scope)).to eq([- ['some-10-value', 'Some 10% Value'],+ ['some-20-value', 'Some 20% Value'],['another-20-value', 'Another 20% Value']])++ expect(property.uniq_values(product_properties_scope: other_scope)).to eq(+ [+ ['another-20-value', 'Another 20% Value'],+ ['another-30-value', 'Another 30% Value']+ ]+ )endendend