diff --git a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee index b442773d31..84a3c2e7c2 100644 --- a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee +++ b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee @@ -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 diff --git a/app/assets/javascripts/darkswarm/services/taxons.js.coffee b/app/assets/javascripts/darkswarm/services/taxons.js.coffee new file mode 100644 index 0000000000..cbe6c118e0 --- /dev/null +++ b/app/assets/javascripts/darkswarm/services/taxons.js.coffee @@ -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 + diff --git a/app/helpers/shared_helper.rb b/app/helpers/shared_helper.rb index f555a9f695..ff4310a8f0 100644 --- a/app/helpers/shared_helper.rb +++ b/app/helpers/shared_helper.rb @@ -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 diff --git a/app/serializers/api/enterprise_serializer.rb b/app/serializers/api/enterprise_serializer.rb index c0b79084f0..c643bf5b21 100644 --- a/app/serializers/api/enterprise_serializer.rb +++ b/app/serializers/api/enterprise_serializer.rb @@ -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 diff --git a/app/views/layouts/darkswarm.html.haml b/app/views/layouts/darkswarm.html.haml index 3d246acab8..f831d93d17 100644 --- a/app/views/layouts/darkswarm.html.haml +++ b/app/views/layouts/darkswarm.html.haml @@ -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 diff --git a/spec/helpers/shared_helper_spec.rb b/spec/helpers/shared_helper_spec.rb index 76d2e6e859..d1d99c331f 100644 --- a/spec/helpers/shared_helper_spec.rb +++ b/spec/helpers/shared_helper_spec.rb @@ -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 diff --git a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee index ebf480d9e8..59d4ae9826 100644 --- a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee @@ -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] diff --git a/spec/javascripts/unit/darkswarm/services/taxon_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/taxon_spec.js.coffee new file mode 100644 index 0000000000..b05c82a13d --- /dev/null +++ b/spec/javascripts/unit/darkswarm/services/taxon_spec.js.coffee @@ -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]