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
Use SearchProvider::Database in admin products controller
Damian Legawiec70ea22fc3e82
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.rbindex 919d157d46..cd89c4b06d 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@@ -3,13 +3,6 @@ module Spreemodule V3module Adminclass ProductsController < ResourceController- # Sort values that require special scopes- CUSTOM_SORT_SCOPES = {- 'price' => :ascend_by_price,- '-price' => :descend_by_price,- 'best_selling' => :by_best_selling- }.freeze-# PATCH /api/v3/admin/products/:iddef updateif @resource.update(update_params)@@ -51,30 +44,24 @@ module Spree]end- def collection_distinct?- !custom_sort_requested?- end-- def apply_collection_sort(collection)- sort_value = sort_param- return collection unless sort_value.present?-- scope_method = CUSTOM_SORT_SCOPES[sort_value]- return collection unless scope_method-- sorted = collection.reorder(nil)- sort_value == 'best_selling' ? sorted.distinct(false).send(scope_method) : sorted.send(scope_method)- end+ # Use SearchProvider::Database for collection to handle price/best_selling+ # sorting correctly (counts before sorting, avoiding PG/Mobility issues).+ def collection+ return @collection if @collection.present?- def ransack_params- rp = super+ filters = params[:q]&.to_unsafe_h || params[:q] || {}- if sort_param.present? && CUSTOM_SORT_SCOPES.key?(sort_param)- rp = rp.is_a?(Hash) ? rp.dup : rp.to_unsafe_h- rp.delete('s')- end+ result = search_provider.search_and_filter(+ scope: scope.includes(collection_includes).preload_associations_lazily.accessible_by(current_ability, :show),+ query: nil,+ filters: filters,+ sort: sort_param,+ page: page,+ limit: limit+ )- rp+ @pagy = result.pagy+ @collection = result.productsenddef permitted_params@@ -95,6 +82,10 @@ module Spreeprivate+ def search_provider+ @search_provider ||= Spree::SearchProvider::Database.new(current_store)+ end+def update_paramsp = permitted_params.to_h.with_indifferent_access@@ -108,10 +99,6 @@ module Spreepend-- def custom_sort_requested?- CUSTOM_SORT_SCOPES.key?(sort_param)- endendendend