Split out payment and shipping methods from admin enterprises controller as services

This commit is contained in:
Rob Harrington
2015-01-22 12:19:41 +11:00
parent 4829e59663
commit b429be707c
9 changed files with 144 additions and 98 deletions

View File

@@ -1,10 +1,10 @@
angular.module("admin.enterprises")
.controller "enterpriseCtrl", ($scope, NavigationCheck, Enterprise, PaymentMethods, ShippingMethods, SideMenu) ->
.controller "enterpriseCtrl", ($scope, NavigationCheck, Enterprise, EnterprisePaymentMethods, EnterpriseShippingMethods, SideMenu) ->
$scope.Enterprise = Enterprise.enterprise
$scope.PaymentMethods = PaymentMethods.paymentMethods
$scope.ShippingMethods = ShippingMethods.shippingMethods
console.log Enterprise
$scope.PaymentMethods = EnterprisePaymentMethods.paymentMethods
$scope.ShippingMethods = EnterpriseShippingMethods.shippingMethods
$scope.navClear = NavigationCheck.clear
# htmlVariable is used by textAngular wysiwyg for the long descrtiption.
$scope.pristineEmail = $scope.Enterprise.email
$scope.menu = SideMenu
@@ -17,33 +17,3 @@ angular.module("admin.enterprises")
# Register the NavigationCheck callback
NavigationCheck.register(enterpriseNavCallback)
for payment_method in $scope.PaymentMethods
payment_method.selected = payment_method.id in $scope.Enterprise.payment_method_ids
$scope.paymentMethodsColor = ->
if $scope.PaymentMethods.length > 0
if $scope.selectedPaymentMethodsCount() > 0 then "blue" else "red"
else
"red"
$scope.selectedPaymentMethodsCount = ->
$scope.PaymentMethods.reduce (count, payment_method) ->
count++ if payment_method.selected
count
, 0
for shipping_method in $scope.ShippingMethods
shipping_method.selected = shipping_method.id in $scope.Enterprise.shipping_method_ids
$scope.shippingMethodsColor = ->
if $scope.ShippingMethods.length > 0
if $scope.selectedShippingMethodsCount() > 0 then "blue" else "red"
else
"red"
$scope.selectedShippingMethodsCount = ->
$scope.ShippingMethods.reduce (count, shipping_method) ->
count++ if shipping_method.selected
count
, 0

View File

@@ -0,0 +1,20 @@
angular.module("admin.enterprises")
.factory "EnterprisePaymentMethods", (Enterprise, PaymentMethods) ->
new class EnterprisePaymentMethods
paymentMethods: PaymentMethods.paymentMethods
constructor: ->
for payment_method in @paymentMethods
payment_method.selected = payment_method.id in Enterprise.enterprise.payment_method_ids
displayColor: ->
if @paymentMethods.length > 0 && @selectedCount() > 0
"blue"
else
"red"
selectedCount: ->
@paymentMethods.reduce (count, payment_method) ->
count++ if payment_method.selected
count
, 0

View File

@@ -0,0 +1,20 @@
angular.module("admin.enterprises")
.factory "EnterpriseShippingMethods", (Enterprise, ShippingMethods) ->
new class EnterpriseShippingMethods
shippingMethods: ShippingMethods.shippingMethods
constructor: ->
for shipping_method in @shippingMethods
shipping_method.selected = shipping_method.id in Enterprise.enterprise.shipping_method_ids
displayColor: ->
if @shippingMethods.length > 0 && @selectedCount() > 0
"blue"
else
"red"
selectedCount: ->
@shippingMethods.reduce (count, shipping_method) ->
count++ if shipping_method.selected
count
, 0

View File

@@ -2,7 +2,7 @@ angular.module("admin.payment_methods")
.factory "PaymentMethods", (paymentMethods) ->
new class PaymentMethods
paymentMethods: paymentMethods
findByID: (id) ->
for paymentMethod in @paymentMethods
return paymentMethod if paymentMethod.id is id

View File

@@ -2,7 +2,7 @@ angular.module("admin.shipping_methods")
.factory "ShippingMethods", (shippingMethods) ->
new class ShippingMethods
shippingMethods: shippingMethods
findByID: (id) ->
for shippingMethod in @shippingMethods
return shippingMethod if shippingMethod.id is id

View File

@@ -9,18 +9,16 @@ describe "enterpriseCtrl", ->
module('admin.enterprises')
Enterprise =
enterprise:
payment_method_ids: [ 1, 3 ]
shipping_method_ids: [ 2, 4 ]
is_primary_producer: true
sells: "none"
PaymentMethods =
paymentMethods: [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ]
paymentMethods: "payment methods"
ShippingMethods =
shippingMethods: [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ]
shippingMethods: "shipping methods"
inject ($rootScope, $controller) ->
scope = $rootScope
ctrl = $controller 'enterpriseCtrl', {$scope: scope, Enterprise: Enterprise, PaymentMethods: PaymentMethods, ShippingMethods: ShippingMethods}
ctrl = $controller 'enterpriseCtrl', {$scope: scope, Enterprise: Enterprise, EnterprisePaymentMethods: PaymentMethods, EnterpriseShippingMethods: ShippingMethods}
describe "initialisation", ->
it "stores enterprise", ->
@@ -31,57 +29,3 @@ describe "enterpriseCtrl", ->
it "stores shipping methods", ->
expect(scope.ShippingMethods).toBe ShippingMethods.shippingMethods
it "sets the selected property of each payment method", ->
expect(PaymentMethods.paymentMethods[0].selected).toBe true
expect(PaymentMethods.paymentMethods[1].selected).toBe false
expect(PaymentMethods.paymentMethods[2].selected).toBe true
expect(PaymentMethods.paymentMethods[3].selected).toBe false
it "sets the selected property of each shipping method", ->
expect(ShippingMethods.shippingMethods[0].selected).toBe false
expect(ShippingMethods.shippingMethods[1].selected).toBe true
expect(ShippingMethods.shippingMethods[2].selected).toBe false
expect(ShippingMethods.shippingMethods[3].selected).toBe true
describe "determining payment method colour", ->
it "returns 'blue' when at least one payment method is selected", ->
scope.PaymentMethods = [ { id: 1 } ]
spyOn(scope, "selectedPaymentMethodsCount").andReturn 1
expect(scope.paymentMethodsColor()).toBe "blue"
it "returns 'red' when no payment methods are selected", ->
scope.PaymentMethods = [ { id: 1 } ]
spyOn(scope, "selectedPaymentMethodsCount").andReturn 0
expect(scope.paymentMethodsColor()).toBe "red"
it "returns 'red' when no payment methods exist", ->
scope.PaymentMethods = [ ]
spyOn(scope, "selectedPaymentMethodsCount").andReturn 1
expect(scope.paymentMethodsColor()).toBe "red"
describe "counting selected payment methods", ->
it "counts only payment methods with selected: true", ->
scopePaymentMethods = [ { selected: true }, { selected: false }, { selected: false }, { selected: true } ]
expect(scope.selectedPaymentMethodsCount()).toBe 2
describe "determining shipping method colour", ->
it "returns 'blue' when at least one shipping method is selected", ->
scope.ShippingMethods = [ { id: 1 } ]
spyOn(scope, "selectedShippingMethodsCount").andReturn 1
expect(scope.shippingMethodsColor()).toBe "blue"
it "returns 'red' when no shipping methods are selected", ->
scope.ShippingMethods = [ { id: 1 } ]
spyOn(scope, "selectedShippingMethodsCount").andReturn 0
expect(scope.shippingMethodsColor()).toBe "red"
it "returns 'red' when no shipping method exist", ->
scope.ShippingMethods = [ ]
spyOn(scope, "selectedShippingMethodsCount").andReturn 1
expect(scope.shippingMethodsColor()).toBe "red"
describe "counting selected shipping methods", ->
it "counts only shipping methods with selected: true", ->
scope.ShippingMethods = [ { selected: true }, { selected: true }, { selected: false }, { selected: true } ]
expect(scope.selectedShippingMethodsCount()).toBe 3

View File

@@ -0,0 +1,45 @@
describe "EnterprisePaymentMethods service", ->
Enterprise = null
PaymentMethods = null
EnterprisePaymentMethods = null
beforeEach ->
Enterprise =
enterprise:
payment_method_ids: [ 1, 3 ]
PaymentMethods =
paymentMethods: [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ]
module 'admin.enterprises'
module ($provide) ->
$provide.value 'PaymentMethods', PaymentMethods
$provide.value 'Enterprise', Enterprise
null
inject (_EnterprisePaymentMethods_) ->
EnterprisePaymentMethods = _EnterprisePaymentMethods_
describe "selecting payment methods", ->
it "sets the selected property of each payment method", ->
expect(PaymentMethods.paymentMethods[0].selected).toBe true
expect(PaymentMethods.paymentMethods[1].selected).toBe false
expect(PaymentMethods.paymentMethods[2].selected).toBe true
expect(PaymentMethods.paymentMethods[3].selected).toBe false
describe "determining payment method colour", ->
it "returns 'blue' when at least one payment method is selected", ->
spyOn(EnterprisePaymentMethods, "selectedCount").andReturn 1
expect(EnterprisePaymentMethods.displayColor()).toBe "blue"
it "returns 'red' when no payment methods are selected", ->
spyOn(EnterprisePaymentMethods, "selectedCount").andReturn 0
expect(EnterprisePaymentMethods.displayColor()).toBe "red"
it "returns 'red' when no payment methods exist", ->
EnterprisePaymentMethods.paymentMethods = []
spyOn(EnterprisePaymentMethods, "selectedCount").andReturn 1
expect(EnterprisePaymentMethods.displayColor()).toBe "red"
describe "counting selected payment methods", ->
it "counts only payment methods with selected: true", ->
expect(EnterprisePaymentMethods.selectedCount()).toBe 2

View File

@@ -0,0 +1,45 @@
describe "EnterpriseShippingMethods service", ->
Enterprise = null
ShippingMethods = null
EnterpriseShippingMethods = null
beforeEach ->
Enterprise =
enterprise:
shipping_method_ids: [ 1, 3 ]
ShippingMethods =
shippingMethods: [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ]
module 'admin.enterprises'
module ($provide) ->
$provide.value 'ShippingMethods', ShippingMethods
$provide.value 'Enterprise', Enterprise
null
inject (_EnterpriseShippingMethods_) ->
EnterpriseShippingMethods = _EnterpriseShippingMethods_
describe "selecting shipping methods", ->
it "sets the selected property of each shipping method", ->
expect(ShippingMethods.shippingMethods[0].selected).toBe true
expect(ShippingMethods.shippingMethods[1].selected).toBe false
expect(ShippingMethods.shippingMethods[2].selected).toBe true
expect(ShippingMethods.shippingMethods[3].selected).toBe false
describe "determining shipping method colour", ->
it "returns 'blue' when at least one shipping method is selected", ->
spyOn(EnterpriseShippingMethods, "selectedCount").andReturn 1
expect(EnterpriseShippingMethods.displayColor()).toBe "blue"
it "returns 'red' when no shipping methods are selected", ->
spyOn(EnterpriseShippingMethods, "selectedCount").andReturn 0
expect(EnterpriseShippingMethods.displayColor()).toBe "red"
it "returns 'red' when no shipping methods exist", ->
EnterpriseShippingMethods.shippingMethods = []
spyOn(EnterpriseShippingMethods, "selectedCount").andReturn 1
expect(EnterpriseShippingMethods.displayColor()).toBe "red"
describe "counting selected shipping methods", ->
it "counts only shipping methods with selected: true", ->
expect(EnterpriseShippingMethods.selectedCount()).toBe 2

View File

@@ -3,10 +3,12 @@ describe "Enterprise service", ->
enterprise = { name: "test ent name" }
beforeEach ->
module 'admin.enterprises'
angular.module('admin.enterprises').value('enterprise', enterprise)
module ($provide) ->
$provide.value 'enterprise', enterprise
null
inject ($injector) ->
Enterprise = $injector.get("Enterprise")
Enterprise = $injector.get("Enterprise")
it "stores enterprise value as Enterprise.enterprise", ->
expect(Enterprise.enterprise).toBe enterprise
expect(Enterprise.enterprise).toBe enterprise