diff --git a/app/services/search_orders.rb b/app/services/search_orders.rb index 60cf824e63..ce41bb036e 100644 --- a/app/services/search_orders.rb +++ b/app/services/search_orders.rb @@ -19,7 +19,17 @@ class SearchOrders includes(:payments, :subscription, :shipments, :bill_address, :distributor, :order_cycle). ransack(params[:q]) - @search.result(distinct: true).joins(:bill_address) + @search = @search.result(distinct: true) + + if ['bill_address', + 'billing_address'].any?{ |param| + params.dig(:q, :s)&.starts_with?(param) + } + @search = @search.left_joins(:bill_address). + select('spree_addresses.*, spree_orders.*') + end + + @search end def search_query diff --git a/app/views/spree/admin/orders/_table_row.html.haml b/app/views/spree/admin/orders/_table_row.html.haml index 3f8feb8d42..52875e7008 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_for_sorting + = order&.bill_address.full_name_for_sorting %td.align-center %span = order.display_total diff --git a/spec/controllers/api/v0/orders_controller_spec.rb b/spec/controllers/api/v0/orders_controller_spec.rb index 773a9170b8..3f2e710bc2 100644 --- a/spec/controllers/api/v0/orders_controller_spec.rb +++ b/spec/controllers/api/v0/orders_controller_spec.rb @@ -149,6 +149,33 @@ module Api .map{ |o| o[:id] }).to eq serialized_orders([order2, order3, order1, order4]) .map{ |o| o["id"] } end + + context "with an order without billing address" do + let!(:order7) { + create(:order_with_line_items, billing_address: nil, order_cycle: order_cycle, + distributor: distributor) + } + + it 'can show orders without bill address' do + get :index, params: { q: {} }, + as: :json + + expect(json_response['orders'] + .map{ |o| o[:id] }).to match_array serialized_orders([order2, order3, order1, order4, + order7]) + .map{ |o| + o["id"] + } + end + + it 'can sort orders by bill_address.lastname' do + get :index, params: { q: { s: 'bill_address_lastname ASC' } }, + as: :json + expect(json_response['orders'] + .map{ |o| o[:id] }).to eq serialized_orders([order2, order3, order1, order4, order7]) + .map{ |o| o["id"] } + end + end end context 'with pagination' do