Renaming granting > related_enterprises_granting

This commit is contained in:
Rob Harrington
2015-05-06 12:14:23 +10:00
parent 5cd528a87d
commit 5806f50a84
3 changed files with 18 additions and 18 deletions

View File

@@ -19,7 +19,7 @@ module OpenFoodNetwork
if @coordinator.sells == "any"
# If the coordinator sells any, relationships come into play
granting(:add_to_order_cycle, to: [@coordinator]).pluck(:id).each do |enterprise_id|
related_enterprises_granting(:add_to_order_cycle, to: [@coordinator]).pluck(:id).each do |enterprise_id|
coordinator_permitted << enterprise_id
end
@@ -30,19 +30,19 @@ module OpenFoodNetwork
Enterprise.where(id: coordinator_permitted | all_active)
else
# Any enterprises that I manage directly, which have granted P-OC to the coordinator
managed_permitted = granting(:add_to_order_cycle, to: [@coordinator], scope: managed_participating_enterprises ).pluck(:id)
managed_permitted = related_enterprises_granting(:add_to_order_cycle, to: [@coordinator], scope: managed_participating_enterprises ).pluck(:id)
# Any hubs in this OC that have been granted P-OC by producers I manage in this OC
hubs_permitted = granted(:add_to_order_cycle, by: managed_participating_producers, scope: @order_cycle.distributors).pluck(:id)
# Any hubs in this OC that have granted P-OC to producers I manage in this OC
hubs_permitting = granting(:add_to_order_cycle, to: managed_participating_producers, scope: @order_cycle.distributors).pluck(:id)
hubs_permitting = related_enterprises_granting(:add_to_order_cycle, to: managed_participating_producers, scope: @order_cycle.distributors).pluck(:id)
# Any producers in this OC that have been granted P-OC by hubs I manage in this OC
producers_permitted = granted(:add_to_order_cycle, by: managed_participating_hubs, scope: @order_cycle.suppliers).pluck(:id)
# Any producers in this OC that have granted P-OC to hubs I manage in this OC
producers_permitting = granting(:add_to_order_cycle, to: managed_participating_hubs, scope: @order_cycle.suppliers).pluck(:id)
producers_permitting = related_enterprises_granting(:add_to_order_cycle, to: managed_participating_hubs, scope: @order_cycle.suppliers).pluck(:id)
managed_active = []
hubs_active = []
@@ -125,7 +125,7 @@ module OpenFoodNetwork
end
# Any variants of any producers that have granted the hub P-OC
producers = granting(:add_to_order_cycle, to: [hub], scope: Enterprise.is_primary_producer)
producers = related_enterprises_granting(:add_to_order_cycle, to: [hub], scope: Enterprise.is_primary_producer)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producers)
# PLUS any variants that are already in an outgoing exchange of this hub, so things don't break
@@ -138,7 +138,7 @@ module OpenFoodNetwork
Spree::Variant.where(id: coordinator_variants | permitted_variants | active_variants)
else
# Any variants produced by MY PRODUCERS that are in this order cycle, where my producer has granted P-OC to the hub
producers = granting(:add_to_order_cycle, to: [hub], scope: managed_participating_producers)
producers = related_enterprises_granting(:add_to_order_cycle, to: [hub], scope: managed_participating_producers)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producers)
# PLUS any of my incoming producers' variants that are already in an outgoing exchange of this hub, so things don't break
@@ -162,7 +162,7 @@ module OpenFoodNetwork
end
# Any variants of any producers that have granted the hub P-OC
producers = granting(:add_to_order_cycle, to: [hub], scope: Enterprise.is_primary_producer)
producers = related_enterprises_granting(:add_to_order_cycle, to: [hub], scope: Enterprise.is_primary_producer)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', producers)
# PLUS any variants that are already in an outgoing exchange of this hub, so things don't break
@@ -178,7 +178,7 @@ module OpenFoodNetwork
granted_producers = granted(:add_to_order_cycle, by: [hub], scope: managed_participating_producers)
# Any variants produced by MY PRODUCERS that are in this order cycle, where my producer has granted P-OC to the hub
granting_producers = granting(:add_to_order_cycle, to: [hub], scope: granted_producers)
granting_producers = related_enterprises_granting(:add_to_order_cycle, to: [hub], scope: granted_producers)
permitted_variants = Spree::Variant.joins(:product).where('spree_products.supplier_id IN (?)', granting_producers)
Spree::Variant.where(id: permitted_variants)
@@ -216,7 +216,7 @@ module OpenFoodNetwork
# Find my managed hubs in this order cycle
hubs = managed_participating_hubs
# Any incoming exchange where the producer has granted P-OC to one or more of those hubs
producers = granting(:add_to_order_cycle, to: hubs, scope: Enterprise.is_primary_producer).pluck :id
producers = related_enterprises_granting(:add_to_order_cycle, to: hubs, scope: Enterprise.is_primary_producer).pluck :id
permitted_exchanges = @order_cycle.exchanges.incoming.where(sender_id: producers).pluck :id
# TODO: remove active_exchanges when we think it is safe to do so

View File

@@ -78,7 +78,7 @@ module OpenFoodNetwork
where(
"spree_orders.distributor_id IN (?) AND spree_products.supplier_id IN (?)",
granted_distributors,
granting(:add_to_order_cycle, to: granted_distributors).merge(managed_enterprises.is_primary_producer)
related_enterprises_granting(:add_to_order_cycle, to: granted_distributors).merge(managed_enterprises.is_primary_producer)
).pluck(:id)
Spree::Order.where(id: editable | produced)
@@ -133,7 +133,7 @@ module OpenFoodNetwork
def managed_and_related_enterprises_granting(permission)
managed_enterprise_ids = managed_enterprises.pluck :id
permitting_enterprise_ids = granting(permission).pluck :id
permitting_enterprise_ids = related_enterprises_granting(permission).pluck :id
Enterprise.where('id IN (?)', managed_enterprise_ids + permitting_enterprise_ids)
end
@@ -148,7 +148,7 @@ module OpenFoodNetwork
@coordinated_order_cycles = OrderCycle.managed_by(@user)
end
def granting(permission, options={})
def related_enterprises_granting(permission, options={})
parent_ids = EnterpriseRelationship.
permitting(options[:to] || managed_enterprises).
with_permission(permission).
@@ -171,7 +171,7 @@ module OpenFoodNetwork
end
def related_enterprise_products
Spree::Product.where('supplier_id IN (?)', granting(:manage_products))
Spree::Product.where('supplier_id IN (?)', related_enterprises_granting(:manage_products))
end
end
end

View File

@@ -154,19 +154,19 @@ module OpenFoodNetwork
it "returns the enterprises" do
permissions.stub(:managed_enterprises) { e2 }
permissions.send(:granting, permission).should == [e1]
permissions.send(:related_enterprises_granting, permission).should == [e1]
end
it "returns an empty array when there are none" do
permissions.stub(:managed_enterprises) { e1 }
permissions.send(:granting, permission).should == []
permissions.send(:related_enterprises_granting, permission).should == []
end
end
describe "finding enterprises that are managed or with a particular permission" do
before do
permissions.stub(:managed_enterprises) { Enterprise.where('1=0') }
permissions.stub(:granting) { Enterprise.where('1=0') }
permissions.stub(:related_enterprises_granting) { Enterprise.where('1=0') }
end
it "returns managed enterprises" do
@@ -175,7 +175,7 @@ module OpenFoodNetwork
end
it "returns permitted enterprises" do
permissions.should_receive(:granting).with(permission).
permissions.should_receive(:related_enterprises_granting).with(permission).
and_return(Enterprise.where(id: e2))
permissions.send(:managed_and_related_enterprises_granting, permission).should == [e2]
end
@@ -186,7 +186,7 @@ module OpenFoodNetwork
let!(:p) { create(:simple_product, supplier: e) }
it "returns supplied products" do
permissions.should_receive(:granting).with(:manage_products) { [e] }
permissions.should_receive(:related_enterprises_granting).with(:manage_products) { [e] }
permissions.send(:related_enterprise_products).should == [p]
end