From 51177b833e1e2f4e9abb2cbdecbe46730cd68e36 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 29 Nov 2019 17:09:12 +0100 Subject: [PATCH 1/4] Remove customer_code N+1 from packing reports --- lib/open_food_network/packing_report.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/open_food_network/packing_report.rb b/lib/open_food_network/packing_report.rb index 3f5e420626..82cb79d4de 100644 --- a/lib/open_food_network/packing_report.rb +++ b/lib/open_food_network/packing_report.rb @@ -95,7 +95,7 @@ module OpenFoodNetwork def columns if is_by_customer? [proc { |line_items| line_items.first.order.distributor.name }, - proc { |line_items| customer_code(line_items.first.order.email) }, + proc { |line_items| customer_code(line_items.first.order) }, proc { |line_items| line_items.first.order.bill_address.firstname }, proc { |line_items| line_items.first.order.bill_address.lastname }, proc { |line_items| line_items.first.product.supplier.name }, @@ -107,7 +107,7 @@ module OpenFoodNetwork [ proc { |line_items| line_items.first.order.distributor.name }, proc { |line_items| line_items.first.product.supplier.name }, - proc { |line_items| customer_code(line_items.first.order.email) }, + proc { |line_items| customer_code(line_items.first.order) }, proc { |line_items| line_items.first.order.bill_address.firstname }, proc { |line_items| line_items.first.order.bill_address.lastname }, proc { |line_items| line_items.first.product.name }, @@ -125,7 +125,7 @@ module OpenFoodNetwork end def line_item_includes - [{ order: [:bill_address, :distributor], + [{ order: [:bill_address, :distributor, :customer], variant: [{ option_values: :option_type }, { product: :supplier }] }] end @@ -146,8 +146,8 @@ module OpenFoodNetwork params[:report_type] == "pack_by_customer" end - def customer_code(email) - customer = Customer.where(email: email).first + def customer_code(order) + customer = order.customer customer.nil? ? "" : customer.code end end From 38c327dae032c8a2a5113cb1abe2240038992bc9 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 29 Nov 2019 17:10:23 +0100 Subject: [PATCH 2/4] Improve N+1 issues around #suppliers_of_products_distributed_by There's still some real mess here with repeating queries, but resolving it is out of scope for this quick PR --- app/controllers/spree/admin/reports_controller_decorator.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 3a7f5217fc..1a21606017 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -222,7 +222,8 @@ Spree::Admin::ReportsController.class_eval do end def suppliers_of_products_distributed_by(distributors) - distributors.map { |d| Spree::Product.in_distributor(d) }.flatten.map(&:supplier).uniq + distributors.map { |d| Spree::Product.in_distributor(d).includes(:supplier).all }. + flatten.map(&:supplier).uniq end # Load order cycles the current user has access to From 9bcd303f4fb72cca9abdfc2a326b980959758ee2 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 29 Nov 2019 17:11:40 +0100 Subject: [PATCH 3/4] Remove shipping_category N+1 from packing reports --- lib/open_food_network/packing_report.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/open_food_network/packing_report.rb b/lib/open_food_network/packing_report.rb index 82cb79d4de..dc10020e6a 100644 --- a/lib/open_food_network/packing_report.rb +++ b/lib/open_food_network/packing_report.rb @@ -126,7 +126,8 @@ module OpenFoodNetwork def line_item_includes [{ order: [:bill_address, :distributor, :customer], - variant: [{ option_values: :option_type }, { product: :supplier }] }] + variant: [{ option_values: [:option_type] }, + { product: [:supplier, :shipping_category] }] }] end def permissions From 6d1fb63a21eef9d10de32508c1a555de09c66812 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 29 Nov 2019 17:39:03 +0100 Subject: [PATCH 4/4] Eager-load option_values on line_item objects instead of variants in packing reports. --- lib/open_food_network/packing_report.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/open_food_network/packing_report.rb b/lib/open_food_network/packing_report.rb index dc10020e6a..87ef459c65 100644 --- a/lib/open_food_network/packing_report.rb +++ b/lib/open_food_network/packing_report.rb @@ -125,9 +125,9 @@ module OpenFoodNetwork end def line_item_includes - [{ order: [:bill_address, :distributor, :customer], - variant: [{ option_values: [:option_type] }, - { product: [:supplier, :shipping_category] }] }] + [{ option_values: :option_type, + order: [:bill_address, :distributor, :customer], + variant: { product: [:supplier, :shipping_category] } }] end def permissions