diff --git a/app/controllers/admin/enterprise_fees_controller.rb b/app/controllers/admin/enterprise_fees_controller.rb index 2aa2a3aa30..3d4acb587e 100644 --- a/app/controllers/admin/enterprise_fees_controller.rb +++ b/app/controllers/admin/enterprise_fees_controller.rb @@ -49,7 +49,7 @@ module Admin end def collection - super.order('enterprise_id', 'fee_type', 'name') + EnterpriseFee.managed_by(spree_current_user).order('enterprise_id', 'fee_type', 'name') end end end diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index 05f3828ecd..8f3f038ded 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -13,6 +13,13 @@ class EnterpriseFee < ActiveRecord::Base scope :for_enterprise, lambda { |enterprise| where(enterprise_id: enterprise) } + scope :managed_by, lambda { |user| + if user.has_spree_role?('admin') + scoped + else + where('enterprise_id IN (?)', user.enterprises) + end + } def self.clear_all_adjustments_for(line_item) line_item.order.adjustments.where(originator_type: 'EnterpriseFee', source_id: line_item, source_type: 'Spree::LineItem').destroy_all diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 34bd9679a4..363a469bd8 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -40,7 +40,10 @@ class AbilityDecorator can [:create], OrderCycle - can [:index, :read], EnterpriseFee + can [:admin, :index, :read], EnterpriseFee do |enterprise_fee| + user.enterprises.include? enterprise_fee.enterprise + end + can [:admin, :index, :read, :create, :edit, :update], ExchangeVariant can [:admin, :index, :read, :create, :edit, :update], Exchange can [:admin, :index, :read, :create, :edit, :update], ExchangeFee diff --git a/spec/features/admin/order_cycles_spec.rb b/spec/features/admin/order_cycles_spec.rb index 9697504901..716894272b 100644 --- a/spec/features/admin/order_cycles_spec.rb +++ b/spec/features/admin/order_cycles_spec.rb @@ -349,7 +349,7 @@ feature %q{ let(:supplier2) { create(:supplier_enterprise, name: 'Another Supplier') } let(:distributor1) { create(:distributor_enterprise, name: 'First Distributor') } let(:distributor2) { create(:distributor_enterprise, name: 'Another Distributor') } - + let!(:distributor1_fee) { create(:enterprise_fee, enterprise: distributor1, name: 'First Distributor Fee') } before(:each) do product = create(:product, supplier: supplier1) product.distributors << distributor1 @@ -384,6 +384,8 @@ feature %q{ click_button 'Add supplier' select 'First Distributor', from: 'order_cycle_coordinator_id' + click_button 'Add coordinator fee' + select 'First Distributor Fee', from: 'order_cycle_coordinator_fee_0_id' select 'First Distributor', from: 'new_distributor_id' click_button 'Add distributor' diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 0d9bb31cc3..05bb1d39ad 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -136,6 +136,10 @@ module Spree it "should be able to create OrderCycles" do should have_ability([:create], for: OrderCycle) end + + it "should be able to read EnterpriseFees" do + should have_ability([:admin, :index, :read], for: EnterpriseFee) + end end context 'Enterprise manager' do @@ -160,4 +164,4 @@ module Spree end end end -end \ No newline at end of file +end