From aacc36ea440d7b84807d5a3b6e6b27a0d3c116d5 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 29 Nov 2012 10:02:23 +1100 Subject: [PATCH] Create order cycle basic fields --- app/assets/javascripts/admin/order_cycle.js | 20 +++++++++++ .../admin/order_cycles_controller.rb | 15 +++++++++ app/models/order_cycle.rb | 2 +- app/views/admin/order_cycles/index.html.haml | 6 ++++ app/views/admin/order_cycles/new.html.haml | 28 ++++++++++++++++ spec/requests/admin/order_cycles_spec.rb | 33 +++++++++++++++++-- 6 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/admin/order_cycle.js create mode 100644 app/views/admin/order_cycles/new.html.haml diff --git a/app/assets/javascripts/admin/order_cycle.js b/app/assets/javascripts/admin/order_cycle.js new file mode 100644 index 0000000000..4b19b19316 --- /dev/null +++ b/app/assets/javascripts/admin/order_cycle.js @@ -0,0 +1,20 @@ +function AdminOrderCycleCtrl($scope, $http) { + $http.get('/admin/order_cycles/new.json').success(function(data) { + $scope.order_cycle = data; + }); + + $scope.submit = function() { + $http.post('/admin/order_cycles', {order_cycle: $scope.order_cycle}).success(function(data) { + if(data['success']) { + window.location = '/admin/order_cycles'; + } else { + console.log('fail'); + } + }); + }; +} + +angular.module('order_cycle', []). + config(function($httpProvider) { + $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content'); + }); diff --git a/app/controllers/admin/order_cycles_controller.rb b/app/controllers/admin/order_cycles_controller.rb index 6be2bd308c..f82b1b2b72 100644 --- a/app/controllers/admin/order_cycles_controller.rb +++ b/app/controllers/admin/order_cycles_controller.rb @@ -9,6 +9,21 @@ module Admin end end + def create + @order_cycle = OrderCycle.new(params[:order_cycle]) + respond_to do |format| + if @order_cycle.save + flash[:notice] = 'Your order cycle has been created.' + format.html { redirect_to admin_order_cycles_path } + format.json { render :json => {:success => true} } + else + format.html + format.json { render :json => {:success => false} } + end + end + end + + private def load_order_cycle_set diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 0983695db6..ea580f4f46 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -5,7 +5,7 @@ class OrderCycle < ActiveRecord::Base has_many :exchanges, :dependent => :destroy - validates_presence_of :name + validates_presence_of :name, :coordinator_id def suppliers diff --git a/app/views/admin/order_cycles/index.html.haml b/app/views/admin/order_cycles/index.html.haml index b5bdf735c3..cb20497993 100644 --- a/app/views/admin/order_cycles/index.html.haml +++ b/app/views/admin/order_cycles/index.html.haml @@ -1,3 +1,9 @@ +.toolbar{'data-hook' => "toolbar"} + %ul.actions + %li + = button_link_to "New Order Cycle", main_app.new_admin_order_cycle_path, :icon => 'add', :id => 'admin_new_order_cycle_link' + %br.clear/ + %h1 Order Cycles = form_for @order_cycle_set, :url => main_app.bulk_update_admin_order_cycles_path do |f| diff --git a/app/views/admin/order_cycles/new.html.haml b/app/views/admin/order_cycles/new.html.haml new file mode 100644 index 0000000000..e6b7cf8dcb --- /dev/null +++ b/app/views/admin/order_cycles/new.html.haml @@ -0,0 +1,28 @@ +%h1 New Order Cycle + += form_for [main_app, :admin, @order_cycle], :url => '', :html => {'ng-app' => 'order_cycle', 'ng-controller' => 'AdminOrderCycleCtrl', 'ng-submit' => 'submit()'} do |f| + = f.label :name + = f.text_field :name, 'ng-model' => 'order_cycle.name' + %br/ + + = f.label :orders_open_at, 'Orders open' + = f.text_field :orders_open_at, 'ng-model' => 'order_cycle.orders_open_at' + = f.label :orders_close_at, 'Orders close' + = f.text_field :orders_close_at, 'ng-model' => 'order_cycle.orders_close_at' + %br/ + + %h2 Incoming + %p TODO + + %h2 Coordinator + = f.label :coordinator_id, 'Coordinator' + = f.collection_select :coordinator_id, Enterprise.all, :id, :name, {}, {'ng-model' => 'order_cycle.coordinator_id'} + + %h2 Outgoing + %p TODO + + = f.submit 'Create' + or + = link_to 'Cancel', main_app.admin_order_cycles_path + + %pre {{ order_cycle | json }} diff --git a/spec/requests/admin/order_cycles_spec.rb b/spec/requests/admin/order_cycles_spec.rb index 73273ed8d6..ea2e2401b5 100644 --- a/spec/requests/admin/order_cycles_spec.rb +++ b/spec/requests/admin/order_cycles_spec.rb @@ -8,24 +8,51 @@ feature %q{ include WebHelper scenario "listing order cycles" do + # Given an order cycle oc = create(:order_cycle) + # When I go to the admin order cycles page login_to_admin_section click_link 'Order Cycles' - # Regular fields + # Then I should see the basic fields page.should have_selector 'a', text: oc.name page.should have_selector "input[value='#{oc.orders_open_at}']" page.should have_selector "input[value='#{oc.orders_close_at}']" page.should have_content oc.coordinator.name - # Suppliers and distributors + # And I should see the suppliers and distributors oc.suppliers.each { |s| page.should have_content s.name } oc.distributors.each { |d| page.should have_content d.name } - # Products + # And I should see a thumbnail image for each product all('td.products img').count.should == 2 end + scenario "creating an order cycle" do + # Given a coordinating enterprise + create(:enterprise, name: 'My coordinator') + + # When I go to the new order cycle page + login_to_admin_section + click_link 'Order Cycles' + click_link 'New Order Cycle' + + # And I fill in the basic fields and click Create + 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' + click_button 'Create' + + # Then my order cycle should have been created + page.should have_content 'Your order cycle has been created.' + + page.should have_selector 'a', text: 'Plums & Avos' + + page.should have_selector "input[value='2012-11-06 06:00:00 UTC']" + page.should have_selector "input[value='2012-11-13 17:00:00 UTC']" + page.should have_content 'My coordinator' + end end