enterprises_for is now order_cycle_enterprises_for, and can be passed an order_cycle or a coordinator (for new order_cycles)

This commit is contained in:
Rob Harrington
2015-03-20 12:22:46 +11:00
parent c9f343f680
commit 7e24e6743e
3 changed files with 45 additions and 28 deletions

View File

@@ -8,7 +8,7 @@
- unless order_cycles_simple_index
%td.suppliers
- suppliers = order_cycle.suppliers.merge(OpenFoodNetwork::Permissions.new(spree_current_user).enterprises_for(order_cycle))
- suppliers = order_cycle.suppliers.merge(OpenFoodNetwork::Permissions.new(spree_current_user).enterprises_for(order_cycle: order_cycle)))
- supplier_list = suppliers.map(&:name).sort.join ', '
- if suppliers.count > 3
%span.with-tip{'data-powertip' => supplier_list}
@@ -18,7 +18,7 @@
= supplier_list
%td= order_cycle.coordinator.name
%td.distributors
- distributors = order_cycle.distributors.merge(OpenFoodNetwork::Permissions.new(spree_current_user).enterprises_for(order_cycle))
- distributors = order_cycle.distributors.merge(OpenFoodNetwork::Permissions.new(spree_current_user).enterprises_for(order_cycle: order_cycle)))
- distributor_list = distributors.map(&:name).sort.join ', '
- if distributors.count > 3
%span.with-tip{'data-powertip' => distributor_list}

View File

@@ -15,31 +15,48 @@ module OpenFoodNetwork
managed_and_related_enterprises_with :add_to_order_cycle
end
# List of any enterprises whose exchanges I should be able to see in order_cycles
def enterprises_for(order_cycle)
# List of any enterprises whose exchanges I should be able to see in order_cycle
# NOTE: the enterprises a given user can see actually in the OC interface depend on the relationships
# of their enterprises to the coordinator of the order cycle, rather than on the order cycle itself
# (until such time as we implement friends of friends)
def order_cycle_enterprises_for(options={})
# Can provide a coordinator OR an order cycle. Use just coordinator for new order cycles
# if both are provided, coordinator will be ignored, and the coordinator of the OC will be used
return Enterprise.where("1=0") unless options[:coordinator] || options[:order_cycle]
coordinator = options[:coordinator]
order_cycle = nil
if options[:order_cycle]
order_cycle = options[:order_cycle]
coordinator = order_cycle.coordinator
end
# If I manage the coordinator (or possibly in the future, if coordinator has made order cycle a friends of friend OC)
# Any hubs that have granted the coordinator P-OC (or any enterprises that have granted mine P-OC if we do friends of friends)
coordinator_permitted = []
if managed_enterprises.include? order_cycle.coordinator
coordinator_permitted = granting(:add_to_order_cycle, to: [order_cycle.coordinator]).pluck(:id)
coordinator_permitted << order_cycle.coordinator
if managed_enterprises.include? coordinator
coordinator_permitted = granting(:add_to_order_cycle, to: [coordinator]).pluck(:id)
coordinator_permitted << coordinator
end
# Any enterprises that I manage directly, which have granted P-OC to the coordinator
managed_permitted = granting(:add_to_order_cycle, to: [order_cycle.coordinator], scope: managed_enterprises).pluck(:id)
# TODO: remove this when permissions are all sorted out
# Any enterprises that I manage that are already in the order_cycle
managed_active = managed_enterprises.where(id: order_cycle.suppliers | order_cycle.distributors).pluck(:id)
managed_permitted = granting(:add_to_order_cycle, to: [coordinator], scope: managed_enterprises).pluck(:id)
# Any hubs that have been granted P-OC by producers I manage
hubs_permitted = granted(:add_to_order_cycle, by: managed_enterprises.is_primary_producer, scope: Enterprise.is_hub).pluck(:id)
# TODO: Remove this when all P-OC are sorted out
# Any hubs that currently have outgoing exchanges distributing variants of producers I manage
variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', managed_enterprises.is_primary_producer)
active_exchanges = order_cycle.exchanges.outgoing.with_any_variant(variants)
hubs_active = active_exchanges.map(&:receiver_id)
managed_active = []
hubs_active = []
if order_cycle
# TODO: remove this when permissions are all sorted out
# Any enterprises that I manage that are already in the order_cycle
managed_active = managed_enterprises.where(id: order_cycle.suppliers | order_cycle.distributors).pluck(:id)
# TODO: Remove this when all P-OC are sorted out
# Any hubs that currently have outgoing exchanges distributing variants of producers I manage
variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', managed_enterprises.is_primary_producer)
active_exchanges = order_cycle.exchanges.outgoing.with_any_variant(variants)
hubs_active = active_exchanges.map(&:receiver_id)
end
Enterprise.where(id: coordinator_permitted | managed_permitted | managed_active | hubs_permitted | hubs_active)
end

View File

@@ -31,14 +31,14 @@ module OpenFoodNetwork
it "returns enterprises which have granted P-OC to the coordinator" do
create(:enterprise_relationship, parent: hub, child: coordinator, permissions_list: [:add_to_order_cycle])
permissions.stub(:managed_enterprises) { Enterprise.where(id: [coordinator]) }
enterprises = permissions.enterprises_for(oc)
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to include hub
expect(enterprises).to_not include producer
end
it "returns the coordinator itself" do
permissions.stub(:managed_enterprises) { Enterprise.where(id: [coordinator]) }
expect(permissions.enterprises_for(oc)).to include coordinator
expect(permissions.order_cycle_enterprises_for(order_cycle: oc)).to include coordinator
end
end
@@ -53,7 +53,7 @@ module OpenFoodNetwork
end
it "returns my hub" do
enterprises = permissions.enterprises_for(oc)
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to include hub
expect(enterprises).to_not include producer, coordinator
end
@@ -63,7 +63,7 @@ module OpenFoodNetwork
create(:enterprise_relationship, parent: hub, child: producer, permissions_list: [:add_to_order_cycle])
end
it "does not return that producer" do
enterprises = permissions.enterprises_for(oc)
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to_not include producer
end
end
@@ -71,7 +71,7 @@ module OpenFoodNetwork
context "that has not granted P-OC to the coordinator" do
it "does not return my hub" do
enterprises = permissions.enterprises_for(oc)
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to_not include hub, producer, coordinator
end
@@ -79,7 +79,7 @@ module OpenFoodNetwork
let!(:ex) { create(:exchange, order_cycle: oc, sender: coordinator, receiver: hub, incoming: false) }
it "returns my hub" do
enterprises = permissions.enterprises_for(oc)
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to include hub
expect(enterprises).to_not include producer, coordinator
end
@@ -98,7 +98,7 @@ module OpenFoodNetwork
end
it "returns my producer, and the coordindator itself" do
enterprises = permissions.enterprises_for(oc)
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to include producer, coordinator
expect(enterprises).to_not include hub
end
@@ -109,7 +109,7 @@ module OpenFoodNetwork
end
it "returns that hub as well" do
enterprises = permissions.enterprises_for(oc)
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to include producer, coordinator, hub
end
end
@@ -117,7 +117,7 @@ module OpenFoodNetwork
context "which has not granted P-OC to the coordinator" do
it "does not return my producer" do
enterprises = permissions.enterprises_for(oc)
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to_not include producer
end
@@ -126,7 +126,7 @@ module OpenFoodNetwork
# TODO: update this when we are confident about P-OCs
it "returns my producer" do
enterprises = permissions.enterprises_for(oc)
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to include producer
expect(enterprises).to_not include hub, coordinator
end
@@ -137,7 +137,7 @@ module OpenFoodNetwork
# TODO: update this when we are confident about P-OCs
it "returns that hub as well" do
enterprises = permissions.enterprises_for(oc)
enterprises = permissions.order_cycle_enterprises_for(order_cycle: oc)
expect(enterprises).to include producer, hub
expect(enterprises).to_not include coordinator
end