diff --git a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee index 89dafa837d..4a306f807e 100644 --- a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee +++ b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee @@ -1,4 +1,4 @@ -Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter, Geo)-> +Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter, Matcher, Geo)-> new class Enterprises enterprises_by_id: {} constructor: -> @@ -28,6 +28,13 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, Dereferencer.dereference enterprise.taxons, Taxons.taxons_by_id Dereferencer.dereference enterprise.supplied_taxons, Taxons.taxons_by_id + flagMatching: (query) -> + for enterprise in @enterprises + enterprise.matches_name_query = if query? && query.length > 0 + Matcher.match([enterprise.name], query) + else + false + updateDistance: (query) -> if query?.length > 0 @calculateDistance(query) diff --git a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee index b424511e09..07c4bdffff 100644 --- a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee @@ -17,14 +17,14 @@ describe "Enterprises service", -> {id: 1, name: "test"} ] enterprises = [ - {id: 1, visible: true, category: "hub", producers: [{id: 5}], taxons: [{id: 1}]}, - {id: 2, visible: true, category: "hub", producers: [{id: 6}]} - {id: 3, visible: true, category: "hub_profile"} - {id: 4, visible: false, category: "hub", producers: [{id: 7}]} - {id: 5, visible: true, category: "producer_hub", hubs: [{id: 1}]}, - {id: 6, visible: true, category: "producer_shop", hubs: [{id: 2}]}, - {id: 7, visible: true, category: "producer", hubs: [{id: 2}]} - {id: 8, visible: false, category: "producer", hubs: [{id: 2}]} + {id: 1, visible: true, name: 'a', category: "hub", producers: [{id: 5}], taxons: [{id: 1}]}, + {id: 2, visible: true, name: 'b', category: "hub", producers: [{id: 6}]} + {id: 3, visible: true, name: 'c', category: "hub_profile"} + {id: 4, visible: false,name: 'd', category: "hub", producers: [{id: 7}]} + {id: 5, visible: true, name: 'e', category: "producer_hub", hubs: [{id: 1}]}, + {id: 6, visible: true, name: 'f', category: "producer_shop", hubs: [{id: 2}]}, + {id: 7, visible: true, name: 'g', category: "producer", hubs: [{id: 2}]} + {id: 8, visible: false,name: 'h', category: "producer", hubs: [{id: 2}]} ] H1: 0 beforeEach -> @@ -87,6 +87,20 @@ describe "Enterprises service", -> expect(Enterprises.producers).toContain Enterprises.enterprises[5] expect(Enterprises.producers).toContain Enterprises.enterprises[6] + describe "flagging enterprises with names matching a query", -> + it "flags enterprises when a query is provided", -> + Enterprises.flagMatching 'c' + expect(e.matches_name_query).toBe true for e in enterprises when e.name == 'c' + expect(e.matches_name_query).toBe false for e in enterprises when e.name != 'c' + + it "clears flags when query is null", -> + Enterprises.flagMatching null + expect(e.matches_name_query).toBe false for e in enterprises + + it "clears flags when query is blank", -> + Enterprises.flagMatching '' + expect(e.matches_name_query).toBe false for e in enterprises + describe "updating distance of enterprises from a location", -> it "calculates the distance when a query is provided", -> spyOn(Enterprises, "calculateDistance")