From 4b2f1cefa0773800a648f9c5cb672fce5d9815f0 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 26 Sep 2014 17:11:11 +1000 Subject: [PATCH] Spliting order management abilities out of product management abilities --- app/models/spree/ability_decorator.rb | 9 ++- spec/features/admin/enterprise_user_spec.rb | 12 ++-- spec/models/spree/ability_spec.rb | 68 +++++++++++---------- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 3ce7066867..5fdb4c2342 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -5,6 +5,7 @@ class AbilityDecorator add_base_abilities user if is_new_user? user add_enterprise_management_abilities user if can_manage_enterprises? user add_product_management_abilities user if can_manage_products? user + add_order_management_abilities user if can_manage_orders? user add_relationship_management_abilities user if can_manage_relationships? user end @@ -17,12 +18,13 @@ class AbilityDecorator user.enterprises.present? end - def can_manage_products?(user) - # ( user.enterprises.map(&:type) & %w(single full) ).any? can_manage_enterprises? user end + def can_manage_orders?(user) + ( user.enterprises.map(&:type) & %w(single full) ).any? + end def can_manage_relationships?(user) can_manage_enterprises? user @@ -47,7 +49,6 @@ class AbilityDecorator end end - def add_product_management_abilities(user) # Enterprise User can only access products that they are a supplier for can [:create], Spree::Product @@ -65,7 +66,9 @@ class AbilityDecorator can [:admin, :index, :read, :search], Spree::Taxon can [:admin, :index, :read, :create, :edit], Spree::Classification + end + def add_order_management_abilities(user) # Enterprise User can only access orders that they are a distributor for can [:index, :create], Spree::Order can [:read, :update, :fire, :resend], Spree::Order do |order| diff --git a/spec/features/admin/enterprise_user_spec.rb b/spec/features/admin/enterprise_user_spec.rb index 77378f6cbd..70e6a9b265 100644 --- a/spec/features/admin/enterprise_user_spec.rb +++ b/spec/features/admin/enterprise_user_spec.rb @@ -64,7 +64,7 @@ feature %q{ page.should have_admin_menu_item 'Dashboard' page.should have_admin_menu_item 'Enterprises' - ['Orders', 'Products', 'Reports', 'Configuration', 'Promotions', 'Users', 'Order Cycles'].each do |menu_item_name| + ['Orders', 'Reports', 'Configuration', 'Promotions', 'Users', 'Order Cycles'].each do |menu_item_name| page.should_not have_admin_menu_item menu_item_name end end @@ -79,15 +79,15 @@ feature %q{ end end - it "does not show me product management controls" do - page.should_not have_selector '#products' + it "shows me product management controls, but not order_cycle controls" do + page.should have_selector '#products' page.should_not have_selector '#order_cycles' end - it "does not show me enterprise product info, payment methods, shipping methods or enterprise fees" do + it "shows me enterprise product info but not payment methods, shipping methods or enterprise fees" do # Producer product info - page.should_not have_selector '.producers_tab span', text: 'Total Products' - page.should_not have_selector '.producers_tab span', text: 'Active Products' + page.should have_selector '.producers_tab span', text: 'Total Products' + page.should have_selector '.producers_tab span', text: 'Active Products' page.should_not have_selector '.producers_tab span', text: 'Products in OCs' # Payment methods, shipping methods, enterprise fees diff --git a/spec/models/spree/ability_spec.rb b/spec/models/spree/ability_spec.rb index 7402c1b527..2d2d1cfb01 100644 --- a/spec/models/spree/ability_spec.rb +++ b/spec/models/spree/ability_spec.rb @@ -13,44 +13,46 @@ module Spree let(:enterprise_single) { create(:enterprise, type: 'single') } let(:enterprise_profile) { create(:enterprise, type: 'profile') } - describe "creating enterprises" do + context "as manager of a 'full' type enterprise" do + before do + user.enterprise_roles.create! enterprise: enterprise_full + end + + it { subject.can_manage_products?(user).should be_true } + it { subject.can_manage_enterprises?(user).should be_true } + it { subject.can_manage_orders?(user).should be_true } + end + + context "as manager of a 'single' type enterprise" do + before do + user.enterprise_roles.create! enterprise: enterprise_single + end + + it { subject.can_manage_products?(user).should be_true } + it { subject.can_manage_enterprises?(user).should be_true } + it { subject.can_manage_orders?(user).should be_true } + end + + context "as manager of a 'profile' type enterprise" do + before do + user.enterprise_roles.create! enterprise: enterprise_profile + end + + it { subject.can_manage_products?(user).should be_true } + it { subject.can_manage_enterprises?(user).should be_true } + it { subject.can_manage_orders?(user).should be_false } + end + + context "as a new user with no enterprises" do + it { subject.can_manage_products?(user).should be_false } + it { subject.can_manage_enterprises?(user).should be_false } + it { subject.can_manage_orders?(user).should be_false } + it "can create enterprises straight off the bat" do subject.is_new_user?(user).should be_true expect(user).to have_ability :create, for: Enterprise end end - - describe "managing enterprises" do - it "can manage enterprises when the user has at least one enterprise assigned" do - user.enterprise_roles.create! enterprise: enterprise_full - subject.can_manage_enterprises?(user).should be_true - end - - it "can't otherwise" do - subject.can_manage_enterprises?(user).should be_false - end - end - - describe "managing products" do - it "can when a user manages a 'full' type enterprise" do - user.enterprise_roles.create! enterprise: enterprise_full - subject.can_manage_products?(user).should be_true - end - - it "can when a user manages a 'single' type enterprise" do - user.enterprise_roles.create! enterprise: enterprise_single - subject.can_manage_products?(user).should be_true - end - - it "can't when a user manages a 'profile' type enterprise" do - user.enterprise_roles.create! enterprise: enterprise_profile - subject.can_manage_products?(user).should be_true - end - - it "can't when the user manages no enterprises" do - subject.can_manage_products?(user).should be_false - end - end end describe 'Roles' do