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.

Added more memoization to speed up product listings (#12811)

Damian Legawiec47b8f41bd038
diff --git a/core/app/models/spree/wishlist.rb b/core/app/models/spree/wishlist.rb
index a820f935cc..37e244be0f 100644
--- a/core/app/models/spree/wishlist.rb
+++ b/core/app/models/spree/wishlist.rb
@@ -36,6 +36,13 @@ module Spree
@wished_items_count ||= variant_ids.count
end
+ # returns the variant ids in the wishlist
+ #
+ # @return [Array<Integer>]
+ def variant_ids
+ @variant_ids ||= wished_items.pluck(:variant_id)
+ end
+
def self.get_by_param(param)
find_by(token: param)
end
diff --git a/core/spec/models/spree/wishlist_spec.rb b/core/spec/models/spree/wishlist_spec.rb
index 77e9a6fc61..b5d12534e3 100644
--- a/core/spec/models/spree/wishlist_spec.rb
+++ b/core/spec/models/spree/wishlist_spec.rb
@@ -123,4 +123,18 @@ describe Spree::Wishlist, type: :model do
expect(wishlist.wished_items_count).to eq 1
end
end
+
+ describe '#variant_ids' do
+ let(:variant) { create(:variant) }
+ let(:variant_2) { create(:variant) }
+
+ before do
+ wishlist.wished_items << create(:wished_item, variant: variant)
+ wishlist.wished_items << create(:wished_item, variant: variant_2)
+ end
+
+ it 'returns the variant ids' do
+ expect(wishlist.variant_ids).to eq [variant.id, variant_2.id]
+ end
+ end
end
diff --git a/storefront/app/controllers/spree/store_controller.rb b/storefront/app/controllers/spree/store_controller.rb
index ee7a77a4e2..84be0a131f 100644
--- a/storefront/app/controllers/spree/store_controller.rb
+++ b/storefront/app/controllers/spree/store_controller.rb
@@ -123,29 +123,31 @@ module Spree
end
def storefront_products_scope
- current_store.products.active(current_currency)
+ @storefront_products_scope ||= current_store.products.active(current_currency)
end
def default_products_finder_params
- taxon = @taxon || current_taxon
+ @default_products_finder_params ||= begin
+ taxon = @taxon || current_taxon
- filter = permitted_products_params.fetch(:filter, {}).dup
+ filter = permitted_products_params.fetch(:filter, {}).dup
- filter[:taxon_ids] ||= [taxon&.id.to_s].compact
- filter[:taxons] = filter[:taxon_ids].join(',')
+ filter[:taxon_ids] ||= [taxon&.id.to_s].compact
+ filter[:taxons] = filter[:taxon_ids].join(',')
- if filter.key?(:min_price) || filter.key?(:max_price)
- min_price = filter[:min_price].presence || 0
- max_price = filter[:max_price].presence || 'Infinity'
+ if filter.key?(:min_price) || filter.key?(:max_price)
+ min_price = filter[:min_price].presence || 0
+ max_price = filter[:max_price].presence || 'Infinity'
- filter[:price] = [min_price, max_price].compact.join(',')
- end
+ filter[:price] = [min_price, max_price].compact.join(',')
+ end
- permitted_products_params.merge(
- store: current_store,
- filter: filter,
- currency: current_currency
- )
+ permitted_products_params.merge(
+ store: current_store,
+ filter: filter,
+ currency: current_currency
+ )
+ end
end
def storefront_products
diff --git a/storefront/app/helpers/spree/filters_helper.rb b/storefront/app/helpers/spree/filters_helper.rb
index 506553fd51..3a9c0533b7 100644
--- a/storefront/app/helpers/spree/filters_helper.rb
+++ b/storefront/app/helpers/spree/filters_helper.rb
@@ -76,10 +76,14 @@ module Spree
if filter_selected
default_storefront_filter_values_scope
else
- Spree::OptionValue.for_products(storefront_products_for_filters).distinct
+ all_option_values_scope
end
end
+ def all_option_values_scope
+ @all_option_values_scope ||= Spree::OptionValue.for_products(storefront_products_scope).distinct
+ end
+
def filter_values_for_filter(filter)
selected = single_option_filter_selected?(filter.name)
if filter.option_values.loaded?