From 36a4e34dffc1dd9e5d66ba743eda26128111a34b Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 9 Apr 2014 15:04:17 +1000 Subject: [PATCH] Preprocessing order attributes, returning path --- .../darkswarm/services/order.js.coffee | 19 ++++++++++++++----- app/controllers/shop/checkout_controller.rb | 10 ++++++++-- app/views/shop/checkout/edit.html.haml | 3 +++ .../shop/checkout_controller_spec.rb | 11 ++++++++++- .../darkswarm/services/order_spec.js.coffee | 7 +++++++ 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/order.js.coffee b/app/assets/javascripts/darkswarm/services/order.js.coffee index 6310d875bb..913b0ae789 100644 --- a/app/assets/javascripts/darkswarm/services/order.js.coffee +++ b/app/assets/javascripts/darkswarm/services/order.js.coffee @@ -8,15 +8,24 @@ Darkswarm.factory 'Order', ($resource, Product, order, $http)-> @order.ship_address_same_as_billing ?= true submit: -> - $http.put('/shop/checkout', {order: @preprocess()}).success (data, status)-> - console.log "success" + $http.put('/shop/checkout', {order: @preprocess()}).success (data, status)=> console.log data - .error (data, status)-> + # Navigate to order confirmation + .error (errors, status)=> console.log "error" - console.log data + @errors = errors + # Rails wants our Spree::Address data to be provided with _attributes preprocess: -> - @order + munged_order = {} + for name, value of @order # Clone all data from the order JSON object + if name == "bill_address" + munged_order["bill_address_attributes"] = value + else if name == "ship_address" + munged_order["ship_address_attributes"] = value + else + munged_order[name] = value + munged_order shippingMethod: -> @order.shipping_methods[@order.shipping_method_id] diff --git a/app/controllers/shop/checkout_controller.rb b/app/controllers/shop/checkout_controller.rb index 2199d4be03..0ffe90a91f 100644 --- a/app/controllers/shop/checkout_controller.rb +++ b/app/controllers/shop/checkout_controller.rb @@ -26,10 +26,16 @@ class Shop::CheckoutController < Spree::CheckoutController return end end - if @order.state == "complete" || @order.completed? flash.notice = t(:order_processed_successfully) - respond_with(@order, :location => order_path(@order)) + respond_to do |format| + format.html do + respond_with(@order, :location => order_path(@order)) + end + format.js do + render json: {path: order_path(@order)}, status: 200 + end + end else update_failed end diff --git a/app/views/shop/checkout/edit.html.haml b/app/views/shop/checkout/edit.html.haml index 17ded5cc12..6d1ef446c7 100644 --- a/app/views/shop/checkout/edit.html.haml +++ b/app/views/shop/checkout/edit.html.haml @@ -8,6 +8,9 @@ %accordion.row{"close-others" => "true"} %checkout{"ng-controller" => "CheckoutCtrl"} + + %pre + {{ Order.errors | json }} .large-9.columns - unless spree_current_user = render partial: "shop/checkout/authentication" diff --git a/spec/controllers/shop/checkout_controller_spec.rb b/spec/controllers/shop/checkout_controller_spec.rb index c6d6851208..31f96587c9 100644 --- a/spec/controllers/shop/checkout_controller_spec.rb +++ b/spec/controllers/shop/checkout_controller_spec.rb @@ -81,7 +81,6 @@ describe Shop::CheckoutController do context "via xhr" do before do controller.stub(:current_distributor).and_return(distributor) - controller.stub(:current_order_cycle).and_return(order_cycle) controller.stub(:current_order).and_return(order) end @@ -91,6 +90,16 @@ describe Shop::CheckoutController do response.status.should == 400 response.body.should == assigns[:order].errors.to_json end + + it "returns order confirmation url on success" do + order.stub(:update_attributes).and_return true + order.stub(:state).and_return "complete" + #order.stub(:completed?).and_return true + + xhr :post, :update, order: {}, use_route: :spree + response.status.should == 200 + response.body.should == {path: spree.order_path(order)}.to_json + end end describe "Paypal routing" do diff --git a/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee index f6190bdfd0..5754705c14 100644 --- a/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/order_spec.js.coffee @@ -7,6 +7,8 @@ describe 'Order service', -> orderData = { id: 3102 payment_method_id: null + bill_address: {} + ship_address: {} shipping_methods: 7: require_ship_address: true @@ -52,3 +54,8 @@ describe 'Order service', -> Order.submit() $httpBackend.flush() + it "Munges the order attributes to add _attributes as Rails needs", -> + expect(Order.preprocess().bill_address_attributes).not.toBe(undefined) + expect(Order.preprocess().bill_address).toBe(undefined) + expect(Order.preprocess().ship_address_attributes).not.toBe(undefined) + expect(Order.preprocess().ship_address).toBe(undefined)