From f6bc1a82beede476cd973e3db064486b8fdc48c3 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 22 May 2014 10:54:51 +1000 Subject: [PATCH] Enterprise user can manage enterprise relationships --- app/models/spree/ability_decorator.rb | 7 ++++++- spec/models/spree/ability_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index ee83e85bf7..44e7d5e02e 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -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 diff --git a/spec/models/spree/ability_spec.rb b/spec/models/spree/ability_spec.rb index fc13925bae..111c8e5196 100644 --- a/spec/models/spree/ability_spec.rb +++ b/spec/models/spree/ability_spec.rb @@ -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