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.
This commit is contained in:
Maikel Linke
2023-08-25 15:45:52 +10:00
parent d8da808901
commit 200729f198
3 changed files with 12 additions and 6 deletions

View File

@@ -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 {

View File

@@ -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

View File

@@ -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