Adding dereferencing to taxons and reducing our data-set

This commit is contained in:
Will Marshall
2014-07-02 12:30:28 +10:00
committed by Rohan Mitchell
parent 4a116570d1
commit 24d3abf6d5
8 changed files with 52 additions and 6 deletions

View File

@@ -1,15 +1,20 @@
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Dereferencer)->
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer)->
new class Enterprises
enterprises_by_id: {} # id/object pairs for lookup
constructor: ->
@enterprises = enterprises
for enterprise in enterprises
@enterprises_by_id[enterprise.id] = enterprise
@dereference()
@dereferenceEnterprises()
@dereferenceTaxons()
dereference: ->
dereferenceEnterprises: ->
if CurrentHub.hub?.id
CurrentHub.hub = @enterprises_by_id[CurrentHub.hub.id]
for enterprise in @enterprises
Dereferencer.dereference enterprise.hubs, @enterprises_by_id
Dereferencer.dereference enterprise.producers, @enterprises_by_id
dereferenceTaxons: ->
for enterprise in @enterprises
Dereferencer.dereference enterprise.taxons, Taxons.taxons_by_id

View File

@@ -0,0 +1,8 @@
Darkswarm.factory "Taxons", (taxons)->
new class Taxons
taxons: taxons
taxons_by_id: {}
constructor: ->
for taxon in @taxons
@taxons_by_id[taxon.id] = taxon

View File

@@ -3,6 +3,10 @@ module SharedHelper
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

View File

@@ -55,7 +55,7 @@ class Api::CachedEnterpriseSerializer < ActiveModel::Serializer
:pickup, :delivery
has_many :distributed_taxons, key: :taxons, serializer: Api::TaxonSerializer
has_many :supplied_taxons, serializer: Api::TaxonSerializer
has_many :supplied_taxons, serializer: Api::IdSerializer
has_many :distributors, key: :hubs, serializer: Api::IdSerializer
has_many :suppliers, key: :producers, serializer: Api::IdSerializer

View File

@@ -28,6 +28,7 @@
= inject_json "currentOrder", "current_order"
= inject_json "user", "current_user"
= inject_json "railsFlash", "flash"
= inject_taxons
.off-canvas-wrap{offcanvas: true}
.inner-wrap

View File

@@ -32,9 +32,14 @@ describe SharedHelper do
end
it "injects enterprises" do
Enterprise.stub(:visible).and_return [enterprise]
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
end
end

View File

@@ -1,8 +1,11 @@
describe "Enterprises service", ->
Enterprises = null
CurrentHubMock = {}
taxons = [
{id: 1, name: "test"}
]
enterprises = [
{id: 1, type: "hub", producers: [{id: 2}]},
{id: 1, type: "hub", producers: [{id: 2}], taxons: [{id: 1}]},
{id: 2, type: "producer", hubs: [{id: 1}]},
{id: 3, type: "producer", hubs: [{id: 1}]}
]
@@ -12,6 +15,7 @@ describe "Enterprises service", ->
$provide.value "CurrentHub", CurrentHubMock
null
angular.module('Darkswarm').value('enterprises', enterprises)
angular.module('Darkswarm').value('taxons', taxons)
inject ($injector)->
Enterprises = $injector.get("Enterprises")
@@ -29,3 +33,6 @@ describe "Enterprises service", ->
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]
it "dereferences taxons", ->
expect(Enterprises.enterprises[0].taxons[0]).toBe taxons[0]

View File

@@ -0,0 +1,16 @@
describe "Taxons service", ->
Taxons = null
taxons = [
{id: 1, name: "test"}
{id: 2, name: "Roger"}
]
beforeEach ->
module('Darkswarm')
angular.module('Darkswarm').value 'taxons', taxons
inject ($injector)->
Taxons = $injector.get("Taxons")
it "caches taxons in an id-referenced hash", ->
expect(Taxons.taxons_by_id[1]).toBe taxons[0]