From 31735d3d102499f63636c31dd464199ec8bb2800 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 30 Nov 2012 15:44:07 +1100 Subject: [PATCH] Add edit order cycle controller, load basic data --- app/assets/javascripts/admin/order_cycle.js | 33 +++++++++++++ app/views/admin/order_cycles/_form.html.haml | 49 ++++++++++---------- app/views/admin/order_cycles/edit.html.haml | 4 ++ app/views/admin/order_cycles/new.html.haml | 3 +- 4 files changed, 63 insertions(+), 26 deletions(-) create mode 100644 app/views/admin/order_cycles/edit.html.haml diff --git a/app/assets/javascripts/admin/order_cycle.js b/app/assets/javascripts/admin/order_cycle.js index c3a91ce976..aca777f845 100644 --- a/app/assets/javascripts/admin/order_cycle.js +++ b/app/assets/javascripts/admin/order_cycle.js @@ -26,10 +26,43 @@ function AdminCreateOrderCycleCtrl($scope, $http, Enterprise) { }; } + +function AdminEditOrderCycleCtrl($scope, $http, OrderCycle, Enterprise) { + // TODO: Research how to do route param parsing from regular url. + // Does Angular have a way to do this? + + OrderCycle.get({order_cycle_id: 24}, function(order_cycle) { + $scope.order_cycle = order_cycle; + $scope.order_cycle.incoming_exchanges = []; + $scope.order_cycle.outgoing_exchanges = []; + for(i in order_cycle.exchanges) { + var exchange = order_cycle.exchanges[i]; + if(exchange.sender_id == order_cycle.coordinator_id) { + $scope.order_cycle.outgoing_exchanges.push({enterprise_id: exchange.receiver_id}); + + } else if(exchange.receiver_id == order_cycle.coordinator_id) { + $scope.order_cycle.incoming_exchanges.push({enterprise_id: exchange.sender_id}); + + } else { + console.log('Exchange between two enterprises, neither of which is coordinator!'); + } + } + + // TODO: Check if this is the best way + delete($scope.order_cycle.exchanges); + }); +} + + angular.module('order_cycle', ['ngResource']). config(function($httpProvider) { $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content'); }). + factory('OrderCycle', function($resource) { + return $resource('/admin/order_cycles/:order_cycle_id.json', {}, + {'index': { method: 'GET', isArray: true}, + 'show': { method: 'GET', isArray: false}}); + }). factory('Enterprise', function($resource) { return $resource('/admin/enterprises/:enterprise_id.json', {}, {'index': { method: 'GET', isArray: true}}); diff --git a/app/views/admin/order_cycles/_form.html.haml b/app/views/admin/order_cycles/_form.html.haml index 1865e5d079..ce46b8fc6c 100644 --- a/app/views/admin/order_cycles/_form.html.haml +++ b/app/views/admin/order_cycles/_form.html.haml @@ -1,34 +1,33 @@ -= form_for [main_app, :admin, @order_cycle], :url => '', :html => {:class => 'ng', 'ng-app' => 'order_cycle', 'ng-controller' => 'AdminCreateOrderCycleCtrl', 'ng-submit' => 'submit()'} do |f| - = f.label :name - = f.text_field :name, 'ng-model' => 'order_cycle.name', 'required' => true - %br/ += f.label :name += f.text_field :name, 'ng-model' => 'order_cycle.name', 'required' => true +%br/ - = f.label :orders_open_at, 'Orders open' - = f.text_field :orders_open_at, 'class' => 'datetimepicker', 'ng-model' => 'order_cycle.orders_open_at' - = f.label :orders_close_at, 'Orders close' - = f.text_field :orders_close_at, 'class' => 'datetimepicker', 'ng-model' => 'order_cycle.orders_close_at' - %br/ += f.label :orders_open_at, 'Orders open' += f.text_field :orders_open_at, 'class' => 'datetimepicker', 'ng-model' => 'order_cycle.orders_open_at' += f.label :orders_close_at, 'Orders close' += f.text_field :orders_close_at, 'class' => 'datetimepicker', 'ng-model' => 'order_cycle.orders_close_at' +%br/ - %h2 Incoming - %table - %tr{'ng-repeat' => 'exchange in order_cycle.incoming_exchanges'} - %td {{ enterprises[exchange.enterprise_id].name }} +%h2 Incoming +%table + %tr{'ng-repeat' => 'exchange in order_cycle.incoming_exchanges'} + %td {{ enterprises[exchange.enterprise_id].name }} - = select_tag :new_supplier_id, options_from_collection_for_select(Enterprise.is_primary_producer, :id, :name), {'ng-model' => 'new_supplier_id'} - = f.submit 'Add supplier', 'ng-click' => 'addSupplier($event)' += select_tag :new_supplier_id, options_from_collection_for_select(Enterprise.is_primary_producer, :id, :name), {'ng-model' => 'new_supplier_id'} += f.submit 'Add supplier', 'ng-click' => 'addSupplier($event)' - %h2 Coordinator - = f.label :coordinator_id, 'Coordinator' - = f.collection_select :coordinator_id, Enterprise.all, :id, :name, {}, {'ng-model' => 'order_cycle.coordinator_id', 'required' => true} +%h2 Coordinator += f.label :coordinator_id, 'Coordinator' += f.collection_select :coordinator_id, Enterprise.all, :id, :name, {}, {'ng-model' => 'order_cycle.coordinator_id', 'required' => true} - %h2 Outgoing - %p TODO +%h2 Outgoing +%p TODO - = f.submit 'Create' - or - = link_to 'Cancel', main_app.admin_order_cycles_path += f.submit 'Create' +or += link_to 'Cancel', main_app.admin_order_cycles_path - %pre {{ order_cycle | json }} - %pre {{ enterprises | json }} +%pre {{ order_cycle | json }} +%pre {{ enterprises | json }} diff --git a/app/views/admin/order_cycles/edit.html.haml b/app/views/admin/order_cycles/edit.html.haml new file mode 100644 index 0000000000..9168d68f92 --- /dev/null +++ b/app/views/admin/order_cycles/edit.html.haml @@ -0,0 +1,4 @@ +%h1 Edit Order Cycle + += form_for [main_app, :admin, @order_cycle], :url => '', :html => {:class => 'ng', 'ng-app' => 'order_cycle', 'ng-controller' => 'AdminEditOrderCycleCtrl', 'ng-submit' => 'submit()'} do |f| + = render 'form', :f => f diff --git a/app/views/admin/order_cycles/new.html.haml b/app/views/admin/order_cycles/new.html.haml index 0e2570c06c..82c19db25d 100644 --- a/app/views/admin/order_cycles/new.html.haml +++ b/app/views/admin/order_cycles/new.html.haml @@ -1,3 +1,4 @@ %h1 New Order Cycle -= render 'form' += form_for [main_app, :admin, @order_cycle], :url => '', :html => {:class => 'ng', 'ng-app' => 'order_cycle', 'ng-controller' => 'AdminCreateOrderCycleCtrl', 'ng-submit' => 'submit()'} do |f| + = render 'form', :f => f