mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-01 06:41:41 +00:00
Improving performance of available variant lookup for standing orders
Only search the DB for available variants once
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user