From 237ebd6ec4a93e8319139dc8df9092612f1ef76b Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Sat, 8 May 2021 21:09:06 +0530 Subject: [PATCH 1/5] Removed N+1 queries while counting line_items --- app/serializers/api/order_serializer.rb | 2 +- app/views/spree/users/show.html.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/serializers/api/order_serializer.rb b/app/serializers/api/order_serializer.rb index f741675318..740d8a9fb2 100644 --- a/app/serializers/api/order_serializer.rb +++ b/app/serializers/api/order_serializer.rb @@ -22,7 +22,7 @@ module Api end def item_count - object.line_items.sum(:quantity) + object.line_items.size end def completed_at diff --git a/app/views/spree/users/show.html.haml b/app/views/spree/users/show.html.haml index 6fa0d875e2..c85ae06d75 100644 --- a/app/views/spree/users/show.html.haml +++ b/app/views/spree/users/show.html.haml @@ -1,5 +1,5 @@ - content_for :injection_data do - = inject_json_array("orders", @orders.all, Api::OrderSerializer) + = inject_json_array("orders", @orders.all.includes(:line_items), Api::OrderSerializer) = inject_json_array("shops", @shops.all, Api::ShopForOrdersSerializer) = inject_saved_credit_cards From 026942dd72e606bd3b97c8ef4bb3519564cf7e7d Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Sat, 8 May 2021 23:45:41 +0530 Subject: [PATCH 2/5] Fix item_count to properly count quantity of every item --- app/serializers/api/order_serializer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/serializers/api/order_serializer.rb b/app/serializers/api/order_serializer.rb index 740d8a9fb2..f9d8ae857f 100644 --- a/app/serializers/api/order_serializer.rb +++ b/app/serializers/api/order_serializer.rb @@ -14,7 +14,7 @@ module Api end def payments - object.payments.joins(:payment_method).where('state IN (?)', %w(completed pending)) + object.payments.joins(:payment_method).includes(:spree_payments).where('state IN (?)', %w(completed pending)) end def shop_id @@ -22,7 +22,7 @@ module Api end def item_count - object.line_items.size + object.line_items.inject(0) {|sum, line_item| sum + line_item.quantity} end def completed_at From e485a4a8efc502cdbdfedbf9e27bd5990a323083 Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Sat, 8 May 2021 23:51:38 +0530 Subject: [PATCH 3/5] Remove untested change to payments --- app/serializers/api/order_serializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/serializers/api/order_serializer.rb b/app/serializers/api/order_serializer.rb index f9d8ae857f..9bc533a52e 100644 --- a/app/serializers/api/order_serializer.rb +++ b/app/serializers/api/order_serializer.rb @@ -14,7 +14,7 @@ module Api end def payments - object.payments.joins(:payment_method).includes(:spree_payments).where('state IN (?)', %w(completed pending)) + object.payments.joins(:payment_method).where('state IN (?)', %w(completed pending)) end def shop_id From fcf70c242e4ec6bd3a00d5a9285f6fa411bd5b9a Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Sun, 9 May 2021 18:31:26 +0530 Subject: [PATCH 4/5] edit item_count method to shorthand for improved readability --- app/serializers/api/order_serializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/serializers/api/order_serializer.rb b/app/serializers/api/order_serializer.rb index 9bc533a52e..3123b973a9 100644 --- a/app/serializers/api/order_serializer.rb +++ b/app/serializers/api/order_serializer.rb @@ -22,7 +22,7 @@ module Api end def item_count - object.line_items.inject(0) {|sum, line_item| sum + line_item.quantity} + object.line_items.sum(&:quantity) end def completed_at From 367cee593fbfa435ea04b9d51600f99017ce08d7 Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Thu, 13 May 2021 01:12:13 +0530 Subject: [PATCH 5/5] Move eager loading line_items to controller --- app/controllers/spree/users_controller.rb | 2 +- app/views/spree/users/show.html.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/spree/users_controller.rb b/app/controllers/spree/users_controller.rb index a591e20064..18effc349d 100644 --- a/app/controllers/spree/users_controller.rb +++ b/app/controllers/spree/users_controller.rb @@ -14,7 +14,7 @@ module Spree def show @payments_requiring_action = PaymentsRequiringAction.new(spree_current_user).query - @orders = orders_collection + @orders = orders_collection.includes(:line_items) customers = spree_current_user.customers @shops = Enterprise diff --git a/app/views/spree/users/show.html.haml b/app/views/spree/users/show.html.haml index c85ae06d75..6fa0d875e2 100644 --- a/app/views/spree/users/show.html.haml +++ b/app/views/spree/users/show.html.haml @@ -1,5 +1,5 @@ - content_for :injection_data do - = inject_json_array("orders", @orders.all.includes(:line_items), Api::OrderSerializer) + = inject_json_array("orders", @orders.all, Api::OrderSerializer) = inject_json_array("shops", @shops.all, Api::ShopForOrdersSerializer) = inject_saved_credit_cards