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
This commit is contained in:
Pau Perez
2018-12-28 20:40:32 +01:00
parent d88be202a6
commit cca15e75a6
3 changed files with 27 additions and 11 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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'