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 memoization to resource/collection objects in API v2 controllers

Damian Legawiec1db2ffa30914
diff --git a/api/app/controllers/spree/api/v2/base_controller.rb b/api/app/controllers/spree/api/v2/base_controller.rb
index 4a36894293..9995756701 100644
--- a/api/app/controllers/spree/api/v2/base_controller.rb
+++ b/api/app/controllers/spree/api/v2/base_controller.rb
@@ -39,7 +39,7 @@ module Spree
end
def paginated_collection
- collection_paginator.new(sorted_collection, params).call
+ @paginated_collection ||= collection_paginator.new(sorted_collection, params).call
end
def collection_paginator
diff --git a/api/app/controllers/spree/api/v2/resource_controller.rb b/api/app/controllers/spree/api/v2/resource_controller.rb
index bdfc5e1d80..4c10afff75 100644
--- a/api/app/controllers/spree/api/v2/resource_controller.rb
+++ b/api/app/controllers/spree/api/v2/resource_controller.rb
@@ -15,7 +15,7 @@ module Spree
protected
def sorted_collection
- collection_sorter.new(collection, params, allowed_sort_attributes).call
+ @sorted_collection ||= collection_sorter.new(collection, params, allowed_sort_attributes).call
end
def allowed_sort_attributes
diff --git a/api/app/controllers/spree/api/v2/storefront/products_controller.rb b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
index a620ed9b6f..183bff2f75 100644
--- a/api/app/controllers/spree/api/v2/storefront/products_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
@@ -10,11 +10,11 @@ module Spree
end
def collection
- collection_finder.new(scope: scope, params: params, current_currency: current_currency).execute
+ @collection ||= collection_finder.new(scope: scope, params: params, current_currency: current_currency).execute
end
def resource
- scope.find_by(slug: params[:id]) || scope.find(params[:id])
+ @resource ||= scope.find_by(slug: params[:id]) || scope.find(params[:id])
end
def collection_sorter
diff --git a/api/app/controllers/spree/api/v2/storefront/stores_controller.rb b/api/app/controllers/spree/api/v2/storefront/stores_controller.rb
index 8949b12b3e..187e7038ec 100644
--- a/api/app/controllers/spree/api/v2/storefront/stores_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/stores_controller.rb
@@ -10,7 +10,7 @@ module Spree
end
def resource
- scope.find_by!(code: params[:code])
+ @resource ||= scope.find_by!(code: params[:code])
end
def resource_serializer
diff --git a/api/app/controllers/spree/api/v2/storefront/taxons_controller.rb b/api/app/controllers/spree/api/v2/storefront/taxons_controller.rb
index 693d679f33..82de2a4fbc 100644
--- a/api/app/controllers/spree/api/v2/storefront/taxons_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/taxons_controller.rb
@@ -18,15 +18,11 @@ module Spree
end
def paginated_collection
- collection_paginator.new(collection, params).call
- end
-
- def collection
- collection_finder.new(scope: scope, params: params).execute
+ @paginated_collection ||= collection_paginator.new(collection, params).call
end
def resource
- scope.find_by(permalink: params[:id]) || scope.find(params[:id])
+ @resource ||= scope.find_by(permalink: params[:id]) || scope.find(params[:id])
end
def model_class