mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
Cache DistributorChangeValidator#all_available_distributors, move Enterprise#available_variants into SQLland - massive render speed improvement
This commit is contained in:
@@ -27,9 +27,9 @@ class Enterprise < ActiveRecord::Base
|
||||
def to_param
|
||||
"#{id}-#{name.parameterize}"
|
||||
end
|
||||
|
||||
|
||||
def available_variants
|
||||
ProductDistribution.find_all_by_distributor_id( self.id ).map{ |pd| pd.product.variants + [pd.product.master] }.flatten
|
||||
Spree::Variant.joins(:product => :product_distributions).where('product_distributions.distributor_id=?', self.id)
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ Spree::Order.class_eval do
|
||||
end
|
||||
|
||||
def line_item_variants
|
||||
line_items.map{ |li| li.variant }
|
||||
line_items.map { |li| li.variant }
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -11,12 +11,16 @@ class DistributorChangeValidator
|
||||
|
||||
def can_change_to_distributor? distributor
|
||||
# Distributor may not be changed once an item has been added to the cart/order, unless all items are available from the specified distributor
|
||||
@order.line_items.empty? || (available_distributors(Enterprise.all) || []).include?(distributor)
|
||||
@order.line_items.empty? || all_available_distributors.include?(distributor)
|
||||
end
|
||||
|
||||
|
||||
def all_available_distributors
|
||||
@all_available_distributors ||= (available_distributors(Enterprise.all) || [])
|
||||
end
|
||||
|
||||
def available_distributors enterprises
|
||||
enterprises.select do |e|
|
||||
(@order.line_item_variants - e.available_variants).empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user