Preprocessing order attributes, returning path

This commit is contained in:
Will Marshall
2014-04-09 15:04:17 +10:00
parent b5550c048a
commit 36a4e34dff
5 changed files with 42 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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

View File

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