From 2a5403e23f92801c09d4f3d547c83b47935be765 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 7 Aug 2019 19:39:51 +0100 Subject: [PATCH] Clean up "visible and editable" variants --- .../order_cycle_permissions.rb | 52 ++++++++----------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/lib/open_food_network/order_cycle_permissions.rb b/lib/open_food_network/order_cycle_permissions.rb index bff84a1477..bfd3631120 100644 --- a/lib/open_food_network/order_cycle_permissions.rb +++ b/lib/open_food_network/order_cycle_permissions.rb @@ -162,22 +162,7 @@ module OpenFoodNetwork return Spree::Variant.where("1=0") unless @order_cycle if user_manages_coordinator_or(hub) - # TODO: Use variants_stockable_by(hub) for this? - - # Any variants of any producers that have granted the hub P-OC - producer_ids = related_enterprises_granting(:add_to_order_cycle, - to: [hub.id], - scope: Enterprise.is_primary_producer) - permitted_variants = Spree::Variant.joins(:product). - where('spree_products.supplier_id IN (?)', producer_ids) - - hub_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id = (?)', hub) - - # PLUS variants that are already in an outgoing exchange of this hub, so things don't break - active_variants = active_outgoing_variants(hub) - - Spree::Variant. - where(id: hub_variants | permitted_variants | active_variants) + visible_and_editable_variants(hub) else # Variants produced by MY PRODUCERS that are in this OC, # where my producer has granted P-OC to the hub @@ -205,20 +190,7 @@ module OpenFoodNetwork return Spree::Variant.where("1=0") unless @order_cycle if user_manages_coordinator_or(hub) - # Any variants of any producers that have granted the hub P-OC - producer_ids = related_enterprises_granting(:add_to_order_cycle, - to: [hub.id], - scope: Enterprise.is_primary_producer) - permitted_variants = Spree::Variant.joins(:product). - where('spree_products.supplier_id IN (?)', producer_ids) - - hub_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id = (?)', hub) - - # PLUS variants that are already in an outgoing exchange of this hub, so things don't break - active_variants = active_outgoing_variants(hub) - - Spree::Variant. - where(id: hub_variants | permitted_variants | active_variants) + visible_and_editable_variants(hub) else # Any of my managed producers in this order cycle granted P-OC by the hub granted_producers = related_enterprises_granted(:add_to_order_cycle, @@ -239,6 +211,26 @@ module OpenFoodNetwork private + def visible_and_editable_variants(hub) + # Producers that have granted the hub P-OC + producer_ids = related_enterprises_granting(:add_to_order_cycle, + to: [hub.id], + scope: Enterprise.is_primary_producer) + + # Variants from Producers via permissions, and from the hub itself + available_variants = variants_from_suppliers(producer_ids.push(hub.id)) + + # PLUS variants that are already in an outgoing exchange of this hub, so things don't break + active_variants = active_outgoing_variants(hub) + + Spree::Variant.where(id: available_variants | active_variants) + end + + def variants_from_suppliers(supplier_ids) + Spree::Variant.joins(:product). + where('spree_products.supplier_id IN (?)', supplier_ids) + end + def active_outgoing_variants(hub) active_variants = [] @order_cycle.exchanges.outgoing.where(receiver_id: hub).limit(1).each do |exchange|