Fixing regressions and refactoring our tests

This commit is contained in:
Will Marshall
2014-07-16 16:00:44 +10:00
parent cd033c300e
commit 2b1ab53d8e
14 changed files with 304 additions and 289 deletions

View File

@@ -1,12 +1,22 @@
Darkswarm.controller "CheckoutCtrl", ($scope, storage, Order, CurrentUser) ->
$scope.Order = Order
Order.bindFieldsToLocalStorage($scope)
Darkswarm.controller "CheckoutCtrl", ($scope, storage, Checkout, CurrentUser, CurrentHub) ->
$scope.Checkout = Checkout
$scope.order = Order.order # Ordering is important
$scope.secrets = Order.secrets
# Bind to local storage
$scope.fieldsToBind = ["bill_address", "email", "payment_method_id", "shipping_method_id", "ship_address"]
prefix = "order_#{Checkout.order.id}#{Checkout.order.user_id}#{CurrentHub.hub.id}"
for field in $scope.fieldsToBind
storage.bind $scope, "Checkout.order.#{field}",
storeName: "#{prefix}_#{field}"
storage.bind $scope, "Checkout.ship_address_same_as_billing",
storeName: "#{prefix}_sameasbilling"
defaultValue: true
$scope.order = Checkout.order # Ordering is important
$scope.secrets = Checkout.secrets
$scope.enabled = if CurrentUser then true else false
$scope.purchase = (event)->
event.preventDefault()
$scope.Order.submit()
$scope.Checkout.submit()

View File

@@ -1,3 +1,5 @@
# TODO this SUCKS. Fix it
Darkswarm.controller "OrderCycleCtrl", ($scope, OrderCycle, $timeout) ->
$scope.order_cycle = OrderCycle.order_cycle
$scope.OrderCycle = OrderCycle

View File

@@ -0,0 +1,5 @@
Darkswarm.factory 'Cart', (Order)->
# Handles syncing of current cart/order state to server
new class Cart
order: Order.order

View File

@@ -0,0 +1,62 @@
Darkswarm.factory 'Checkout', (Order, $http, Navigation, CurrentHub, RailsFlashLoader, Loading)->
new class Checkout
errors: {}
secrets: {}
order: Order.order
ship_address_same_as_billing: true
submit: ->
Loading.message = "Submitting your order: please wait"
$http.put('/checkout', {order: @preprocess()}).success (data, status)=>
Navigation.go data.pat
.error (response, status)=>
Loading.clear()
@errors = response.errors
RailsFlashLoader.loadFlash(response.flash)
# Rails wants our Spree::Address data to be provided with _attributes
preprocess: ->
munged_order = {}
for name, value of @order # Clone all data from the order JSON object
switch name
when "bill_address"
munged_order["bill_address_attributes"] = value
when "ship_address"
munged_order["ship_address_attributes"] = value
when "payment_method_id"
munged_order["payments_attributes"] = [{payment_method_id: value}]
when "form_state" # don't keep this shit
else
munged_order[name] = value
if @ship_address_same_as_billing
munged_order.ship_address_attributes = munged_order.bill_address_attributes
if @paymentMethod()?.method_type == 'gateway'
angular.extend munged_order.payments_attributes[0], {
source_attributes:
number: @secrets.card_number
month: @secrets.card_month
year: @secrets.card_year
verification_value: @secrets.card_verification_value
first_name: @order.bill_address.firstname
last_name: @order.bill_address.lastname
}
munged_order
shippingMethod: ->
@order.shipping_methods[@order.shipping_method_id] if @order.shipping_method_id
requireShipAddress: ->
@shippingMethod()?.require_ship_address
shippingPrice: ->
@shippingMethod()?.price || 0.0
paymentMethod: ->
@order.payment_methods[@order.payment_method_id]
cartTotal: ->
@shippingPrice() + @order.display_total

View File

@@ -1,77 +1,5 @@
Darkswarm.factory 'Order', ($resource, order, $http, Navigation, storage, CurrentHub, RailsFlashLoader, Loading)->
Darkswarm.factory 'Order', (order)->
new class Order
errors: {}
secrets: {}
order: order
ship_address_same_as_billing: true
#Whitelist of fields from Order.order to bind into localStorage
fieldsToBind: ["bill_address", "email", "payment_method_id", "shipping_method_id", "ship_address"]
# Bind all the fields from fieldsToBind, + anything on the Order class
bindFieldsToLocalStorage: (scope)=>
prefix = "order_#{@order.id}#{@order.user_id}#{CurrentHub.hub.id}"
for field in @fieldsToBind
storage.bind scope, "Order.order.#{field}",
storeName: "#{prefix}_#{field}"
storage.bind scope, "Order.ship_address_same_as_billing",
storeName: "#{prefix}_sameasbilling"
defaultValue: true
submit: ->
Loading.message = "Submitting your order: please wait"
$http.put('/checkout', {order: @preprocess()}).success (data, status)=>
Navigation.go data.path
.error (response, status)=>
Loading.clear()
@errors = response.errors
RailsFlashLoader.loadFlash(response.flash)
# Rails wants our Spree::Address data to be provided with _attributes
preprocess: ->
munged_order = {}
for name, value of @order # Clone all data from the order JSON object
switch name
when "bill_address"
munged_order["bill_address_attributes"] = value
when "ship_address"
munged_order["ship_address_attributes"] = value
when "payment_method_id"
munged_order["payments_attributes"] = [{payment_method_id: value}]
when "form_state" # don't keep this shit
else
munged_order[name] = value
if @ship_address_same_as_billing
munged_order.ship_address_attributes = munged_order.bill_address_attributes
if @paymentMethod()?.method_type == 'gateway'
angular.extend munged_order.payments_attributes[0], {
source_attributes:
number: @secrets.card_number
month: @secrets.card_month
year: @secrets.card_year
verification_value: @secrets.card_verification_value
first_name: @order.bill_address.firstname
last_name: @order.bill_address.lastname
}
munged_order
shippingMethod: ->
@order.shipping_methods[@order.shipping_method_id] if @order.shipping_method_id
requireShipAddress: ->
@shippingMethod()?.require_ship_address
shippingPrice: ->
@shippingMethod()?.price || 0.0
paymentMethod: ->
@order.payment_methods[@order.payment_method_id]
cartTotal: ->
@shippingPrice() + @order.display_total

View File

@@ -23,8 +23,8 @@ Darkswarm.factory 'Product', ($resource, Enterprises, Dereferencer, Taxons) ->
extend: ->
for product in @products
if product.variants.length > 0
if product.variants?.length > 0
prices = (v.price for v in product.variants)
product.price = Math.min.apply(null, prices)
product.hasVariants = product.variants.length > 0
product.hasVariants = product.variants?.length > 0