Files
openfoodnetwork/app/services/search_orders.rb
Maikel Linke 200729f198 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.
2023-08-25 16:15:11 +10:00

44 lines
1.0 KiB
Ruby

# frozen_string_literal: true
class SearchOrders
attr_reader :orders
def initialize(params, current_user)
@params = params
@current_user = current_user
@orders = fetch_orders
end
private
attr_reader :params, :current_user
def fetch_orders
search = search_query.
includes(:payments, :subscription, :shipments, :bill_address, :distributor, :order_cycle).
ransack(params[:q]).
result(distinct: true)
if params.dig(:q, :s)&.starts_with?("bill_address_")
search = search.select('spree_addresses.*, spree_orders.*')
end
search
end
def search_query
base_query = ::Permissions::Order.new(current_user).editable_orders.not_empty
.or(::Permissions::Order.new(current_user).editable_orders.finalized)
return base_query if params[:shipping_method_id].blank?
base_query
.joins(shipments: :shipping_rates)
.where(spree_shipping_rates: {
selected: true,
shipping_method_id: params[:shipping_method_id]
})
end
end