From 785595a9519d2fd5122f32991d053dbcc0c1fec7 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sat, 11 May 2019 23:55:09 +0100 Subject: [PATCH] Reduce initial queries on shop page by ~95% --- .../tabs/about_us_controller.js.coffee | 4 ++-- .../tabs/producers_controller.js.coffee | 6 ++---- .../darkswarm/services/products.js.coffee | 4 ++-- .../darkswarm/services/shopfront.js.coffee | 8 ++++++++ app/controllers/enterprises_controller.rb | 7 ++----- app/helpers/injection_helper.rb | 8 ++++++++ .../api/enterprise_shopfront_serializer.rb | 5 ++++- app/views/enterprises/shop.html.haml | 2 +- app/views/shopping_shared/_about.html.haml | 4 ++-- app/views/shopping_shared/_producers.html.haml | 4 ++-- .../darkswarm/services/products_spec.js.coffee | 16 ++++++++++------ 11 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 app/assets/javascripts/darkswarm/services/shopfront.js.coffee diff --git a/app/assets/javascripts/darkswarm/controllers/tabs/about_us_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/tabs/about_us_controller.js.coffee index c86cf12286..3fe16194f0 100644 --- a/app/assets/javascripts/darkswarm/controllers/tabs/about_us_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/tabs/about_us_controller.js.coffee @@ -1,2 +1,2 @@ -Darkswarm.controller "AboutUsCtrl", ($scope, CurrentHub) -> - $scope.CurrentHub = CurrentHub +Darkswarm.controller "AboutUsCtrl", ($scope, Shopfront) -> + $scope.shopfront = Shopfront.shopfront diff --git a/app/assets/javascripts/darkswarm/controllers/tabs/producers_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/tabs/producers_controller.js.coffee index 7a5ebb78f3..7d28868856 100644 --- a/app/assets/javascripts/darkswarm/controllers/tabs/producers_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/tabs/producers_controller.js.coffee @@ -1,4 +1,2 @@ -Darkswarm.controller "ProducersTabCtrl", ($scope, CurrentHub, Enterprises, EnterpriseModal) -> - # Injecting Enterprises so CurrentHub.producers is dereferenced. - # We should probably dereference here instead and separate out CurrentHub dereferencing from the Enterprise factory. - $scope.CurrentHub = CurrentHub +Darkswarm.controller "ProducersTabCtrl", ($scope, Shopfront, EnterpriseModal) -> + $scope.shopfront = Shopfront.shopfront diff --git a/app/assets/javascripts/darkswarm/services/products.js.coffee b/app/assets/javascripts/darkswarm/services/products.js.coffee index 6c6e222330..0a24cbd374 100644 --- a/app/assets/javascripts/darkswarm/services/products.js.coffee +++ b/app/assets/javascripts/darkswarm/services/products.js.coffee @@ -1,4 +1,4 @@ -Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons, Properties, Cart, Variants) -> +Darkswarm.factory 'Products', ($resource, Shopfront, Dereferencer, Taxons, Properties, Cart, Variants) -> new class Products constructor: -> @update() @@ -31,7 +31,7 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons, Pro dereference: -> for product in @products - product.supplier = Enterprises.enterprises_by_id[product.supplier.id] + product.supplier = Shopfront.producers_by_id[product.supplier.id] Dereferencer.dereference product.taxons, Taxons.taxons_by_id product.properties = angular.copy(product.properties_with_values) diff --git a/app/assets/javascripts/darkswarm/services/shopfront.js.coffee b/app/assets/javascripts/darkswarm/services/shopfront.js.coffee new file mode 100644 index 0000000000..3c4812a676 --- /dev/null +++ b/app/assets/javascripts/darkswarm/services/shopfront.js.coffee @@ -0,0 +1,8 @@ +Darkswarm.factory 'Shopfront', (shopfront) -> + new class Shopfront + shopfront: shopfront + producers_by_id: {} + + constructor: -> + for producer in shopfront.producers + @producers_by_id[producer.id] = producer diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb index ebc139c461..18ca712c9f 100644 --- a/app/controllers/enterprises_controller.rb +++ b/app/controllers/enterprises_controller.rb @@ -4,6 +4,7 @@ class EnterprisesController < BaseController layout "darkswarm" helper Spree::ProductsHelper include OrderCyclesHelper + include SerializerHelper # These prepended filters are in the reverse order of execution prepend_before_filter :set_order_cycles, :require_distributor_chosen, :reset_order, only: :shop @@ -17,11 +18,7 @@ class EnterprisesController < BaseController return redirect_to spree.cart_path unless enough_stock? set_noindex_meta_tag - @enterprises = current_distributor - .plus_relatives_and_oc_producers(shop_order_cycles) - .activated - .includes(address: :state) - .all + @enterprise = current_distributor end def relatives diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index e83a1dbe36..aa85939fbe 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -22,6 +22,14 @@ module InjectionHelper ) end + def inject_enterprise_shopfront(enterprise) + inject_json_ams( + "shopfront", + enterprise, + Api::EnterpriseShopfrontSerializer + ) + end + def inject_enterprise_shopfront_list select_only = required_attributes Enterprise, Api::EnterpriseShopfrontListSerializer diff --git a/app/serializers/api/enterprise_shopfront_serializer.rb b/app/serializers/api/enterprise_shopfront_serializer.rb index e7b645def6..89c5e68f5b 100644 --- a/app/serializers/api/enterprise_shopfront_serializer.rb +++ b/app/serializers/api/enterprise_shopfront_serializer.rb @@ -51,7 +51,10 @@ module Api def producers ActiveModel::ArraySerializer.new( - enterprise.suppliers, each_serializer: Api::EnterpriseThinSerializer + enterprise.plus_relatives_and_oc_producers( + OrderCycle.not_closed.with_distributor(enterprise) + ), + each_serializer: Api::EnterpriseThinSerializer ) end diff --git a/app/views/enterprises/shop.html.haml b/app/views/enterprises/shop.html.haml index ed27346d2d..108ad893e9 100644 --- a/app/views/enterprises/shop.html.haml +++ b/app/views/enterprises/shop.html.haml @@ -5,7 +5,7 @@ - content_for(:image) do = current_distributor.logo.url -= inject_enterprises(@enterprises) += inject_enterprise_shopfront(@enterprise) %shop.darkswarm - if @shopfront_layout == 'embedded' diff --git a/app/views/shopping_shared/_about.html.haml b/app/views/shopping_shared/_about.html.haml index e4d03a1d2c..138f5ad44b 100644 --- a/app/views/shopping_shared/_about.html.haml +++ b/app/views/shopping_shared/_about.html.haml @@ -3,7 +3,7 @@ .panel .row .small-12.large-8.columns - %img.hero-img-small{"ng-src" => "{{::CurrentHub.hub.promo_image}}", "ng-if" => "::CurrentHub.hub.promo_image"} - %p{"ng-bind-html" => "::CurrentHub.hub.long_description"} + %img.hero-img-small{"ng-src" => "{{::shopfront.promo_image}}", "ng-if" => "::shopfront.promo_image"} + %p{"ng-bind-html" => "::shopfront.long_description"} .small-12.large-4.columns   diff --git a/app/views/shopping_shared/_producers.html.haml b/app/views/shopping_shared/_producers.html.haml index 754a26b265..ed22bebd00 100644 --- a/app/views/shopping_shared/_producers.html.haml +++ b/app/views/shopping_shared/_producers.html.haml @@ -4,9 +4,9 @@ .row .small-12.columns %h5 - = t :shopping_producers_of_hub, hub: '{{CurrentHub.hub.name}}' + = t :shopping_producers_of_hub, hub: '{{ shopfront.name }}' %ul.small-block-grid-2.large-block-grid-4 - %li{"ng-repeat" => "enterprise in CurrentHub.hub.producers"} + %li{"ng-repeat" => "enterprise in shopfront.producers"} %enterprise-modal %i.ofn-i_036-producers {{ enterprise.name }} diff --git a/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee index 62eeaf823f..0075750fba 100644 --- a/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee @@ -1,10 +1,10 @@ describe 'Products service', -> $httpBackend = null Products = null - Enterprises = null + Shopfront = null Variants = null Cart = null - CurrentHubMock = {} + shopfront = null currentOrder = null product = null productWithImage = null @@ -34,10 +34,14 @@ describe 'Products service', -> { id: 1, name: "some property" } taxons = { id: 2, name: "some taxon" } + shopfront = + producers: + id: 9, + name: "Test" module 'Darkswarm' module ($provide)-> - $provide.value "CurrentHub", CurrentHubMock + $provide.value "shopfront", shopfront $provide.value "currentOrder", currentOrder $provide.value "taxons", taxons $provide.value "properties", properties @@ -46,7 +50,7 @@ describe 'Products service', -> inject ($injector, _$httpBackend_)-> Products = $injector.get("Products") - Enterprises = $injector.get("Enterprises") + Shopfront = $injector.get("Shopfront") Properties = $injector.get("Properties") Variants = $injector.get("Variants") Cart = $injector.get("Cart") @@ -58,11 +62,11 @@ describe 'Products service', -> expect(Products.products[0].test).toEqual "cats" it "dereferences suppliers", -> - Enterprises.enterprises_by_id = + Shopfront.producers_by_id = {id: 9, name: "test"} $httpBackend.expectGET("/shop/products").respond([{supplier : {id: 9}, master: {}}]) $httpBackend.flush() - expect(Products.products[0].supplier).toBe Enterprises.enterprises_by_id["9"] + expect(Products.products[0].supplier).toBe Shopfront.producers_by_id["9"] it "dereferences taxons", -> product.taxons = [2]