mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-27 21:06:49 +00:00
New pattern: blank Options for index drop down filters are added within service
Rather than in the controller after data is loaded
This commit is contained in:
@@ -1,16 +1,22 @@
|
||||
angular.module("admin.enterprises").factory 'Enterprises', ($q, EnterpriseResource) ->
|
||||
angular.module("admin.enterprises").factory 'Enterprises', ($q, EnterpriseResource, blankOption) ->
|
||||
new class Enterprises
|
||||
enterprisesByID: {}
|
||||
pristineByID: {}
|
||||
|
||||
index: (params={}, callback=null) ->
|
||||
EnterpriseResource.index params, (data) =>
|
||||
includeBlank = !!params['includeBlank']
|
||||
delete params['includeBlank']
|
||||
EnterpriseResource.index(params, (data) =>
|
||||
for enterprise in data
|
||||
@enterprisesByID[enterprise.id] = enterprise
|
||||
@pristineByID[enterprise.id] = angular.copy(enterprise)
|
||||
|
||||
(callback || angular.noop)(data)
|
||||
|
||||
data.unshift(blankOption()) if includeBlank
|
||||
data
|
||||
)
|
||||
|
||||
save: (enterprise) ->
|
||||
deferred = $q.defer()
|
||||
enterprise.$update({id: enterprise.permalink})
|
||||
|
||||
@@ -43,9 +43,9 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout,
|
||||
RequestMonitor.load $scope.lineItems = LineItems.index("q[order][state_not_eq]": "canceled", "q[order][completed_at_not_null]": "true", "q[order][completed_at_gt]": "#{$scope.startDate}", "q[order][completed_at_lt]": "#{$scope.endDate}")
|
||||
|
||||
unless $scope.initialized
|
||||
RequestMonitor.load $scope.distributors = Enterprises.index(action: "for_line_items", ams_prefix: "basic", "q[sells_in][]": ["own", "any"] )
|
||||
RequestMonitor.load $scope.orderCycles = OrderCycles.index(ams_prefix: "basic", as: "distributor", "q[orders_close_at_gt]": "#{formatDate(daysFromToday(-90))}")
|
||||
RequestMonitor.load $scope.suppliers = Enterprises.index(action: "for_line_items", ams_prefix: "basic", "q[is_primary_producer_eq]": "true" )
|
||||
RequestMonitor.load $scope.distributors = Enterprises.index(includeBlank: true, action: "for_line_items", ams_prefix: "basic", "q[sells_in][]": ["own", "any"])
|
||||
RequestMonitor.load $scope.orderCycles = OrderCycles.index(includeBlank: true, ams_prefix: "basic", as: "distributor", "q[orders_close_at_gt]": "#{formatDate(daysFromToday(-90))}")
|
||||
RequestMonitor.load $scope.suppliers = Enterprises.index(includeBlank: true, action: "for_line_items", ams_prefix: "basic", "q[is_primary_producer_eq]": "true")
|
||||
|
||||
RequestMonitor.load $q.all([$scope.orders.$promise, $scope.distributors.$promise, $scope.orderCycles.$promise]).then ->
|
||||
Dereferencer.dereferenceAttr $scope.orders, "distributor", Enterprises.enterprisesByID
|
||||
@@ -58,9 +58,6 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout,
|
||||
unless $scope.initialized
|
||||
$scope.initialized = true
|
||||
$timeout ->
|
||||
$scope.orderCycles.unshift blankOption()
|
||||
$scope.suppliers.unshift blankOption()
|
||||
$scope.distributors.unshift blankOption()
|
||||
$scope.resetSelectFilters()
|
||||
|
||||
$scope.refreshData()
|
||||
|
||||
@@ -1 +1 @@
|
||||
angular.module('admin.orderCycles', ['ngResource'])
|
||||
angular.module('admin.orderCycles', ['ngResource', 'admin.indexUtils'])
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
angular.module("admin.orderCycles").factory 'OrderCycles', ($q, OrderCycleResource) ->
|
||||
angular.module("admin.orderCycles").factory 'OrderCycles', ($q, OrderCycleResource, blankOption) ->
|
||||
new class OrderCycles
|
||||
orderCyclesByID: {}
|
||||
pristineByID: {}
|
||||
|
||||
index: (params={}, callback=null) ->
|
||||
OrderCycleResource.index params, (data) =>
|
||||
includeBlank = !!params['includeBlank']
|
||||
delete params['includeBlank']
|
||||
OrderCycleResource.index(params, (data) =>
|
||||
for orderCycle in data
|
||||
@orderCyclesByID[orderCycle.id] = orderCycle
|
||||
@pristineByID[orderCycle.id] = angular.copy(orderCycle)
|
||||
|
||||
(callback || angular.noop)(data)
|
||||
|
||||
data.unshift(blankOption()) if includeBlank
|
||||
data
|
||||
)
|
||||
|
||||
save: (order_cycle) ->
|
||||
deferred = $q.defer()
|
||||
order_cycle.$update({id: order_cycle.id})
|
||||
|
||||
@@ -18,19 +18,43 @@ describe "Enterprises service", ->
|
||||
|
||||
beforeEach ->
|
||||
response = [{ id: 5, name: 'Enterprise 1'}]
|
||||
$httpBackend.expectGET('/admin/enterprises.json').respond 200, response
|
||||
result = Enterprises.index()
|
||||
$httpBackend.flush()
|
||||
|
||||
it "stores returned data in @enterprisesByID, with ids as keys", ->
|
||||
# EnterpriseResource returns instances of Resource rather than raw objects
|
||||
expect(Enterprises.enterprisesByID).toDeepEqual { 5: response[0] }
|
||||
describe "when no params are passed", ->
|
||||
beforeEach ->
|
||||
$httpBackend.expectGET('/admin/enterprises.json').respond 200, response
|
||||
result = Enterprises.index()
|
||||
$httpBackend.flush()
|
||||
|
||||
it "stores returned data in @pristineByID, with ids as keys", ->
|
||||
expect(Enterprises.pristineByID).toDeepEqual { 5: response[0] }
|
||||
it "stores returned data in @enterprisesByID, with ids as keys", ->
|
||||
# EnterpriseResource returns instances of Resource rather than raw objects
|
||||
expect(Enterprises.enterprisesByID).toDeepEqual { 5: response[0] }
|
||||
|
||||
it "returns an array of enterprises", ->
|
||||
expect(result).toDeepEqual response
|
||||
it "stores returned data in @pristineByID, with ids as keys", ->
|
||||
expect(Enterprises.pristineByID).toDeepEqual { 5: response[0] }
|
||||
|
||||
it "returns an array of enterprises", ->
|
||||
expect(result).toDeepEqual response
|
||||
|
||||
describe "when params are passed", ->
|
||||
describe "where includeBlank param is truthy", ->
|
||||
beforeEach ->
|
||||
params = {includeBlank: true, someParam: 'someVal'}
|
||||
$httpBackend.expectGET('/admin/enterprises.json?someParam=someVal').respond 200, response
|
||||
result = Enterprises.index(params)
|
||||
$httpBackend.flush()
|
||||
|
||||
it "returns an array of enterprises, with a blank option appended to the beginning", ->
|
||||
expect(result).toDeepEqual [{id: '0', name: 'All'} ,{ id: 5, name: 'Enterprise 1'}]
|
||||
|
||||
describe "where includeBlank param is falsey", ->
|
||||
beforeEach ->
|
||||
params = {includeBlank: false, someParam: 'someVal'}
|
||||
$httpBackend.expectGET('/admin/enterprises.json?someParam=someVal').respond 200, response
|
||||
result = Enterprises.index(params)
|
||||
$httpBackend.flush()
|
||||
|
||||
it "returns an array of enterprises, with a blank option appended to the beginning", ->
|
||||
expect(result).toDeepEqual response
|
||||
|
||||
|
||||
describe "#save", ->
|
||||
|
||||
@@ -18,19 +18,43 @@ describe "OrderCycles service", ->
|
||||
|
||||
beforeEach ->
|
||||
response = [{ id: 5, name: 'OrderCycle 1'}]
|
||||
$httpBackend.expectGET('/admin/order_cycles.json').respond 200, response
|
||||
result = OrderCycles.index()
|
||||
$httpBackend.flush()
|
||||
|
||||
it "stores returned data in @orderCyclesByID, with ids as keys", ->
|
||||
# OrderCycleResource returns instances of Resource rather than raw objects
|
||||
expect(OrderCycles.orderCyclesByID).toDeepEqual { 5: response[0] }
|
||||
describe "when no params are passed", ->
|
||||
beforeEach ->
|
||||
$httpBackend.expectGET('/admin/order_cycles.json').respond 200, response
|
||||
result = OrderCycles.index()
|
||||
$httpBackend.flush()
|
||||
|
||||
it "stores returned data in @pristineByID, with ids as keys", ->
|
||||
expect(OrderCycles.pristineByID).toDeepEqual { 5: response[0] }
|
||||
it "stores returned data in @orderCyclesByID, with ids as keys", ->
|
||||
# OrderCycleResource returns instances of Resource rather than raw objects
|
||||
expect(OrderCycles.orderCyclesByID).toDeepEqual { 5: response[0] }
|
||||
|
||||
it "returns an array of orderCycles", ->
|
||||
expect(result).toDeepEqual response
|
||||
it "stores returned data in @pristineByID, with ids as keys", ->
|
||||
expect(OrderCycles.pristineByID).toDeepEqual { 5: response[0] }
|
||||
|
||||
it "returns an array of orderCycles", ->
|
||||
expect(result).toDeepEqual response
|
||||
|
||||
describe "when no params are passed", ->
|
||||
describe "where includeBlank param is truthy", ->
|
||||
beforeEach ->
|
||||
params = {includeBlank: true, someParam: 'someVal'}
|
||||
$httpBackend.expectGET('/admin/order_cycles.json?someParam=someVal').respond 200, response
|
||||
result = OrderCycles.index(params)
|
||||
$httpBackend.flush()
|
||||
|
||||
it "returns an array of orderCycles", ->
|
||||
expect(result).toDeepEqual [{id: '0', name: 'All'} ,{ id: 5, name: 'OrderCycle 1'}]
|
||||
|
||||
describe "where includeBlank param is falsey", ->
|
||||
beforeEach ->
|
||||
params = {includeBlank: false, someParam: 'someVal'}
|
||||
$httpBackend.expectGET('/admin/order_cycles.json?someParam=someVal').respond 200, response
|
||||
result = OrderCycles.index(params)
|
||||
$httpBackend.flush()
|
||||
|
||||
it "returns an array of orderCycles", ->
|
||||
expect(result).toDeepEqual response
|
||||
|
||||
|
||||
describe "#save", ->
|
||||
|
||||
Reference in New Issue
Block a user