Cherry-pick c3b2f52: Cache DistributorChangeValidator#all_available_distributors, move Enterprise#available_variants into SQLland - massive render speed improvement

Conflicts:

	app/models/enterprise.rb
	lib/open_food_web/distribution_change_validator.rb
This commit is contained in:
Rohan Mitchell
2013-05-20 21:16:43 +10:00
parent 0e08829a29
commit 1b25364160
3 changed files with 7 additions and 3 deletions

View File

@@ -50,7 +50,7 @@ class Enterprise < ActiveRecord::Base
end
def distributed_variants
Spree::Product.in_distributor(self).map { |product| product.variants + [product.master] }.flatten
Spree::Variant.joins(:product).merge(Spree::Product.in_distributor(self)).select('spree_variants.*')
end

View File

@@ -40,7 +40,7 @@ Spree::Order.class_eval do
end
def line_item_variants
line_items.map{ |li| li.variant }
line_items.map { |li| li.variant }
end

View File

@@ -11,7 +11,7 @@ class DistributionChangeValidator
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 product_compatible_with_current_order(product)
@@ -28,6 +28,10 @@ class DistributionChangeValidator
distributors
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.distributed_variants).empty?