User must set the coordinator first when creating an order cycle

This commit is contained in:
Rob Harrington
2015-02-20 23:57:36 +11:00
parent 53594b3a0f
commit 8de6f983a2
10 changed files with 143 additions and 38 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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
 

View File

@@ -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

View File

@@ -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'

View File

@@ -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