Find enterprises that we manage products for

This commit is contained in:
Rohan Mitchell
2014-09-01 10:36:59 +10:00
parent cfb31b46e4
commit 4d8d74dec7
2 changed files with 51 additions and 15 deletions

View File

@@ -6,10 +6,7 @@ module OpenFoodNetwork
# Find enterprises that an admin is allowed to add to an order cycle
def order_cycle_enterprises
managed_enterprise_ids = managed_enterprises.pluck :id
permitted_enterprise_ids = related_enterprises_with(:add_to_order_cycle).pluck :id
Enterprise.where('id IN (?)', managed_enterprise_ids + permitted_enterprise_ids)
managed_and_related_enterprises_with :add_to_order_cycle
end
# Find the exchanges of an order cycle that an admin can manage
@@ -24,6 +21,10 @@ module OpenFoodNetwork
Spree::Product.where('id IN (?)', managed_enterprise_products_ids + permitted_enterprise_products_ids)
end
def managed_product_enterprises
managed_and_related_enterprises_with :manage_products
end
private
@@ -40,6 +41,14 @@ module OpenFoodNetwork
Enterprise.where('id IN (?)', parent_ids)
end
def managed_and_related_enterprises_with(permission)
managed_enterprise_ids = managed_enterprises.pluck :id
permitted_enterprise_ids = related_enterprises_with(permission).pluck :id
Enterprise.where('id IN (?)', managed_enterprise_ids + permitted_enterprise_ids)
end
def managed_enterprise_products
Spree::Product.managed_by(@user)
end

View File

@@ -9,19 +9,15 @@ module OpenFoodNetwork
let(:e2) { create(:enterprise) }
describe "finding enterprises that can be added to an order cycle" do
before do
permissions.stub(:managed_enterprises) { Enterprise.where('1=0') }
permissions.stub(:related_enterprises_with) { Enterprise.where('1=0') }
end
let(:e) { double(:enterprise) }
it "returns managed enterprises" do
permissions.stub(:managed_enterprises) { Enterprise.where(id: e1) }
permissions.order_cycle_enterprises.should == [e1]
end
it "returns managed and related enterprises with add_to_order_cycle permission" do
permissions.
should_receive(:managed_and_related_enterprises_with).
with(:add_to_order_cycle).
and_return([e])
it "returns permitted enterprises" do
permissions.stub(:related_enterprises_with) { Enterprise.where(id: e2) }
permissions.order_cycle_enterprises.should == [e2]
permissions.order_cycle_enterprises.should == [e]
end
end
@@ -75,6 +71,19 @@ module OpenFoodNetwork
end
end
describe "finding enterprises that we manage products for" do
let(:e) { double(:enterprise) }
it "returns managed and related enterprises with manage_products permission" do
permissions.
should_receive(:managed_and_related_enterprises_with).
with(:manage_products).
and_return([e])
permissions.managed_product_enterprises.should == [e]
end
end
########################################
describe "finding related enterprises with a particular permission" do
@@ -91,6 +100,24 @@ module OpenFoodNetwork
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(:related_enterprises_with) { Enterprise.where('1=0') }
end
it "returns managed enterprises" do
permissions.should_receive(:managed_enterprises) { Enterprise.where(id: e1) }
permissions.send(:managed_and_related_enterprises_with, permission).should == [e1]
end
it "returns permitted enterprises" do
permissions.should_receive(:related_enterprises_with).with(permission).
and_return(Enterprise.where(id: e2))
permissions.send(:managed_and_related_enterprises_with, permission).should == [e2]
end
end
describe "finding the supplied products of related enterprises" do
let!(:e) { create(:enterprise) }
let!(:p) { create(:simple_product, supplier: e) }