From d8c23d37ac02c3cf6f4ecf0dbbeef3fffcc54e24 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Wed, 29 Apr 2015 13:44:00 +1000 Subject: [PATCH] Update accessible_by scope on enterprise, to read from permissions --- app/models/enterprise.rb | 6 ++---- spec/models/enterprise_spec.rb | 11 +++++------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 1aa76b88a8..3cf00730a8 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -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 } diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 69ba68569f..df5dbbfbc7 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -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