Add permitted_by scope to EnterpriseRelationship

This commit is contained in:
Rohan Mitchell
2015-01-30 16:36:45 +11:00
parent fafbfe8735
commit c0a7d22a50
2 changed files with 12 additions and 5 deletions

View File

@@ -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).

View File

@@ -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