mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-05 02:41:33 +00:00
Reduce initial queries on shop page by ~95%
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
Darkswarm.controller "AboutUsCtrl", ($scope, CurrentHub) ->
|
||||
$scope.CurrentHub = CurrentHub
|
||||
Darkswarm.controller "AboutUsCtrl", ($scope, Shopfront) ->
|
||||
$scope.shopfront = Shopfront.shopfront
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 }}
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user