diff --git a/app/models/enterprise_relationship.rb b/app/models/enterprise_relationship.rb index ff2c1fe3fa..600ef87f11 100644 --- a/app/models/enterprise_relationship.rb +++ b/app/models/enterprise_relationship.rb @@ -15,6 +15,7 @@ class EnterpriseRelationship < ActiveRecord::Base } scope :permitting, ->(enterprises) { where('child_id IN (?)', enterprises) } + scope :permitted_by, ->(enterprises) { where('parent_id IN (?)', enterprises) } scope :with_permission, ->(permission) { joins(:permissions). diff --git a/spec/models/enterprise_relationship_spec.rb b/spec/models/enterprise_relationship_spec.rb index 3a6c48e891..e87c204037 100644 --- a/spec/models/enterprise_relationship_spec.rb +++ b/spec/models/enterprise_relationship_spec.rb @@ -44,12 +44,18 @@ describe EnterpriseRelationship do end end - it "finds relationships that grant permissions to some enterprises" do - er1 = create(:enterprise_relationship, parent: e2, child: e1) - er2 = create(:enterprise_relationship, parent: e3, child: e2) - er3 = create(:enterprise_relationship, parent: e1, child: e3) + describe "finding by permission" do + let!(:er1) { create(:enterprise_relationship, parent: e2, child: e1) } + let!(:er2) { create(:enterprise_relationship, parent: e3, child: e2) } + let!(:er3) { create(:enterprise_relationship, parent: e1, child: e3) } - EnterpriseRelationship.permitting([e1, e2]).sort.should == [er1, er2] + it "finds relationships that grant permissions to some enterprises" do + EnterpriseRelationship.permitting([e1, e2]).sort.should == [er1, er2].sort + end + + it "finds relationships that are granted by particular enterprises" do + EnterpriseRelationship.permitted_by([e1, e2]).sort.should == [er1, er3].sort + end end it "finds relationships that grant a particular permission" do