diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index 4c9c32dbae..8561704686 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -2,14 +2,14 @@ require 'open_food_network/spree_api_key_loader' Spree::Admin::ProductsController.class_eval do include OpenFoodNetwork::SpreeApiKeyLoader - before_filter :load_form_data, :only => [:bulk_edit, :new, :edit] + before_filter :load_form_data, :only => [:bulk_edit, :new, :create, :edit, :update] before_filter :load_spree_api_key, :only => :bulk_edit alias_method :location_after_save_original, :location_after_save respond_to :json, :only => :clone - respond_override create: { html: { + respond_override create: { html: { success: lambda { if params[:button] == "add_another" redirect_to new_admin_product_path diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index a81655a1de..a88e1c766e 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -50,9 +50,9 @@ class Enterprise < ActiveRecord::Base validates :type, presence: true, inclusion: {in: TYPES} 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 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/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 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