From 200729f1988c93ddf6293fc97dc37c5710b160cc Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 25 Aug 2023 15:45:52 +1000 Subject: [PATCH] Show incomplete orders when sorting by name An inner join with the billing address was hiding some orders when sorting by billing address name. Telling Rails that those fields are referenced triggers an outer left join including orders without billing address. But when the Bulk Order Management page sorts by `bill_address_lastname` then Ransack does most of the magic, except that we need to override the select in combination with distinct results. --- app/models/spree/order.rb | 6 ++++-- app/services/search_orders.rb | 5 +---- spec/system/admin/orders_spec.rb | 7 +++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 6fc2ec1ac0..ea823f06b5 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -149,11 +149,13 @@ module Spree } scope :sort_by_billing_address_name_asc, -> { - joins(:bill_address).order("spree_addresses.lastname ASC, spree_addresses.firstname ASC") + references(:bill_address) + .order("spree_addresses.lastname ASC, spree_addresses.firstname ASC") } scope :sort_by_billing_address_name_desc, -> { - joins(:bill_address).order("spree_addresses.lastname DESC, spree_addresses.firstname DESC") + references(:bill_address) + .order("spree_addresses.lastname DESC, spree_addresses.firstname DESC") } scope :with_line_items_variants_and_products_outer, lambda { diff --git a/app/services/search_orders.rb b/app/services/search_orders.rb index e9430b85ad..e8992d1b90 100644 --- a/app/services/search_orders.rb +++ b/app/services/search_orders.rb @@ -20,10 +20,7 @@ class SearchOrders ransack(params[:q]). result(distinct: true) - if ['bill_address', - 'billing_address'].any?{ |param| - params.dig(:q, :s)&.starts_with?(param) - } + if params.dig(:q, :s)&.starts_with?("bill_address_") search = search.select('spree_addresses.*, spree_orders.*') end diff --git a/spec/system/admin/orders_spec.rb b/spec/system/admin/orders_spec.rb index 2d362a6e38..d64b74eb9d 100644 --- a/spec/system/admin/orders_spec.rb +++ b/spec/system/admin/orders_spec.rb @@ -277,6 +277,13 @@ describe ' # non-empty cart order, with no with bill- and ship-address appear in the results expect(page).to have_content order_not_empty_no_address.number + + # And the same orders are displayed when sorting by name: + find("th a", text: "NAME").click + + expect(page).to have_no_content order_empty.number + expect(page).to have_content order_not_empty.number + expect(page).to have_content order_not_empty_no_address.number end end