From 8de6f983a218db40e1a05546cc5086521d5a718f Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 20 Feb 2015 23:57:36 +1100 Subject: [PATCH] User must set the coordinator first when creating an order cycle --- .../admin/order_cycle.js.erb.coffee | 3 +- .../admin/order_cycles_controller.rb | 19 ++++++ app/helpers/admin/injection_helper.rb | 4 ++ app/helpers/order_cycles_helper.rb | 40 ++++++------- app/views/admin/order_cycles/_form.html.haml | 6 +- app/views/admin/order_cycles/new.html.haml | 1 + .../order_cycles/set_coordinator.html.haml | 15 +++++ .../admin/order_cycles_controller_spec.rb | 60 +++++++++++++++++++ spec/features/admin/order_cycles_spec.rb | 15 ++++- spec/helpers/order_cycles_helper_spec.rb | 18 +++--- 10 files changed, 143 insertions(+), 38 deletions(-) create mode 100644 app/views/admin/order_cycles/set_coordinator.html.haml create mode 100644 spec/controllers/admin/order_cycles_controller_spec.rb diff --git a/app/assets/javascripts/admin/order_cycle.js.erb.coffee b/app/assets/javascripts/admin/order_cycle.js.erb.coffee index 608de85d8a..83f6e4b080 100644 --- a/app/assets/javascripts/admin/order_cycle.js.erb.coffee +++ b/app/assets/javascripts/admin/order_cycle.js.erb.coffee @@ -1,10 +1,11 @@ angular.module('admin.order_cycles', ['ngResource']) - .controller('AdminCreateOrderCycleCtrl', ['$scope', 'OrderCycle', 'Enterprise', 'EnterpriseFee', ($scope, OrderCycle, Enterprise, EnterpriseFee) -> + .controller('AdminCreateOrderCycleCtrl', ['$scope', 'OrderCycle', 'Enterprise', 'EnterpriseFee', 'ocInstance', ($scope, OrderCycle, Enterprise, EnterpriseFee, ocInstance) -> $scope.enterprises = Enterprise.index() $scope.supplied_products = Enterprise.supplied_products $scope.enterprise_fees = EnterpriseFee.index() $scope.order_cycle = OrderCycle.order_cycle + $scope.order_cycle.coordinator_id = ocInstance.coordinator_id $scope.loaded = -> Enterprise.loaded && EnterpriseFee.loaded diff --git a/app/controllers/admin/order_cycles_controller.rb b/app/controllers/admin/order_cycles_controller.rb index 63daf918de..f978066bf4 100644 --- a/app/controllers/admin/order_cycles_controller.rb +++ b/app/controllers/admin/order_cycles_controller.rb @@ -6,6 +6,7 @@ module Admin include OrderCyclesHelper before_filter :load_order_cycle_set, :only => :index + before_filter :set_coordinator, only: :new def show respond_to do |format| @@ -85,5 +86,23 @@ module Admin def load_order_cycle_set @order_cycle_set = OrderCycleSet.new :collection => collection end + + def set_coordinator + if params[:coordinator_id] && @coordinator = order_cycle_coordinating_enterprises.find_by_id(params[:coordinator_id]) + return + end + + available_coordinators = order_cycle_coordinating_enterprises.select(&:confirmed?) + case available_coordinators.count + when 0 + flash[:error] = "None of your enterprises have permission to coordinate an order cycle" + redirect_to main_app.admin_order_cycles_path + when 1 + @coordinator = available_coordinators.first + else + flash[:error] = "You don't have permission to create an order cycle coordinated by that enterprise" if params[:coordinator_id] + render :set_coordinator + end + end end end diff --git a/app/helpers/admin/injection_helper.rb b/app/helpers/admin/injection_helper.rb index 50b9aa1125..755ba1f5a3 100644 --- a/app/helpers/admin/injection_helper.rb +++ b/app/helpers/admin/injection_helper.rb @@ -62,6 +62,10 @@ module Admin admin_inject_json_ams_array "ofn.admin", "variantOverrides", @variant_overrides, Api::Admin::VariantOverrideSerializer end + def admin_inject_order_cycle_instance + render partial: "admin/json/injection_ams", locals: {ngModule: 'admin.order_cycles', name: 'ocInstance', json: "{coordinator_id: '#{@coordinator.id}'}"} + end + def admin_inject_spree_api_key render partial: "admin/json/injection_ams", locals: {ngModule: 'ofn.admin', name: 'SpreeApiKey', json: "'#{@spree_api_key.to_s}'"} end diff --git a/app/helpers/order_cycles_helper.rb b/app/helpers/order_cycles_helper.rb index 8eeda8b293..66e6e16c20 100644 --- a/app/helpers/order_cycles_helper.rb +++ b/app/helpers/order_cycles_helper.rb @@ -7,34 +7,28 @@ module OrderCyclesHelper OpenFoodNetwork::Permissions.new(spree_current_user).order_cycle_enterprises end - def order_cycle_producer_enterprises(options={}) - enterprises = order_cycle_permitted_enterprises.is_primary_producer.by_name - - if options[:without_validation] - enterprises - else - validated_enterprise_options enterprises, confirmed: true - end + def order_cycle_producer_enterprises + order_cycle_permitted_enterprises.is_primary_producer.by_name end - def order_cycle_coordinating_enterprises(options={}) - enterprises = order_cycle_permitted_enterprises.is_distributor.by_name - - if options[:without_validation] - enterprises - else - validated_enterprise_options enterprises, confirmed: true - end + def order_cycle_producer_enterprise_options + validated_enterprise_options order_cycle_producer_enterprises, confirmed: true end - def order_cycle_hub_enterprises(options={}) - enterprises = order_cycle_permitted_enterprises.is_distributor.by_name + def order_cycle_coordinating_enterprises + order_cycle_permitted_enterprises.is_distributor.by_name + end - if options[:without_validation] - enterprises - else - validated_enterprise_options enterprises, confirmed: true, shipping_and_payment_methods: true - end + def order_cycle_coordinating_enterprise_options + validated_enterprise_options order_cycle_coordinating_enterprises, confirmed: true + end + + def order_cycle_hub_enterprises + order_cycle_permitted_enterprises.is_distributor.by_name + end + + def order_cycle_hub_enterprise_options + validated_enterprise_options order_cycle_hub_enterprises, confirmed: true, shipping_and_payment_methods: true end def order_cycle_status_class(order_cycle) diff --git a/app/views/admin/order_cycles/_form.html.haml b/app/views/admin/order_cycles/_form.html.haml index 4fa9d70578..b624bbd4b2 100644 --- a/app/views/admin/order_cycles/_form.html.haml +++ b/app/views/admin/order_cycles/_form.html.haml @@ -15,14 +15,14 @@ %tr.products{'ng-show' => 'exchange.showProducts'} = render 'exchange_supplied_products_form' -= select_tag :new_supplier_id, options_for_select(order_cycle_producer_enterprises), {'ng-model' => 'new_supplier_id'} += select_tag :new_supplier_id, options_for_select(order_cycle_producer_enterprise_options), {'ng-model' => 'new_supplier_id'} = f.submit 'Add supplier', 'ng-click' => 'addSupplier($event)' %h2 Coordinator = f.label :coordinator_id, 'Coordinator' -= f.select :coordinator_id, order_cycle_coordinating_enterprises, { include_blank: true }, {'ng-model' => 'order_cycle.coordinator_id', 'ofn-on-change' => 'order_cycle.coordinator_fees = []', 'required' => true} += f.select :coordinator_id, order_cycle_coordinating_enterprise_options, { include_blank: true }, {'ng-model' => 'order_cycle.coordinator_id', 'ofn-on-change' => 'order_cycle.coordinator_fees = []', 'required' => true} = render 'coordinator_fees', f: f @@ -41,7 +41,7 @@ %tr.products{'ng-show' => 'exchange.showProducts'} = render 'exchange_distributed_products_form' -= select_tag :new_distributor_id, options_for_select(order_cycle_hub_enterprises), {'ng-model' => 'new_distributor_id'} += select_tag :new_distributor_id, options_for_select(order_cycle_hub_enterprise_options), {'ng-model' => 'new_distributor_id'} = f.submit 'Add distributor', 'ng-click' => 'addDistributor($event)' .actions diff --git a/app/views/admin/order_cycles/new.html.haml b/app/views/admin/order_cycles/new.html.haml index 817f790b19..95974f9ec5 100644 --- a/app/views/admin/order_cycles/new.html.haml +++ b/app/views/admin/order_cycles/new.html.haml @@ -1,6 +1,7 @@ %h1 New Order Cycle - ng_controller = order_cycles_simple_view ? 'AdminSimpleCreateOrderCycleCtrl' : 'AdminCreateOrderCycleCtrl' += admin_inject_order_cycle_instance = form_for [main_app, :admin, @order_cycle], :url => '', :html => {:class => 'ng order_cycle', 'ng-app' => 'admin.order_cycles', 'ng-controller' => ng_controller, 'ng-submit' => 'submit($event)'} do |f| - if order_cycles_simple_view diff --git a/app/views/admin/order_cycles/set_coordinator.html.haml b/app/views/admin/order_cycles/set_coordinator.html.haml new file mode 100644 index 0000000000..ebf0cffbb8 --- /dev/null +++ b/app/views/admin/order_cycles/set_coordinator.html.haml @@ -0,0 +1,15 @@ +%h4.text-center Select a coordinator for your order cycle + +%br + += form_for @order_cycle, :url => main_app.new_admin_order_cycle_path, method: :get do |f| + .row + .two.columns.alpha +   + .ten.columns + = select_tag :coordinator_id, options_for_select(order_cycle_coordinating_enterprise_options), { 'required' => true, class: 'select2 fullwidth'} + .two.columns.alpha + = f.submit "Continue >" + + .two.columns.omega +   diff --git a/spec/controllers/admin/order_cycles_controller_spec.rb b/spec/controllers/admin/order_cycles_controller_spec.rb new file mode 100644 index 0000000000..598059b5f4 --- /dev/null +++ b/spec/controllers/admin/order_cycles_controller_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper' + +module Admin + describe OrderCyclesController do + include AuthenticationWorkflow + let!(:distributor_owner) { create_enterprise_user enterprise_limit: 2 } + + before do + controller.stub spree_current_user: distributor_owner + end + + describe "new" do + describe "when the user manages no distributor enterprises suitable for coordinator" do + let!(:distributor) { create(:distributor_enterprise, owner: distributor_owner, confirmed_at: nil) } + + it "redirects to order cycles index" do + spree_get :new + expect(response).to redirect_to admin_order_cycles_path + end + end + + describe "when the user manages a single distributor enterprise suitable for coordinator" do + let!(:distributor) { create(:distributor_enterprise, owner: distributor_owner) } + + it "renders the new template" do + spree_get :new + expect(response).to render_template :new + end + end + + describe "when a user manages multiple enterprises suitable for coordinator" do + let!(:distributor1) { create(:distributor_enterprise, owner: distributor_owner) } + let!(:distributor2) { create(:distributor_enterprise, owner: distributor_owner) } + let!(:distributor3) { create(:distributor_enterprise) } + + it "renders the set_coordinator template" do + spree_get :new + expect(response).to render_template :set_coordinator + end + + describe "and a coordinator_id is submitted as part of the request" do + describe "when the user manages the enterprise" do + it "renders the new template" do + spree_get :new, coordinator_id: distributor1.id + expect(response).to render_template :new + end + end + + describe "when the user does not manage the enterprise" do + it "renders the set_coordinator template and sets a flash error" do + spree_get :new, coordinator_id: distributor3.id + expect(response).to render_template :set_coordinator + expect(flash[:error]).to eq "You don't have permission to create an order cycle coordinated by that enterprise" + end + end + end + end + end + end +end diff --git a/spec/features/admin/order_cycles_spec.rb b/spec/features/admin/order_cycles_spec.rb index 180485a734..c84a6d65c7 100644 --- a/spec/features/admin/order_cycles_spec.rb +++ b/spec/features/admin/order_cycles_spec.rb @@ -77,11 +77,14 @@ feature %q{ click_link 'Order Cycles' click_link 'New Order Cycle' + # Select a coordinator since there are two available + select2_select 'My coordinator', from: 'coordinator_id' + click_button "Continue >" + # And I fill in the basic fields fill_in 'order_cycle_name', with: 'Plums & Avos' fill_in 'order_cycle_orders_open_at', with: '2012-11-06 06:00:00' fill_in 'order_cycle_orders_close_at', with: '2012-11-13 17:00:00' - select 'My coordinator', from: 'order_cycle_coordinator_id' # And I add a coordinator fee click_button 'Add coordinator fee' @@ -502,7 +505,7 @@ feature %q{ # I should see only the order cycle I am coordinating page.should have_content oc_user_coordinating.name page.should_not have_content oc_for_other_user.name - + # The order cycle should show enterprises that I manage page.should have_selector 'td.suppliers', text: supplier_managed.name page.should have_selector 'td.distributors', text: distributor_managed.name @@ -516,6 +519,14 @@ feature %q{ click_link "Order Cycles" click_link 'New Order Cycle' + save_screenshot '/Users/rob/Desktop/ss.png' + + # Select a coordinator since there are two available + select2_select 'Managed distributor', from: 'coordinator_id' + click_button "Continue >" + + save_screenshot '/Users/rob/Desktop/ss1.png' + fill_in 'order_cycle_name', with: 'My order cycle' fill_in 'order_cycle_orders_open_at', with: '2012-11-06 06:00:00' fill_in 'order_cycle_orders_close_at', with: '2012-11-13 17:00:00' diff --git a/spec/helpers/order_cycles_helper_spec.rb b/spec/helpers/order_cycles_helper_spec.rb index 3706c72579..0e1ea42121 100644 --- a/spec/helpers/order_cycles_helper_spec.rb +++ b/spec/helpers/order_cycles_helper_spec.rb @@ -1,36 +1,36 @@ require 'spec_helper' describe OrderCyclesHelper do - describe "finding producer enterprises" do + describe "finding producer enterprise options" do before do - helper.stub_chain(:order_cycle_permitted_enterprises, :is_primary_producer, :by_name) { "enterprise list" } + helper.stub(:order_cycle_producer_enterprises) { "enterprise list" } end it "asks for a validation option list" do expect(helper).to receive(:validated_enterprise_options).with("enterprise list", {confirmed: true}) - helper.order_cycle_producer_enterprises + helper.order_cycle_producer_enterprise_options end end - describe "finding coodinator enterprises" do + describe "finding coodinator enterprise options" do before do - helper.stub_chain(:order_cycle_permitted_enterprises, :is_distributor, :by_name) { "enterprise list" } + helper.stub(:order_cycle_coordinating_enterprises) { "enterprise list" } end it "asks for a validation option list" do expect(helper).to receive(:validated_enterprise_options).with("enterprise list", {confirmed: true}) - helper.order_cycle_coordinating_enterprises + helper.order_cycle_coordinating_enterprise_options end end - describe "finding hub enterprises" do + describe "finding hub enterprise options" do before do - helper.stub_chain(:order_cycle_permitted_enterprises, :is_distributor, :by_name) { "enterprise list" } + helper.stub(:order_cycle_hub_enterprises) { "enterprise list" } end it "asks for a validation option list" do expect(helper).to receive(:validated_enterprise_options).with("enterprise list", {confirmed: true, shipping_and_payment_methods: true}) - helper.order_cycle_hub_enterprises + helper.order_cycle_hub_enterprise_options end end