mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Convert the manipulation of arrays into active record relations: let the DB do the matching job
This commit is contained in:
@@ -15,43 +15,33 @@ class ExchangeProductsRenderer
|
||||
private
|
||||
|
||||
def products_for_incoming_exchange(enterprise)
|
||||
supplied_products(enterprise)
|
||||
supplied_products(enterprise.id)
|
||||
end
|
||||
|
||||
def supplied_products(enterprise)
|
||||
def supplied_products(enterprises_query_matcher)
|
||||
products_relation = Spree::Product.where(supplier_id: enterprises_query_matcher)
|
||||
|
||||
if @order_cycle.present? &&
|
||||
@order_cycle.prefers_product_selection_from_coordinator_inventory_only?
|
||||
enterprise.supplied_products.visible_for(@order_cycle.coordinator)
|
||||
else
|
||||
enterprise.supplied_products
|
||||
products_relation = products_relation.visible_for(@order_cycle.coordinator)
|
||||
end
|
||||
|
||||
products_relation
|
||||
end
|
||||
|
||||
def products_for_outgoing_exchange
|
||||
products = []
|
||||
enterprises_for_outgoing_exchange.each do |enterprise|
|
||||
products.push( *supplied_products(enterprise).to_a )
|
||||
|
||||
products.each do |product|
|
||||
unless product_supplied_to_order_cycle?(product)
|
||||
products.delete(product)
|
||||
end
|
||||
end
|
||||
end
|
||||
products
|
||||
end
|
||||
|
||||
def product_supplied_to_order_cycle?(product)
|
||||
(product.variants.map(&:id) & incoming_exchanges_variants).any?
|
||||
supplied_products(enterprises_for_outgoing_exchange.select(:id)).
|
||||
includes(:variants).
|
||||
where("spree_variants.id": incoming_exchanges_variants)
|
||||
end
|
||||
|
||||
def incoming_exchanges_variants
|
||||
return @incoming_exchanges_variants if @incoming_exchanges_variants.present?
|
||||
|
||||
@incoming_exchanges_variants = []
|
||||
visible_incoming_exchanges.each do |exchange|
|
||||
visible_incoming_exchanges.each do |incoming_exchange|
|
||||
@incoming_exchanges_variants.push(
|
||||
*exchange.variants.merge(visible_incoming_variants(exchange)).map(&:id).to_a
|
||||
*incoming_exchange.variants.merge(visible_incoming_variants(incoming_exchange.sender)).map(&:id).to_a
|
||||
)
|
||||
end
|
||||
@incoming_exchanges_variants
|
||||
@@ -65,17 +55,20 @@ class ExchangeProductsRenderer
|
||||
incoming
|
||||
end
|
||||
|
||||
def visible_incoming_variants(exchange)
|
||||
if exchange.order_cycle.prefers_product_selection_from_coordinator_inventory_only?
|
||||
permitted_incoming_variants(exchange).visible_for(exchange.order_cycle.coordinator)
|
||||
else
|
||||
permitted_incoming_variants(exchange)
|
||||
def visible_incoming_variants(incoming_exchange_sender)
|
||||
variants_relation = permitted_incoming_variants(incoming_exchange_sender)
|
||||
|
||||
if @order_cycle.prefers_product_selection_from_coordinator_inventory_only?
|
||||
variants_relation = variants_relation.visible_for(@order_cycle.coordinator)
|
||||
end
|
||||
|
||||
variants_relation
|
||||
end
|
||||
|
||||
def permitted_incoming_variants(exchange)
|
||||
OpenFoodNetwork::OrderCyclePermissions.new(@user, exchange.order_cycle).
|
||||
visible_variants_for_incoming_exchanges_from(exchange.sender)
|
||||
def permitted_incoming_variants(incoming_exchange_sender)
|
||||
OpenFoodNetwork::OrderCyclePermissions.
|
||||
new(@user, @order_cycle).
|
||||
visible_variants_for_incoming_exchanges_from(incoming_exchange_sender)
|
||||
end
|
||||
|
||||
def enterprises_for_outgoing_exchange
|
||||
|
||||
Reference in New Issue
Block a user