Enterprise user can manage enterprise relationships

This commit is contained in:
Rohan Mitchell
2014-05-22 10:54:51 +10:00
parent 48a7b9c3f8
commit f6bc1a82be
2 changed files with 33 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
class AbilityDecorator
include CanCan::Ability
def initialize(user)
if user.enterprises.count > 0
@@ -53,6 +53,11 @@ class AbilityDecorator
(user.enterprises & shipping_method.distributors).any?
end
can [:admin, :index, :create], EnterpriseRelationship
can [:destroy], EnterpriseRelationship do |enterprise_relationship|
user.enterprises.include? enterprise_relationship.parent
end
can [:create], OrderCycle
can [:admin, :index, :read, :edit, :update, :bulk_update, :clone], OrderCycle do |order_cycle|
user.enterprises.include? order_cycle.coordinator

View File

@@ -17,6 +17,9 @@ module Spree
let(:p1) { create(:product, supplier: s1, distributors:[d1, d2]) }
let(:p2) { create(:product, supplier: s2, distributors:[d1, d2]) }
let(:er1) { create(:enterprise_relationship, parent: s1, child: d1) }
let(:er2) { create(:enterprise_relationship, parent: d1, child: s1) }
subject { user }
let(:user) { nil }
@@ -72,6 +75,18 @@ module Spree
should have_ability([:admin, :index, :read, :create, :edit], for: Spree::Classification)
end
it "should be able to read and create enterprise relationships" do
should have_ability([:admin, :index, :create], for: EnterpriseRelationship)
end
it "should be able to destroy enterprise relationships for its enterprises" do
should have_ability(:destroy, for: er1)
end
it "should not be able to destroy enterprise relationships for other enterprises" do
should_not have_ability(:destroy, for: er2)
end
end
context "when is a distributor enterprise user" do
@@ -146,6 +161,18 @@ module Spree
it "should be able to read/write ShippingMethods" do
should have_ability([:admin, :index, :create, :update, :destroy], for: Spree::ShippingMethod)
end
it "should be able to read and create enterprise relationships" do
should have_ability([:admin, :index, :create], for: EnterpriseRelationship)
end
it "should be able to destroy enterprise relationships for its enterprises" do
should have_ability(:destroy, for: er2)
end
it "should not be able to destroy enterprise relationships for other enterprises" do
should_not have_ability(:destroy, for: er1)
end
end
context 'Order Cycle co-ordinator' do