Files
openfoodnetwork/app/services/search_orders.rb
Matt-Yorkley 0c5ac99e0f Fix multiple results with 'inventory_items_shipment_id_null' ransack query
The q['inventory_items_shipment_id_null'] search element was generating duplicates where there were multiple line items on an order that were ready to be shipped, with id = null
2018-12-10 20:48:53 +00:00

42 lines
823 B
Ruby

class SearchOrders
attr_reader :orders
def initialize(params, current_user)
@params = params
@current_user = current_user
@orders = fetch_orders
end
def pagination_data
return unless using_pagination?
{
results: @orders.total_count,
pages: @orders.num_pages,
page: params[:page].to_i,
per_page: params[:per_page].to_i
}
end
private
attr_reader :params, :current_user
def fetch_orders
@search = OpenFoodNetwork::Permissions.new(current_user).editable_orders.ransack(params[:q])
return paginated_results if using_pagination?
@search.result(distinct: true)
end
def paginated_results
@search.result(distinct: true)
.page(params[:page])
.per(params[:per_page])
end
def using_pagination?
params[:per_page]
end
end