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
Added Spree::BaseSorter class
Damian Legawiec9224d27e3016
diff --git a/api/app/models/spree/api_dependencies.rb b/api/app/models/spree/api_dependencies.rbindex 91e1c3a3a6..799511429e 100644--- a/api/app/models/spree/api_dependencies.rb+++ b/api/app/models/spree/api_dependencies.rb@@ -15,7 +15,8 @@ module Spree:storefront_product_serializer, :storefront_taxon_serializer, :storefront_taxon_finder, :storefront_find_by_variant_finder,:storefront_cart_update_service, :storefront_cart_estimate_shipping_rates_service, :storefront_estimated_shipment_serializer,:storefront_store_serializer, :storefront_address_serializer, :storefront_order_serializer,- :storefront_account_create_address_service, :storefront_account_update_address_service, :storefront_address_finder+ :storefront_account_create_address_service, :storefront_account_update_address_service, :storefront_address_finder,+ :storefront_collection_sorter].freezeattr_accessor *INJECTION_POINTS@@ -69,7 +70,8 @@ module Spree@storefront_order_serializer = 'Spree::V2::Storefront::CartSerializer'# sorters- @storefront_order_sorter = Spree::Dependencies.order_sorter+ @storefront_collection_sorter = Spree::Dependencies.collection_sorter+ @storefront_order_sorter = Spree::Dependencies.collection_sorter@storefront_products_sorter = Spree::Dependencies.products_sorter# paginatorsdiff --git a/api/spec/requests/spree/api/v2/storefront/account/orders_spec.rb b/api/spec/requests/spree/api/v2/storefront/account/orders_spec.rbindex dd1929987e..37010676b1 100644--- a/api/spec/requests/spree/api/v2/storefront/account/orders_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/account/orders_spec.rb@@ -111,7 +111,7 @@ describe 'Storefront API v2 Orders spec', type: :request doit 'returns orders sorted by completed_at' doexpect(json_response['data'].count).to eq Spree::Order.count- expect(json_response['data'].pluck(:id)).to eq Spree::Order.select('*').order(completed_at: :desc).pluck(:id).map(&:to_s)+ expect(json_response['data'].pluck(:id)).to eq Spree::Order.select('*').order(completed_at: :asc).pluck(:id).map(&:to_s)endend@@ -122,7 +122,7 @@ describe 'Storefront API v2 Orders spec', type: :request doit 'returns orders sorted by completed_at' doexpect(json_response['data'].count).to eq Spree::Order.count- expect(json_response['data'].pluck(:id)).to eq Spree::Order.select('*').order(completed_at: :asc).pluck(:id).map(&:to_s)+ expect(json_response['data'].pluck(:id)).to eq Spree::Order.select('*').order(completed_at: :desc).pluck(:id).map(&:to_s)endendenddiff --git a/core/app/models/spree/app_dependencies.rb b/core/app/models/spree/app_dependencies.rbindex 74bc338dc5..54ab158ed0 100644--- a/core/app/models/spree/app_dependencies.rb+++ b/core/app/models/spree/app_dependencies.rb@@ -12,7 +12,8 @@ module Spree:coupon_handler, :country_finder, :current_order_finder, :credit_card_finder,:completed_order_finder, :order_sorter, :cart_compare_line_items_service, :collection_paginator, :products_sorter,:products_finder, :taxon_finder, :line_item_by_variant_finder, :cart_estimate_shipping_rates_service,- :account_create_address_service, :account_update_address_service, :address_finder+ :account_create_address_service, :account_update_address_service, :address_finder,+ :collection_sorter].freezeattr_accessor *INJECTION_POINTS@@ -51,7 +52,8 @@ module Spree@checkout_get_shipping_rates_service = 'Spree::Checkout::GetShippingRates'# sorter- @order_sorter = 'Spree::Orders::Sort'+ @collection_sorter = 'Spree::BaseSorter'+ @order_sorter = 'Spree::BaseSorter'@products_sorter = 'Spree::Products::Sort'# paginatordiff --git a/core/app/sorters/spree/base_sorter.rb b/core/app/sorters/spree/base_sorter.rbnew file mode 100644index 0000000000..c3f0bfca30--- /dev/null+++ b/core/app/sorters/spree/base_sorter.rb@@ -0,0 +1,35 @@+module Spree+ class BaseSorter+ def initialize(scope, params = {}, allowed_sort_attributes = [])+ @scope = scope+ @sort = params[:sort]+ @allowed_sort_attributes = allowed_sort_attributes+ end++ def call+ by_param_attribute(scope)+ end++ protected++ attr_reader :scope, :collection, :sort, :allowed_sort_attributes++ def by_param_attribute(scope)+ return scope if sort_field.blank? || !allowed_sort_attributes.include?(sort_field.to_sym)++ scope.order("#{sort_field}": order_direction)+ end++ def desc_order+ @desc_order ||= String(sort)[0] == '-'+ end++ def sort_field+ @sort_field ||= desc_order ? sort[1..-1] : sort+ end++ def order_direction+ desc_order ? :desc : :asc+ end+ end+enddiff --git a/core/app/sorters/spree/orders/sort.rb b/core/app/sorters/spree/orders/sort.rbindex 35c230544f..e5c7a1b9dc 100644--- a/core/app/sorters/spree/orders/sort.rb+++ b/core/app/sorters/spree/orders/sort.rb@@ -1,42 +1,6 @@module Spreemodule Orders- class Sort- attr_reader :scope, :sort-- def initialize(scope, params)- @scope = scope- @sort = params[:sort]- end-- def call- orders = completed_at(scope)-- orders- end-- private-- def desc_order- @desc_order ||= String(sort)[0] == '-'- end-- def sort_field- @sort_field ||= desc_order ? sort[1..-1] : sort- end-- def order_direction- desc_order ? :asc : :desc- end-- def completed_at?- sort_field.eql?('completed_at')- end-- def completed_at(orders)- return orders unless completed_at?-- orders.order(completed_at: order_direction)- end+ class Sort < ::Spree::BaseSorterendendenddiff --git a/core/app/sorters/spree/products/sort.rb b/core/app/sorters/spree/products/sort.rbindex e0cb4b582f..590fb1d552 100644--- a/core/app/sorters/spree/products/sort.rb+++ b/core/app/sorters/spree/products/sort.rb@@ -1,53 +1,30 @@module Spreemodule Products- class Sort- def initialize(scope, params, current_currency)- @scope = scope- @sort = params[:sort]+ class Sort < ::Spree::BaseSorter+ def initialize(scope, current_currency, params = {}, allowed_sort_attributes = [])+ super(scope, params, allowed_sort_attributes)@currency = params[:currency] || current_currencyenddef call- products = updated_at(scope)- products = price(products)+ products = by_param_attribute(scope)+ products = by_price(products)products.distinctendprivate- attr_reader :sort, :scope, :currency-- def desc_order- @desc_order ||= String(sort)[0] == '-'- end-- def sort_field- @sort_field ||= desc_order ? sort[1..-1] : sort- end-- def updated_at?- sort_field == 'updated_at'- end+ attr_reader :sort, :scope, :currency, :allowed_sort_attributesdef price?sort_field == 'price'end- def order_direction- desc_order ? :desc : :asc- end-- def updated_at(products)- return products unless updated_at?-- products.order(updated_at: order_direction)- end-- def price(products)- return products unless price?+ def by_price(scope)+ return scope unless price?- products.joins(master: :prices).+ scope.joins(master: :prices).select("#{Spree::Product.table_name}.*, #{Spree::Price.table_name}.amount").distinct.where(spree_prices: { currency: currency }).