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.

Preload product associations on the nested membership listings

Damian Legawiec246f1e0b769f
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/admin/product_listing.rb b/spree/api/app/controllers/concerns/spree/api/v3/admin/product_listing.rb
new file mode 100644
index 0000000000..bb8e061148
--- /dev/null
+++ b/spree/api/app/controllers/concerns/spree/api/v3/admin/product_listing.rb
@@ -0,0 +1,44 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # Preloads for any controller that serializes products through
+ # `Spree.api.admin_product_serializer` — the products index itself and
+ # the nested membership listings (a category's or a collection's
+ # products).
+ #
+ # The serializer walks variants, prices, stock items and media for every
+ # row, so a listing without these preloads issues a few hundred queries
+ # per page instead of a flat couple of dozen.
+ module ProductListing
+ extend ActiveSupport::Concern
+
+ protected
+
+ # Applied to the paginated index. The search provider builds its own
+ # relation, so `ProductsController` passes this to it explicitly.
+ def collection_includes
+ product_listing_includes
+ end
+
+ # Applied to the single-resource lookup by the base controller.
+ def scope_includes
+ product_listing_includes
+ end
+
+ private
+
+ def product_listing_includes
+ [
+ :tax_category,
+ { product_publications: :channel },
+ { primary_media: [attachment_attachment: :blob] },
+ { default_variant: [:prices, { stock_items: [:stock_location, :active_stock_reservations] }] },
+ { variants: [:prices, { stock_items: [:stock_location, :active_stock_reservations] }] }
+ ]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb
index 93aaf9374b..7217de09ec 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb
@@ -8,6 +8,8 @@ module Spree
# Listing is ordered by the classification position so the merchant's
# manual ordering round-trips; +reposition+ persists a drag-to-reorder.
class ProductsController < ResourceController
+ include Spree::Api::V3::Admin::ProductListing
+
scoped_resource :products
# Skip the base single-resource load — membership actions resolve
diff --git a/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb
index ba8da1d160..bfb2c64c5e 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb
@@ -9,6 +9,8 @@ module Spree
# persists a drag-to-reorder. Automatic (rule-based) membership is
# materialized by the rules, not managed here.
class ProductsController < ResourceController
+ include Spree::Api::V3::Admin::ProductListing
+
scoped_resource :products
# Skip the base single-resource load — membership actions resolve
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
index ef36aa4ee7..9448486aee 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
@@ -4,6 +4,7 @@ module Spree
module Admin
class ProductsController < ResourceController
include Spree::Api::V3::BulkOperations
+ include Spree::Api::V3::Admin::ProductListing
scoped_resource :products
@@ -125,16 +126,6 @@ module Spree
Spree.api.admin_product_serializer
end
- def scope_includes
- [
- :tax_category,
- product_publications: :channel,
- primary_media: [attachment_attachment: :blob],
- default_variant: [:prices, stock_items: [:stock_location, :active_stock_reservations]],
- variants: [:prices, stock_items: [:stock_location, :active_stock_reservations]]
- ]
- end
-
# Use SearchProvider::Database for collection to handle price/best_selling
# sorting correctly (counts before sorting, avoiding PG/Mobility issues).
def collection