From 2daceb1111249e045cb7d72a6742a3655ba07f90 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 4 Dec 2015 08:45:32 +1100 Subject: [PATCH] Angularise new order form --- .../controllers/orders_controller.js.coffee | 4 +++ .../add_distribution_fields.html.haml.deface | 29 +++++++++++++++++++ .../spree/admin/line_items/create.js.erb | 5 ++++ .../orders/_distribution_fields.html.haml | 14 --------- app/views/spree/admin/orders/new.html.haml | 13 ++++----- spec/features/admin/orders_spec.rb | 11 +++---- 6 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee create mode 100644 app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface create mode 100644 app/views/spree/admin/line_items/create.js.erb delete mode 100644 app/views/spree/admin/orders/_distribution_fields.html.haml diff --git a/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee b/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee new file mode 100644 index 0000000000..826bc6ed11 --- /dev/null +++ b/app/assets/javascripts/admin/orders/controllers/orders_controller.js.coffee @@ -0,0 +1,4 @@ +angular.module("admin.orders").controller "ordersCtrl", ($scope, $compile, shops, orderCycles) -> + $scope.$compile = $compile + $scope.shops = shops + $scope.orderCycles = orderCycles diff --git a/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface b/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface new file mode 100644 index 0000000000..a8e716769d --- /dev/null +++ b/app/overrides/spree/admin/orders/_form/add_distribution_fields.html.haml.deface @@ -0,0 +1,29 @@ +/ insert_before "[data-hook='admin_order_form_buttons']" + +%fieldset.no-border-bottom + %legend{align: 'center'} Distribution + + - if @order.complete? + .alpha.six.columns + %p + %b Distributor: + = f.object.distributor.andand.name || "None" + = f.hidden_field :distributor_id + .omega.six.columns + %p + %b Order cycle: + = f.object.order_cycle.andand.name || "None" + = f.hidden_field :order_cycle_id + + - else + .alpha.six.columns + .field + %label{for: "order_distributor_id"} Distributor + %select{id: "order_distributor_id", name: "order[distributor_id]", 'ng-model' => 'distributor_id'} + %option{"ng-repeat" => "shop in shops", "ng-value" => "shop.id", "ng-selected" => "distributor_id == shop.id", "ng-bind" => "shop.name"} + + .omega.six.columns + .field + %label{for: "order_order_cycle_id"} Order Cycle + %select{id: "order_order_cycle_id", name: "order[order_cycle_id]", 'ng-model' => 'order_cycle_id'} + %option{"ng-repeat" => "oc in orderCycles", "ng-value" => "oc.id", "ng-selected" => "order_cycle_id == oc.id", "ng-bind" => "oc.name"} diff --git a/app/views/spree/admin/line_items/create.js.erb b/app/views/spree/admin/line_items/create.js.erb new file mode 100644 index 0000000000..58afe5c8ef --- /dev/null +++ b/app/views/spree/admin/line_items/create.js.erb @@ -0,0 +1,5 @@ +// The admin order form contains angular directives, so it needs to be +// compiled before insertion into the DOM +var scope = angular.element("#order-form-wrapper").scope(); +$("#order-form-wrapper").html(scope.$compile('<%= escape_javascript(render :partial => "spree/admin/orders/form") %>')(scope)); +scope.$apply(); diff --git a/app/views/spree/admin/orders/_distribution_fields.html.haml b/app/views/spree/admin/orders/_distribution_fields.html.haml deleted file mode 100644 index 01753e025b..0000000000 --- a/app/views/spree/admin/orders/_distribution_fields.html.haml +++ /dev/null @@ -1,14 +0,0 @@ -%fieldset.no-border-bottom - %legend{align: 'center'} Distribution - - .alpha.six.columns - .field - %label{for: "distributor_id"} Distributor - %select{id: "distributor_id", 'ng-model' => "", 'ng-options' => ''} - -#= f.collection_select :distributor_id, Enterprise.is_distributor.managed_by(spree_current_user), :id, :name, include_blank: true - - .omega.six.columns - .field - %label{for: "order_cycle_id"} Order Cycle - %select{id: "order_cycle_id", 'ng-model' => "", 'ng-options' => ''} - -#= f.collection_select :order_cycle_id, OrderCycle.managed_by(spree_current_user), :id, :name, include_blank: true diff --git a/app/views/spree/admin/orders/new.html.haml b/app/views/spree/admin/orders/new.html.haml index 50b58ce62f..0404790595 100644 --- a/app/views/spree/admin/orders/new.html.haml +++ b/app/views/spree/admin/orders/new.html.haml @@ -14,14 +14,13 @@ %div{"data-hook" => "admin_order_new_header"} = render 'spree/shared/error_messages', :target => @order -= render 'distribution_fields' +%div{"ng-app" => "admin.orders", "ng-controller" => "ordersCtrl"} + = render 'add_product' -= render 'add_product' - -- unless @order.line_items.any? - %div{"data-hook" => "admin_order_new_form"} - #order-form-wrapper - = render 'form' + - unless @order.line_items.any? + %div{"data-hook" => "admin_order_new_form"} + #order-form-wrapper + = render 'form' - content_for :head do = javascript_tag 'var expand_variants = true;' diff --git a/spec/features/admin/orders_spec.rb b/spec/features/admin/orders_spec.rb index 575d37d7e7..ba085b1a89 100644 --- a/spec/features/admin/orders_spec.rb +++ b/spec/features/admin/orders_spec.rb @@ -22,23 +22,24 @@ feature %q{ end scenario "creating an order with distributor and order cycle", js: true, retry: 3 do - order_cycle = create(:order_cycle) - distributor = order_cycle.distributors.first - product = order_cycle.products.first + distributor = create(:distributor_enterprise) + product = create(:simple_product) + order_cycle = create(:simple_order_cycle, distributors: [distributor], variants: [product.variants.first]) login_to_admin_section visit '/admin/orders' click_link 'New Order' + select distributor.name, from: 'order_distributor_id' + select order_cycle.name, from: 'order_order_cycle_id' + page.should have_content 'ADD PRODUCT' targetted_select2_search product.name, from: '#add_variant_id', dropdown_css: '.select2-drop' click_link 'Add' page.has_selector? "table.index tbody[data-hook='admin_order_form_line_items'] tr" # Wait for JS page.should have_selector 'td', text: product.name - select distributor.name, from: 'order_distributor_id' - select order_cycle.name, from: 'order_order_cycle_id' click_button 'Update' page.should have_selector 'h1', text: 'Customer Details'