Moving payment methods and shipping methods into their own services

This commit is contained in:
Will Marshall
2014-07-17 14:19:39 +10:00
parent 2b1ab53d8e
commit 547d9f9f5c
17 changed files with 63 additions and 91 deletions

View File

@@ -1,5 +1,6 @@
Darkswarm.controller "ShippingCtrl", ($scope, $timeout) ->
Darkswarm.controller "ShippingCtrl", ($scope, $timeout, ShippingMethods) ->
angular.extend(this, new FieldsetMixin($scope))
$scope.ShippingMethods = ShippingMethods
$scope.name = "shipping"
$scope.nextPanel = "payment"

View File

@@ -1,4 +1,5 @@
Darkswarm.factory "AuthenticationService", (Navigation, $modal, $location, Redirections)->
new class AuthenticationService
selectedPath: "/login"

View File

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

View File

@@ -1,4 +1,4 @@
Darkswarm.factory 'Checkout', (Order, $http, Navigation, CurrentHub, RailsFlashLoader, Loading)->
Darkswarm.factory 'Checkout', (Order, ShippingMethods, PaymentMethods, $http, Navigation, CurrentHub, RailsFlashLoader, Loading)->
new class Checkout
errors: {}
secrets: {}
@@ -8,7 +8,7 @@ Darkswarm.factory 'Checkout', (Order, $http, Navigation, CurrentHub, RailsFlashL
submit: ->
Loading.message = "Submitting your order: please wait"
$http.put('/checkout', {order: @preprocess()}).success (data, status)=>
Navigation.go data.pat
Navigation.go data.path
.error (response, status)=>
Loading.clear()
@errors = response.errors
@@ -47,7 +47,7 @@ Darkswarm.factory 'Checkout', (Order, $http, Navigation, CurrentHub, RailsFlashL
munged_order
shippingMethod: ->
@order.shipping_methods[@order.shipping_method_id] if @order.shipping_method_id
ShippingMethods.shipping_methods_by_id[@order.shipping_method_id] if @order.shipping_method_id
requireShipAddress: ->
@shippingMethod()?.require_ship_address
@@ -56,7 +56,7 @@ Darkswarm.factory 'Checkout', (Order, $http, Navigation, CurrentHub, RailsFlashL
@shippingMethod()?.price || 0.0
paymentMethod: ->
@order.payment_methods[@order.payment_method_id]
PaymentMethods.payment_methods_by_id[@order.payment_method_id]
cartTotal: ->
@shippingPrice() + @order.display_total

View File

@@ -1,8 +1,5 @@
Darkswarm.factory 'CurrentOrder', (currentOrder) ->
new class CurrentOrder
constructor: ->
@[k] = v for k, v of currentOrder
@cart_count ?= 0
order: currentOrder
empty: =>
@line_items.length == 0
@order.line_items.length == 0

View File

@@ -1,5 +1,4 @@
Darkswarm.factory 'Order', (order)->
new class Order
errors: {}
order: order

View File

@@ -3,6 +3,10 @@ module EnterprisesHelper
@current_distributor ||= current_order(false).andand.distributor
end
def available_shipping_methods
current_distributor.shipping_methods.uniq
end
def managed_enterprises
Enterprise.managed_by(spree_current_user)
end

View File

@@ -1,21 +1,4 @@
module SharedHelper
def inject_enterprises
inject_json_ams "enterprises", Enterprise.all, Api::EnterpriseSerializer, active_distributors: @active_distributors
end
def inject_taxons
inject_json_ams "taxons", Spree::Taxon.all, Api::TaxonSerializer
end
def inject_json(name, partial, opts = {})
render partial: "json/injection", locals: {name: name, partial: partial}.merge(opts)
end
def inject_json_ams(name, data, serializer, opts = {})
json = ActiveModel::ArraySerializer.new(data, {each_serializer: serializer}.merge(opts)).to_json
render partial: "json/injection_ams", locals: {name: name, json: json}
end
def distributor_link_class(distributor)
cart = current_order(true)
@active_distributors ||= Enterprise.distributors_with_active_order_cycles

View File

@@ -5,6 +5,6 @@ class Api::AddressSerializer < ActiveModel::Serializer
attributes :id, :zipcode, :city, :state
def state
object.state.abbr
object.state.andand.abbr
end
end

View File

@@ -4,6 +4,8 @@
novalidate: true,
name: "checkout"} do |f|
= inject_available_shipping_methods
= inject_available_payment_methods
:javascript
angular.module('Darkswarm').value('order', #{render "checkout/order"})

View File

@@ -1,5 +1,5 @@
#NOTE: when adding new fields for user input, it may want to be cached in localStorage
# If so, make sure to add it to Order.attributes_to_cache
# If so, make sure to add it to controller attribute caching
object current_order
attributes :id, :email, :shipping_method_id, :user_id
@@ -20,6 +20,9 @@ child current_order.ship_address => :ship_address do
attributes :phone, :firstname, :lastname, :address1, :address2, :city, :country_id, :state_id, :zipcode
end
# This is actually totally decoupled data and should be injected separately into their
# own services
node :shipping_methods do
Hash[current_distributor.shipping_methods.uniq.collect { |method|
[method.id, {

View File

@@ -16,7 +16,7 @@
.small-11.columns
%em
%small
{{ Order.paymentMethod().name }}
{{ Checkout.paymentMethod().name }}
.small-1.columns.right
%span.accordion-up.right
%i.ofn-i_006-caret-up

View File

@@ -16,7 +16,7 @@
.small-11.columns
%em
%small
{{ Order.shippingMethod().name }}
{{ Checkout.shippingMethod().name }}
.small-1.columns.right
%span.accordion-up.right
%i.ofn-i_006-caret-up
@@ -24,20 +24,20 @@
%i.ofn-i_005-caret-down
.small-12.columns.medium-6.columns.large-6.columns
- for ship_method, i in current_distributor.shipping_methods.uniq
%label
-#= radio_button_tag "order[shipping_method_id]", ship_method.id, false,
-#"ng-model" => "order.shipping_method_id"
%input{type: :radio, value: ship_method.id,
required: true,
"ng-model" => "order.shipping_method_id"}
= ship_method.name
%label{"ng-if" => "Order.requireShipAddress()"}
%input{type: :checkbox, "ng-model" => "Order.ship_address_same_as_billing"}
%label{"ng-repeat" => "method in ShippingMethods.shipping_methods"}
%input{type: :radio,
required: true,
"ng-value" => "method.id",
"ng-model" => "order.shipping_method_id"}
{{ method.name }}
%label{"ng-if" => "Checkout.requireShipAddress()"}
%input{type: :checkbox, "ng-model" => "Checkout.ship_address_same_as_billing"}
Shipping address same as billing address?
.small-12.columns.medium-6.columns.large-6.columns
#distributor_address.panel{"ng-show" => "Order.shippingMethod().description"}
%span{ style: "white-space: pre-wrap;" }{{ Order.shippingMethod().description }}
#distributor_address.panel{"ng-show" => "Checkout.shippingMethod().description"}
%span{ style: "white-space: pre-wrap;" }{{ Checkout.shippingMethod().description }}
%br/
%br/
= 'Ready for:' if @order.order_cycle.pickup_time_for(@order.distributor)
@@ -45,8 +45,8 @@
= f.fields_for :ship_address, @order.ship_address do |sa|
.small-12.columns
#ship_address{"ng-if" => "Order.requireShipAddress()"}
%div.visible{"ng-if" => "!Order.ship_address_same_as_billing"}
#ship_address{"ng-if" => "Checkout.requireShipAddress()"}
%div.visible{"ng-if" => "!Checkout.ship_address_same_as_billing"}
.row
.small-12.columns
= validated_input "Address", "order.ship_address.address1", "ofn-focus" => "accordion['shipping']"

View File

@@ -13,10 +13,10 @@
%td= adjustment.display_amount.to_html
%tr
%th Shipping
%td {{ Order.shippingPrice() | currency }}
%td {{ Checkout.shippingPrice() | currency }}
%tr
%th Cart total
%td {{ Order.cartTotal() | currency }}
%td {{ Checkout.cartTotal() | currency }}
- if current_order.price_adjustment_totals.present?
- current_order.price_adjustment_totals.each do |label, total|
%tr

View File

@@ -25,10 +25,10 @@
= javascript_include_tag "iehack"
= inject_json "currentHub", "current_hub"
= inject_json "currentOrder", "current_order"
= inject_json "user", "current_user"
= inject_json "railsFlash", "flash"
= inject_taxons
= inject_current_order
.off-canvas-wrap{offcanvas: true}
.inner-wrap

View File

@@ -23,28 +23,4 @@ describe SharedHelper do
helper.stub(:current_order) { order }
helper.distributor_link_class(d1).should =~ /empties-cart/
end
describe "injecting json" do
let!(:enterprise) { create(:distributor_enterprise, facebook: "roger") }
it "will inject via AMS" do
helper.inject_json_ams("test", [enterprise], Api::EnterpriseSerializer).should match enterprise.name
end
it "injects enterprises" do
helper.inject_enterprises.should match enterprise.name
helper.inject_enterprises.should match enterprise.facebook
end
it "injects taxons" do
taxon = create(:taxon)
helper.inject_taxons.should match taxon.name
end
it "injects taxons" do
taxon = create(:taxon)
helper.inject_taxons.should match taxon.name
end
end
end

View File

@@ -7,6 +7,25 @@ describe 'Checkout service', ->
scope = null
FlashLoaderMock =
loadFlash: (arg)->
paymentMethods = [{
id: 99
test: "foo"
method_type: "gateway"
}, {
id: 123
test: "bar"
method_type: "check"
}]
shippingMethods = [
{
id: 7
require_ship_address: true
price: 0.0
}, {
id: 25
require_ship_address: false
price: 13
}]
beforeEach ->
orderData =
@@ -19,26 +38,13 @@ describe 'Checkout service', ->
lastname: "Harrington"
ship_address: {test: "bar"}
user_id: 901
shipping_methods:
7:
require_ship_address: true
price: 0.0
25:
require_ship_address: false
price: 13
payment_methods:
99:
test: "foo"
method_type: "gateway"
123:
test: "bar"
method_type: "check"
angular.module('Darkswarm').value('order', orderData)
module 'Darkswarm'
module ($provide)->
$provide.value "RailsFlashLoader", FlashLoaderMock
$provide.value "shippingMethods", shippingMethods
$provide.value "paymentMethods", paymentMethods
null
inject ($injector, _$httpBackend_, $rootScope)->
@@ -74,7 +80,7 @@ describe 'Checkout service', ->
it 'Gets the current payment method', ->
expect(Checkout.paymentMethod()).toEqual null
Checkout.order.payment_method_id = 99
expect(Checkout.paymentMethod()).toEqual {test: "foo", method_type: "gateway"}
expect(Checkout.paymentMethod()).toEqual paymentMethods[0]
it "Posts the Checkout to the server", ->
$httpBackend.expectPUT("/checkout", {order: Checkout.preprocess()}).respond 200, {path: "test"}