diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index a078652d68..fc14c80440 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -50,9 +50,9 @@ class Enterprise < ActiveRecord::Base validates :sells, presence: true, inclusion: {in: SELLS} validates :address, presence: true, associated: true validates_presence_of :owner - validate :enforce_ownership_limit, if: lambda { owner_id_changed? } + validate :enforce_ownership_limit, if: lambda { owner_id_changed? && !owner_id.nil? } - before_validation :ensure_owner_is_manager, if: lambda { owner_id_changed? } + before_validation :ensure_owner_is_manager, if: lambda { owner_id_changed? && !owner_id.nil? } before_validation :set_unused_address_fields after_validation :geocode_address diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 1391e0485c..8dc0f95381 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/app/views/spree/order_mailer/confirm_email.text.haml b/app/views/spree/order_mailer/confirm_email.text.haml index 1ebc1a89af..9822653969 100644 --- a/app/views/spree/order_mailer/confirm_email.text.haml +++ b/app/views/spree/order_mailer/confirm_email.text.haml @@ -13,7 +13,7 @@ Subtotal: #{number_to_currency checkout_cart_total_with_adjustments(@order)} - checkout_adjustments_for_summary(@order, exclude: [:distribution]).each do |adjustment| #{raw(adjustment.label)} #{adjustment.display_amount} Order Total: #{@order.display_total} -- if @order.payments.first.andand.payment_method.andand.type == "Spree::PaymentMethod::Check" +- if @order.payments.first.andand.payment_method.andand.type == "Spree::PaymentMethod::Check" and @order.payments.first.andand.payment_method.andand.description \ ============================================================ Payment Details @@ -28,11 +28,14 @@ Order Total: #{@order.display_total} Your order will be delivered to: #{@order.ship_address.to_s} -- if @order.order_cycle.andand.pickup_time_for(@order.distributor) - Delivery on: #{@order.order_cycle.pickup_time_for(@order.distributor)} + - if @order.shipping_method.andand.description + #{@order.shipping_method.description.html_safe} -- if @order.order_cycle.andand.pickup_instructions_for(@order.distributor) - Other delivery information: #{@order.order_cycle.pickup_instructions_for(@order.distributor)} + - if @order.order_cycle.andand.pickup_time_for(@order.distributor) + Delivery on: #{@order.order_cycle.pickup_time_for(@order.distributor)} + + - if @order.order_cycle.andand.pickup_instructions_for(@order.distributor) + Other delivery information: #{@order.order_cycle.pickup_instructions_for(@order.distributor)} - else \ diff --git a/spec/features/admin/enterprise_user_spec.rb b/spec/features/admin/enterprise_user_spec.rb index 22f322e51e..232ffb68ae 100644 --- a/spec/features/admin/enterprise_user_spec.rb +++ b/spec/features/admin/enterprise_user_spec.rb @@ -67,7 +67,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 @@ -82,15 +82,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 759feb499d..7677f55eed 100644 --- a/spec/models/spree/ability_spec.rb +++ b/spec/models/spree/ability_spec.rb @@ -14,44 +14,46 @@ module Spree let(:enterprise_single) { create(:enterprise, sells: 'single') } let(:enterprise_profile) { create(:enterprise, sells: '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