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.

Add memomization to some methods to speed things up

Damian Legawiecb2794e2469fe
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 8921d211ee..86a6c04295 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -160,7 +160,7 @@ module Spree
#
# @return [Spree::Variant]
def default_variant
- Rails.cache.fetch(default_variant_cache_key) do
+ @default_variant ||= Rails.cache.fetch(default_variant_cache_key) do
if Spree::Config[:track_inventory_levels] && variants.in_stock_or_backorderable.any?
variants.in_stock_or_backorderable.first
else
@@ -172,11 +172,11 @@ module Spree
# Returns default Variant ID for Product
# @return [Integer]
def default_variant_id
- default_variant.id
+ @default_variant_id ||= default_variant.id
end
def tax_category
- super || TaxCategory.find_by(is_default: true)
+ @tax_category ||= super || TaxCategory.find_by(is_default: true)
end
# Adding properties and option types on creation based on a chosen prototype
@@ -288,11 +288,11 @@ module Spree
end
def total_on_hand
- if any_variants_not_track_inventory?
- Float::INFINITY
- else
- stock_items.sum(:count_on_hand)
- end
+ @total_on_hand ||= if any_variants_not_track_inventory?
+ Float::INFINITY
+ else
+ stock_items.sum(:count_on_hand)
+ end
end
# Master variant may be deleted (i.e. when the product is deleted)
@@ -303,14 +303,11 @@ module Spree
end
def brand
- taxons.joins(:taxonomy).find_by(spree_taxonomies: { name: Spree.t(:taxonomy_brands_name) })
+ @brand ||= taxons.joins(:taxonomy).find_by(spree_taxonomies: { name: Spree.t(:taxonomy_brands_name) })
end
def category
- taxons.joins(:taxonomy).
- where(spree_taxonomies: { name: Spree.t(:taxonomy_categories_name) }).
- order(depth: :desc).
- first
+ @category ||= taxons.joins(:taxonomy).order(depth: :desc).find_by(spree_taxonomies: { name: Spree.t(:taxonomy_categories_name) })
end
private
@@ -440,12 +437,12 @@ module Spree
end
def taxon_and_ancestors
- taxons.map(&:self_and_ancestors).flatten.uniq
+ @taxon_and_ancestors ||= taxons.map(&:self_and_ancestors).flatten.uniq
end
# Get the taxonomy ids of all taxons assigned to this product and their ancestors.
def taxonomy_ids
- taxon_and_ancestors.map(&:taxonomy_id).flatten.uniq
+ @taxonomy_ids ||= taxon_and_ancestors.map(&:taxonomy_id).flatten.uniq
end
# Iterate through this products taxons and taxonomies and touch their timestamps in a batch
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 2c8848a0bf..ce5005b073 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -123,15 +123,15 @@ module Spree
end
def tax_category
- if self[:tax_category_id].nil?
- product.tax_category
- else
- Spree::TaxCategory.find(self[:tax_category_id])
- end
+ @tax_category ||= if self[:tax_category_id].nil?
+ product.tax_category
+ else
+ Spree::TaxCategory.find(self[:tax_category_id])
+ end
end
def options_text
- Spree::Variants::OptionsPresenter.new(self).to_sentence
+ @options_text ||= Spree::Variants::OptionsPresenter.new(self).to_sentence
end
# Default to master name
@@ -226,7 +226,7 @@ module Spree
end
def compare_at_price
- price_in(cost_currency).try(:compare_at_amount)
+ @compare_at_price ||= price_in(cost_currency).try(:compare_at_amount)
end
def name_and_sku
diff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rb
index 1049929305..f8ce4bc0d0 100644
--- a/frontend/app/helpers/spree/frontend_helper.rb
+++ b/frontend/app/helpers/spree/frontend_helper.rb
@@ -295,7 +295,7 @@ module Spree
end
def checkout_available_payment_methods
- @order.available_payment_methods(current_store)
+ @checkout_available_payment_methods ||= @order.available_payment_methods(current_store)
end
private
diff --git a/frontend/app/helpers/spree/navigation_helper.rb b/frontend/app/helpers/spree/navigation_helper.rb
index 9127d07548..e584f9fa5c 100644
--- a/frontend/app/helpers/spree/navigation_helper.rb
+++ b/frontend/app/helpers/spree/navigation_helper.rb
@@ -3,7 +3,7 @@ require 'digest'
module Spree
module NavigationHelper
def spree_navigation_data
- SpreeStorefrontConfig.dig(current_store.code, :navigation) || SpreeStorefrontConfig.dig(:default, :navigation) || []
+ @spree_navigation_data ||= SpreeStorefrontConfig.dig(current_store.code, :navigation) || SpreeStorefrontConfig.dig(:default, :navigation) || []
# safeguard for older Spree installs that don't have spree_navigation initializer
# or spree.yml file present
rescue