From cca15e75a6ccc79d207443ee709305871746eea8 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 28 Dec 2018 20:40:32 +0100 Subject: [PATCH] Refactor to reveal how we load enterprises in shop By sticking to Rails conventions we make it more obvious how (badly) we are loading enterprises in EnterprisesController#shop and shed some light on the obscure InjectionHelper. This will also make it easier to improve its performance in the future as it's among the top offenders. See https://www.skylight.io/app/applications/ibo3NOqCYMnq/1545851520/1d/endpoints/EnterprisesController%23shop?responseType=html --- app/controllers/enterprises_controller.rb | 27 ++++++++++++++++++++++- app/helpers/injection_helper.rb | 9 -------- app/views/enterprises/shop.html.haml | 2 +- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb index 00af18d701..6a817a47d7 100644 --- a/app/controllers/enterprises_controller.rb +++ b/app/controllers/enterprises_controller.rb @@ -7,13 +7,28 @@ class EnterprisesController < BaseController # These prepended filters are in the reverse order of execution prepend_before_filter :set_order_cycles, :require_distributor_chosen, :reset_order, only: :shop - before_filter :check_stock_levels, :set_noindex_meta_tag, only: :shop before_filter :clean_permalink, only: :check_permalink before_filter :enable_embedded_shopfront respond_to :js, only: :permalink_checker + def shop + check_stock_levels + set_noindex_meta_tag + + order_cycles = if current_order_cycle + [current_order_cycle] + else + OrderCycle.not_closed.with_distributor(current_distributor) + end + + enterprises = current_distributor.plus_relatives_and_oc_producers(order_cycles).activated.includes(address: :state).all + enterprises = inject_json_ams('enterprises', enterprises) + + render locals: { enterprises: enterprises } + end + def relatives set_enterprise @@ -88,4 +103,14 @@ class EnterprisesController < BaseController def set_noindex_meta_tag @noindex_meta_tag = true unless current_distributor.visible? end + + def inject_json_ams(name, object) + options = { + each_serializer: Api::EnterpriseSerializer, + data: OpenFoodNetwork::EnterpriseInjectionData.new + } + serializer_instance = ActiveModel::ArraySerializer.new(object, options) + + { name: name, json: serializer_instance.to_json } + end end diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index dfcf2bb6ff..52d178cb74 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -14,15 +14,6 @@ module InjectionHelper inject_json_ams "enterprises", current_distributor.relatives_including_self.activated.includes(address: :state).all, Api::EnterpriseSerializer, enterprise_injection_data end - def inject_shop_enterprises - ocs = if current_order_cycle - [current_order_cycle] - else - OrderCycle.not_closed.with_distributor(current_distributor) - end - inject_json_ams "enterprises", current_distributor.plus_relatives_and_oc_producers(ocs).activated.includes(address: :state).all, Api::EnterpriseSerializer, enterprise_injection_data - end - def inject_group_enterprises inject_json_ams "group_enterprises", @group.enterprises.activated.all, Api::EnterpriseSerializer, enterprise_injection_data end diff --git a/app/views/enterprises/shop.html.haml b/app/views/enterprises/shop.html.haml index f05ac115f3..041c8be07b 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_shop_enterprises += render partial: 'json/injection_ams', locals: enterprises %shop.darkswarm - if @shopfront_layout == 'embedded'