Update accessible_by scope on enterprise, to read from permissions

This commit is contained in:
Rob Harrington
2015-04-29 13:44:00 +10:00
parent 89b153dc2c
commit d8c23d37ac
2 changed files with 7 additions and 10 deletions

View File

@@ -162,14 +162,12 @@ class Enterprise < ActiveRecord::Base
end
}
# Return enterprises that participate in order cycles that user coordinates, sends to or receives from
# Return enterprises that the user manages and those that have granted P-OC to managed enterprises
scope :accessible_by, lambda { |user|
if user.has_spree_role?('admin')
scoped
else
with_order_cycles_outer.
where('order_cycles.id IN (?)', OrderCycle.accessible_by(user)).
select('DISTINCT enterprises.*')
where(id: OpenFoodNetwork::Permissions.new(user).order_cycle_enterprises)
end
}

View File

@@ -560,20 +560,19 @@ describe Enterprise do
end
describe "accessible_by" do
it "shows only enterprises that are invloved in order cycles which are common to those managed by the given user" do
it "shows only managed enterprises and enterprises granting them P-OC" do
user = create(:user)
user.spree_roles = []
e1 = create(:enterprise)
e2 = create(:enterprise)
e3 = create(:enterprise)
e4 = create(:enterprise)
e1.enterprise_roles.build(user: user).save
oc = create(:simple_order_cycle, coordinator: e2, suppliers: [e1], distributors: [e3])
create(:enterprise_relationship, parent: e2, child: e1, permissions_list: [:add_to_order_cycle])
enterprises = Enterprise.accessible_by user
enterprises.length.should == 3
enterprises.should include e1, e2, e3
enterprises.should_not include e4
enterprises.length.should == 2
enterprises.should include e1, e2
enterprises.should_not include e3
end
it "shows all enterprises for admin user" do