From 276b94de5bd7bb971c25b4f64e6acda0b5691391 Mon Sep 17 00:00:00 2001 From: Cillian O'Ruanaidh Date: Fri, 28 Jul 2023 14:20:29 +0100 Subject: [PATCH] Sort orders by last name, then first name i.e. the same way bulk order management works --- app/models/spree/order.rb | 11 ++++++++++- app/views/spree/admin/orders/_table.html.haml | 2 +- app/views/spree/admin/orders/_table_row.html.haml | 2 +- config/locales/en.yml | 2 +- spec/system/admin/orders_spec.rb | 14 +++++++------- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index dead0129fc..6ba226f222 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -16,7 +16,8 @@ module Spree searchable_attributes :number, :state, :shipment_state, :payment_state, :distributor_id, :order_cycle_id, :email, :total, :customer_id searchable_associations :shipping_method, :bill_address, :distributor - searchable_scopes :complete, :incomplete + searchable_scopes :complete, :incomplete, :sort_by_billing_address_name_asc, + :sort_by_billing_address_name_desc checkout_flow do go_to_state :address @@ -145,6 +146,14 @@ module Spree end } + scope :sort_by_billing_address_name_asc, -> { + joins(: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") + } + scope :with_line_items_variants_and_products_outer, lambda { left_outer_joins(line_items: { variant: :product }) } diff --git a/app/views/spree/admin/orders/_table.html.haml b/app/views/spree/admin/orders/_table.html.haml index f15cc261d1..237e309106 100644 --- a/app/views/spree/admin/orders/_table.html.haml +++ b/app/views/spree/admin/orders/_table.html.haml @@ -14,7 +14,7 @@ %th = t(:products_distributor) - - columns = ['completed_at', 'number', 'state', 'payment_state', 'shipment_state', 'email', 'bill_address_lastname', 'total'] + - columns = ['completed_at', 'number', 'state', 'payment_state', 'shipment_state', 'email', 'billing_address_name', 'total'] = render partial: "spree/admin/shared/stimulus_sortable_header", collection: columns, as: :column, locals: { sorted: params.dig(:q, :s), default: "completed_at desc" } diff --git a/app/views/spree/admin/orders/_table_row.html.haml b/app/views/spree/admin/orders/_table_row.html.haml index b65e59a399..3f8feb8d42 100644 --- a/app/views/spree/admin/orders/_table_row.html.haml +++ b/app/views/spree/admin/orders/_table_row.html.haml @@ -33,7 +33,7 @@ %a{ href: "mailto:#{order.email}", target: "_blank" } = order.email %td - = order.bill_address.full_name + = order.bill_address.full_name_for_sorting %td.align-center %span = order.display_total diff --git a/config/locales/en.yml b/config/locales/en.yml index 21a9671552..cdfd69c746 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4346,7 +4346,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using shipment_state: "Shipment State" email: "Email" total: "Total" - bill_address_lastname: "Name" + billing_address_name: "Name" general_settings: edit: legal_settings: "Legal Settings" diff --git a/spec/system/admin/orders_spec.rb b/spec/system/admin/orders_spec.rb index 2696a84687..61b9173199 100644 --- a/spec/system/admin/orders_spec.rb +++ b/spec/system/admin/orders_spec.rb @@ -410,22 +410,22 @@ describe ' context "orders with different billing addresses" do before do - billing_address2.update!(lastname: "Mad Hatter") - billing_address3.update!(lastname: "Duchess") - billing_address4.update!(lastname: "Cheshire Cat") - billing_address5.update!(lastname: "Alice") + billing_address2.update!(firstname: "Mad", lastname: "Hatter") + billing_address3.update!(firstname: "Alice", lastname: "Smith") + billing_address4.update!(firstname: "Cheshire", lastname: "Cat") + billing_address5.update!(lastname: "Bob", lastname: "Smith") login_as_admin visit spree.admin_orders_path end - it "orders by last name" do + it "orders by last name then first name" do find("a", text: 'NAME').click # sets ascending ordering expect(page).to have_content( - /#{order5.number}.*#{order4.number}.*#{order3.number}.*#{order2.number}/m + /#{order4.number}.*#{order2.number}.*#{order3.number}.*#{order5.number}/m ) find("a", text: 'NAME').click # sets descending ordering expect(page).to have_content( - /#{order2.number}.*#{order3.number}.*#{order4.number}.*#{order5.number}/m + /#{order5.number}.*#{order3.number}.*#{order2.number}.*#{order4.number}/m ) end end