Merge pull request #5022 from Matt-Yorkley/performance_tweaks

Performance tweaks
This commit is contained in:
Luis Ramos
2020-03-25 15:42:07 +00:00
committed by GitHub
5 changed files with 44 additions and 8 deletions

View File

@@ -32,6 +32,12 @@ module Admin
end
end
def edit
@object = Enterprise.where(permalink: params[:id]).
includes(users: [:ship_address, :bill_address]).first
super
end
def welcome
render layout: "spree/layouts/bare_admin"
end
@@ -172,12 +178,14 @@ module Admin
end
def load_methods_and_fees
enterprise_payment_methods = @enterprise.payment_methods.to_a
enterprise_shipping_methods = @enterprise.shipping_methods.to_a
# rubocop:disable Style/TernaryParentheses
@payment_methods = Spree::PaymentMethod.managed_by(spree_current_user).sort_by! do |pm|
[(@enterprise.payment_methods.include? pm) ? 0 : 1, pm.name]
[(enterprise_payment_methods.include? pm) ? 0 : 1, pm.name]
end
@shipping_methods = Spree::ShippingMethod.managed_by(spree_current_user).sort_by! do |sm|
[(@enterprise.shipping_methods.include? sm) ? 0 : 1, sm.name]
[(enterprise_shipping_methods.include? sm) ? 0 : 1, sm.name]
end
# rubocop:enable Style/TernaryParentheses

View File

@@ -8,7 +8,7 @@ class ProducersController < BaseController
.activated
.visible
.is_primary_producer
.includes(address: :state)
.includes(address: [:state, :country])
.includes(:properties)
.includes(supplied_products: :properties)
.all

View File

@@ -8,7 +8,7 @@ class ShopsController < BaseController
.activated
.visible
.is_distributor
.includes(address: :state)
.includes(address: [:state, :country])
.includes(:properties)
.includes(supplied_products: :properties)
.all

View File

@@ -3,10 +3,10 @@ require 'open_food_network/enterprise_injection_data'
module InjectionHelper
include SerializerHelper
def inject_enterprises(enterprises = Enterprise.activated.includes(address: :state).all)
def inject_enterprises(enterprises = nil)
inject_json_ams(
"enterprises",
enterprises,
enterprises || default_enterprise_query,
Api::EnterpriseSerializer,
enterprise_injection_data
)
@@ -35,13 +35,21 @@ module InjectionHelper
inject_json_ams(
"enterprises",
Enterprise.activated.visible.select(select_only).includes(address: :state).all,
Enterprise.activated.visible.select(select_only).includes(address: [:state, :country]).all,
Api::EnterpriseShopfrontListSerializer
)
end
def inject_enterprise_and_relatives
inject_json_ams "enterprises", current_distributor.relatives_including_self.activated.includes(address: :state).all, Api::EnterpriseSerializer, enterprise_injection_data
enterprises_and_relatives = current_distributor.
relatives_including_self.
activated.
includes(address: [:state, :country]).
all
inject_json_ams "enterprises",
enterprises_and_relatives,
Api::EnterpriseSerializer, enterprise_injection_data
end
def inject_group_enterprises
@@ -138,6 +146,10 @@ module InjectionHelper
private
def default_enterprise_query
Enterprise.activated.includes(address: [:state, :country]).all
end
def enterprise_injection_data
@enterprise_injection_data ||= OpenFoodNetwork::EnterpriseInjectionData.new
{ data: @enterprise_injection_data }

View File

@@ -5,5 +5,21 @@ module Spree
def variant_options(v, _options = {})
v.options_text
end
# Overriden to eager-load :states
def available_countries
checkout_zone = Zone.find_by_name(Spree::Config[:checkout_zone])
countries = if checkout_zone && checkout_zone.kind == 'country'
checkout_zone.country_list
else
Country.includes(:states).all
end
countries.collect do |country|
country.name = Spree.t(country.iso, scope: 'country_names', default: country.name)
country
end.sort { |a, b| a.name <=> b.name }
end
end
end