From f870e705602324966fffae84b961ca7dede3bf7f Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 22 Aug 2013 11:40:33 +1000 Subject: [PATCH] Enterprise manager can edit and bulk edit enterprises they have access to --- .../admin/enterprises_controller.rb | 6 +++- app/models/spree/ability_decorator.rb | 3 +- spec/features/admin/enterprises_spec.rb | 28 ++++++++++++++++++- spec/models/ability_spec.rb | 4 +-- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 981299a8ea..d524966d7c 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -24,7 +24,11 @@ module Admin end def collection - super.managed_by(spree_current_user).order('is_primary_producer DESC, is_distributor ASC, name') + Enterprise.managed_by(spree_current_user).order('is_primary_producer DESC, is_distributor ASC, name') + end + + def collection_actions + [:index, :bulk_update] end end end diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index ccd4252897..5c674fea09 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -47,8 +47,9 @@ class AbilityDecorator can [:admin, :index, :read, :create, :edit, :update], ExchangeVariant can [:admin, :index, :read, :create, :edit, :update], Exchange can [:admin, :index, :read, :create, :edit, :update], ExchangeFee + can [:admin, :index], Enterprise - can [:read, :edit, :update], Enterprise do |enterprise| + can [:read, :edit, :update, :bulk_update], Enterprise do |enterprise| user.enterprises.include? enterprise end diff --git a/spec/features/admin/enterprises_spec.rb b/spec/features/admin/enterprises_spec.rb index db97f28b9f..211ca93bbd 100644 --- a/spec/features/admin/enterprises_spec.rb +++ b/spec/features/admin/enterprises_spec.rb @@ -105,6 +105,7 @@ feature %q{ click_button 'Update' flash_message.should == 'Enterprise "Eaterprises" has been successfully updated!' + page.should have_selector '#listing_enterprises a', text: 'Eaterprises' end @@ -128,7 +129,6 @@ feature %q{ end context 'as an Enterprise user' do - let(:supplier1) { create(:supplier_enterprise, name: 'First Supplier') } let(:supplier2) { create(:supplier_enterprise, name: 'Another Supplier') } let(:distributor1) { create(:distributor_enterprise, name: 'First Distributor') } @@ -153,5 +153,31 @@ feature %q{ page.should_not have_content supplier2.name page.should_not have_content distributor2.name end + + scenario "can edit enterprises I have permission to" do + click_link 'Enterprises' + within('#listing_enterprises tbody tr:first') { click_link 'Edit' } + + fill_in 'enterprise_name', :with => 'Eaterprises' + click_button 'Update' + + flash_message.should == 'Enterprise "Eaterprises" has been successfully updated!' + page.should have_selector '#listing_enterprises a', text: 'Eaterprises' + end + + scenario "can bulk edit enterprise collection dates/times for enterprises I have permission to" do + click_link 'Enterprises' + + fill_in 'enterprise_set_collection_attributes_0_next_collection_at', :with => 'One' + fill_in 'enterprise_set_collection_attributes_1_next_collection_at', :with => 'Two' + click_button 'Update' + + flash_message.should == 'Distributor collection times updated.' + + supplier1.reload.next_collection_at.should == 'One' + distributor1.reload.next_collection_at.should == 'Two' + supplier2.reload.next_collection_at.should be_nil + distributor2.reload.next_collection_at.should be_nil + end end end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index e4d04a69ab..b31258bfc2 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -151,11 +151,11 @@ module Spree end it 'should have the ability to read and edit enterprises that I manage' do - should have_ability([:read, :edit, :update], for: s1) + should have_ability([:read, :edit, :update, :bulk_update], for: s1) end it 'should not have the ability to read and edit enterprises that I do not manage' do - should_not have_ability([:read, :edit, :update], for: s2) + should_not have_ability([:read, :edit, :update, :bulk_update], for: s2) end it 'should have the ability administrate enterpises' do