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
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.rbnew file mode 100644index 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+enddiff --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.rbindex 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 resolvediff --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.rbindex 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 resolvediff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rbindex 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 Spreemodule Adminclass ProductsController < ResourceControllerinclude Spree::Api::V3::BulkOperations+ include Spree::Api::V3::Admin::ProductListingscoped_resource :products@@ -125,16 +126,6 @@ module SpreeSpree.api.admin_product_serializerend- 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