mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Adding dereferencing on construct to Enterprises service - this is MAGIC
This commit is contained in:
@@ -5,3 +5,14 @@ Darkswarm.factory 'Enterprises', (enterprises)->
|
||||
@enterprises = enterprises
|
||||
for enterprise in enterprises
|
||||
@enterprises_by_id[enterprise.id] = enterprise
|
||||
@dereference()
|
||||
|
||||
dereference: ->
|
||||
for enterprise in @enterprises
|
||||
if enterprise.hubs
|
||||
for hub, i in enterprise.hubs
|
||||
enterprise.hubs[i] = @enterprises_by_id[hub.id]
|
||||
|
||||
if enterprise.producers
|
||||
for producer, i in enterprise.producers
|
||||
enterprise.producers[i] = @enterprises_by_id[producer.id]
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
describe "Enterprises service", ->
|
||||
Enterprises = null
|
||||
enterprises = [
|
||||
{id: 1, type: "hub"},
|
||||
{id: 2, type: "producer"}
|
||||
{id: 1, type: "hub", producers: [{id: 2}]},
|
||||
{id: 2, type: "producer", hubs: [{id: 1}]},
|
||||
{id: 3, type: "producer", hubs: [{id: 1}]}
|
||||
]
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
@@ -20,3 +21,7 @@ describe "Enterprises service", ->
|
||||
|
||||
it "puts the same objects in enterprises and enterprises_by_id", ->
|
||||
expect(Enterprises.enterprises[0]).toBe Enterprises.enterprises_by_id["1"]
|
||||
|
||||
it "dereferences references to other enterprises", ->
|
||||
expect(Enterprises.enterprises_by_id["1"].producers[0]).toBe enterprises[1]
|
||||
expect(Enterprises.enterprises_by_id["3"].hubs[0]).toBe enterprises[0]
|
||||
|
||||
Reference in New Issue
Block a user