From 7f2508eeaa7426fcf99d3568e6a4255b2609827a Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 2 Jul 2015 15:04:16 +1000 Subject: [PATCH] When a name match is found, calculate distances from that enterprise, rather than from the geocoded lookup --- .../enterprises_controller.js.coffee | 3 +- .../darkswarm/services/enterprises.js.coffee | 27 +++++++++----- .../services/enterprise_spec.js.coffee | 37 ++++++++++++++----- 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/enterprises_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/enterprises_controller.js.coffee index a23d4b5517..7514d98705 100644 --- a/app/assets/javascripts/darkswarm/controllers/enterprises_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/enterprises_controller.js.coffee @@ -12,8 +12,7 @@ Darkswarm.controller "EnterprisesCtrl", ($scope, Enterprises, Search, $document, $scope.$watch "query", (query)-> - Enterprises.flagMatching query - Enterprises.updateDistance query + Enterprises.evaluateQuery query Search.search query $rootScope.$on "$locationChangeSuccess", (newRoute, oldRoute) -> diff --git a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee index daf121ed5a..1bba3880f0 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, Matcher, Geo, $rootScope)-> +Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter, Matcher, Geo)-> new class Enterprises enterprises_by_id: {} constructor: -> @@ -28,6 +28,10 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, Dereferencer.dereference enterprise.taxons, Taxons.taxons_by_id Dereferencer.dereference enterprise.supplied_taxons, Taxons.taxons_by_id + evaluateQuery: (query) -> + @flagMatching query + @calculateDistance query + flagMatching: (query) -> for enterprise in @enterprises enterprise.matches_name_query = if query? && query.length > 0 @@ -35,25 +39,30 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, else false - updateDistance: (query) -> + firstMatching: -> + (enterprise for enterprise in @enterprises when enterprise.matches_name_query)[0] + + calculateDistance: (query) -> if query?.length > 0 - @calculateDistance(query) + if @firstMatching()? + @setDistanceFrom @firstMatching() + else + @calculateDistanceGeo(query) else @resetDistance() - calculateDistance: (query) -> + calculateDistanceGeo: (query) -> Geo.geocode query, (results, status) => if status == Geo.OK - console.log "Geocoded #{query} -> #{results[0].geometry.location}." + #console.log "Geocoded #{query} -> #{results[0].geometry.location}." @setDistanceFrom results[0].geometry.location else console.log "Geocoding failed for the following reason: #{status}" @resetDistance() - setDistanceFrom: (location) -> - $rootScope.$apply => - for enterprise in @enterprises - enterprise.distance = Geo.distanceBetween enterprise, location + setDistanceFrom: (locatable) -> + for enterprise in @enterprises + enterprise.distance = Geo.distanceBetween enterprise, locatable resetDistance: -> enterprise.distance = null for enterprise in @enterprises diff --git a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee index 07c4bdffff..f9b866518c 100644 --- a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee @@ -101,35 +101,52 @@ describe "Enterprises service", -> 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") - Enterprises.updateDistance "asdf" - expect(Enterprises.calculateDistance).toHaveBeenCalledWith("asdf") + describe "finding the first enterprise matched by a query", -> + it "returns the enterprise when one exists", -> + enterprises[0].matches_name_query = false + enterprises[1].matches_name_query = true + expect(Enterprises.firstMatching()).toEqual enterprises[1] + + it "returns undefined otherwise", -> + e.matches_name_query = false for e in enterprises + expect(Enterprises.firstMatching()).toBeUndefined() + + describe "calculating the distance of enterprises from a location", -> + describe "when a query is provided", -> + it "sets the distance from the enterprise when a name match is available", -> + spyOn(Enterprises, "firstMatching").andReturn('match') + spyOn(Enterprises, "setDistanceFrom") + Enterprises.calculateDistance "asdf" + expect(Enterprises.setDistanceFrom).toHaveBeenCalledWith('match') + + it "calculates the distance from the geocoded query otherwise", -> + spyOn(Enterprises, "calculateDistanceGeo") + Enterprises.calculateDistance "asdf" + expect(Enterprises.calculateDistanceGeo).toHaveBeenCalledWith("asdf") it "resets the distance when query is null", -> spyOn(Enterprises, "resetDistance") - Enterprises.updateDistance null + Enterprises.calculateDistance null expect(Enterprises.resetDistance).toHaveBeenCalled() it "resets the distance when query is blank", -> spyOn(Enterprises, "resetDistance") - Enterprises.updateDistance "" + Enterprises.calculateDistance "" expect(Enterprises.resetDistance).toHaveBeenCalled() - describe "calculating the distance of enterprises from a location", -> + describe "calculating the distance of enterprises from a location by geocoding", -> beforeEach -> spyOn(Enterprises, "setDistanceFrom") it "calculates distance for all enterprises when geocoding succeeds", -> Geo.succeed = true - Enterprises.calculateDistance('query') + Enterprises.calculateDistanceGeo('query') expect(Enterprises.setDistanceFrom).toHaveBeenCalledWith("location") it "resets distance when geocoding fails", -> Geo.succeed = false spyOn(Enterprises, "resetDistance") - Enterprises.calculateDistance('query') + Enterprises.calculateDistanceGeo('query') expect(Enterprises.setDistanceFrom).not.toHaveBeenCalled() expect(Enterprises.resetDistance).toHaveBeenCalled()