Improving performance of available variant lookup for standing orders

Only search the DB for available variants once
This commit is contained in:
Rob Harrington
2017-03-08 12:19:50 +11:00
parent 846cccf373
commit 42266397aa
2 changed files with 11 additions and 7 deletions

View File

@@ -217,14 +217,24 @@ class StandingOrderForm
end
def standing_line_items_available?
available_variant_ids = variant_ids_for_shop_and_schedule
standing_line_items.each do |sli|
unless sli.available_from?(shop_id, schedule_id)
unless available_variant_ids.include? sli.variant_id
name = "#{sli.variant.product.name} - #{sli.variant.full_name}"
errors.add(:standing_line_items, :not_available, name: name)
end
end
end
def variant_ids_for_shop_and_schedule
Spree::Variant.joins(exchanges: { order_cycle: :schedules})
.where(id: standing_line_items.map(&:variant_id))
.where(schedules: { id: schedule}, exchanges: { incoming: false, receiver_id: shop })
.merge(OrderCycle.not_closed)
.select('DISTINCT spree_variants.id')
.pluck(:id)
end
def build_msg_from(k, msg)
return msg[1..-1] if msg.starts_with?("^")
errors.full_message(k,msg)

View File

@@ -6,12 +6,6 @@ class StandingLineItem < ActiveRecord::Base
validates :variant, presence: true
validates :quantity, { presence: true, numericality: { only_integer: true } }
def available_from?(shop, schedule)
Spree::Variant.joins(exchanges: { order_cycle: :schedules})
.where(id: variant_id, schedules: { id: schedule}, exchanges: { incoming: false, receiver_id: shop })
.any?
end
def total_estimate
(price_estimate || 0) * (quantity || 0)
end