From f28c39f8bf5ae90bb1c8a1e83f639a6b3f00aa16 Mon Sep 17 00:00:00 2001 From: starswan Date: Mon, 2 Dec 2024 12:15:24 +0000 Subject: [PATCH] in memory polygon support --- app/models/location_polygon.rb | 3 - app/models/subscription.rb | 8 +- .../finds_liverpool_as_well.yml | 86 ------------------- .../finds_basildon_and_st_albans.yml | 86 ------------------- .../finds_just_basildon.yml | 86 ------------------- spec/models/subscription_spec.rb | 2 +- 6 files changed, 4 insertions(+), 267 deletions(-) delete mode 100644 spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_large_radius/finds_liverpool_as_well.yml delete mode 100644 spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_medium_radius/finds_basildon_and_st_albans.yml delete mode 100644 spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_small_radius/finds_just_basildon.yml diff --git a/app/models/location_polygon.rb b/app/models/location_polygon.rb index 49a3b8e99f..f0f0a29621 100644 --- a/app/models/location_polygon.rb +++ b/app/models/location_polygon.rb @@ -5,9 +5,6 @@ class LocationPolygon < ApplicationRecord # Scope that expands any polygons returned by subsequent scopes by the provided radius # by overriding the `area` attribute with a buffered version of itself - # - # TODO: This is a "clever" temporary solution to allow us to generate expanded polygons - # to send to Algolia, and should be removed once we search through ActiveRecord. scope :buffered, ->(radius_in_miles) { select("*, ST_Buffer(area, #{convert_miles_to_metres(radius_in_miles || 0)}) AS area") } def self.with_name(location) diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 7769d9ab8f..6c4551e23a 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -87,13 +87,11 @@ def limit_by_location(vacancies, location, radius_in_miles) if query.blank? || LocationQuery::NATIONWIDE_LOCATIONS.include?(query) vacancies else - #TODO: add polygon support? 'within?' function for polygons doesn't support distance like ST_Within function does - radius_in_metres = convert_miles_to_metres radius_in_miles - - polygon = LocationPolygon.with_name(query) + polygon = LocationPolygon.buffered(radius_in_miles).with_name(query) if polygon.present? - vacancies.select { |v| v.organisations.map(&:geopoint).any? { |point| point.buffer(radius_in_metres).intersects?(polygon.area) } } + vacancies.select { |v| v.organisations.map(&:geopoint).any? { |point| polygon.area.contains?(point) } } else + radius_in_metres = convert_miles_to_metres radius_in_miles coordinates = Geocoding.new(query).coordinates search_point = RGeo::Geographic.spherical_factory.point(coordinates.second, coordinates.first) vacancies.select { |v| v.organisations.map(&:geopoint).any? { |point| search_point.distance(point) < radius_in_metres } } diff --git a/spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_large_radius/finds_liverpool_as_well.yml b/spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_large_radius/finds_liverpool_as_well.yml deleted file mode 100644 index 4de992289f..0000000000 --- a/spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_large_radius/finds_liverpool_as_well.yml +++ /dev/null @@ -1,86 +0,0 @@ ---- -http_interactions: -- request: - method: get - uri: https://maps.googleapis.com/maps/api/geocode/json?address=basildon&components=country:gb&key=&language=en&sensor=false - body: - encoding: US-ASCII - string: '' - headers: - User-Agent: - - Teaching Vacancies Service teaching.vacancies@education.gov.uk - Accept-Encoding: - - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 - Accept: - - "*/*" - response: - status: - code: 200 - message: OK - headers: - Content-Type: - - application/json; charset=UTF-8 - Date: - - Mon, 02 Dec 2024 09:19:10 GMT - Pragma: - - no-cache - Expires: - - Fri, 01 Jan 1990 00:00:00 GMT - Cache-Control: - - no-cache, must-revalidate - Access-Control-Allow-Origin: - - "*" - Content-Security-Policy-Report-Only: - - script-src 'none'; form-action 'none'; frame-src 'none'; report-uri https://csp.withgoogle.com/csp/scaffolding/msaifdggmnwc:229:0 - Cross-Origin-Opener-Policy-Report-Only: - - same-origin; report-to=coop_reporting - Report-To: - - '{"group":"coop_reporting","max_age":2592000,"endpoints":[{"url":"https://csp.withgoogle.com/csp/report-to/scaffolding/msaifdggmnwc:229:0"}],}' - Server: - - mafe - Content-Length: - - '2350' - X-Xss-Protection: - - '0' - X-Frame-Options: - - SAMEORIGIN - Server-Timing: - - gfet4t7; dur=51 - Alt-Svc: - - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 - body: - encoding: UTF-8 - string: "{\n \"results\" : \n [\n {\n \"address_components\" - : \n [\n {\n \"long_name\" : \"Basildon\",\n - \ \"short_name\" : \"Basildon\",\n \"types\" : - \n [\n \"locality\",\n \"political\"\n - \ ]\n },\n {\n \"long_name\" - : \"Basildon\",\n \"short_name\" : \"Basildon\",\n \"types\" - : \n [\n \"postal_town\"\n ]\n - \ },\n {\n \"long_name\" : \"Essex\",\n - \ \"short_name\" : \"Essex\",\n \"types\" : \n - \ [\n \"administrative_area_level_2\",\n \"political\"\n - \ ]\n },\n {\n \"long_name\" - : \"England\",\n \"short_name\" : \"England\",\n \"types\" - : \n [\n \"administrative_area_level_1\",\n - \ \"political\"\n ]\n },\n {\n - \ \"long_name\" : \"United Kingdom\",\n \"short_name\" - : \"GB\",\n \"types\" : \n [\n \"country\",\n - \ \"political\"\n ]\n }\n ],\n - \ \"formatted_address\" : \"Basildon, UK\",\n \"geometry\" - : \n {\n \"bounds\" : \n {\n \"northeast\" - : \n {\n \"lat\" : 51.5952192,\n \"lng\" - : 0.5250789\n },\n \"southwest\" : \n {\n - \ \"lat\" : 51.552998,\n \"lng\" : 0.4001419\n - \ }\n },\n \"location\" : \n {\n - \ \"lat\" : 51.57608399999999,\n \"lng\" : 0.488736\n - \ },\n \"location_type\" : \"APPROXIMATE\",\n \"viewport\" - : \n {\n \"northeast\" : \n {\n \"lat\" - : 51.5952192,\n \"lng\" : 0.5250789\n },\n - \ \"southwest\" : \n {\n \"lat\" - : 51.552998,\n \"lng\" : 0.4001419\n }\n }\n - \ },\n \"place_id\" : \"ChIJc8LrZATE2EcRidD0x-o6xFs\",\n \"types\" - : \n [\n \"locality\",\n \"political\"\n ]\n - \ }\n ],\n \"status\" : \"OK\"\n}" - recorded_at: Mon, 02 Dec 2024 09:19:10 GMT -recorded_with: VCR 6.3.1 diff --git a/spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_medium_radius/finds_basildon_and_st_albans.yml b/spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_medium_radius/finds_basildon_and_st_albans.yml deleted file mode 100644 index 5ada8307a3..0000000000 --- a/spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_medium_radius/finds_basildon_and_st_albans.yml +++ /dev/null @@ -1,86 +0,0 @@ ---- -http_interactions: -- request: - method: get - uri: https://maps.googleapis.com/maps/api/geocode/json?address=basildon&components=country:gb&key=&language=en&sensor=false - body: - encoding: US-ASCII - string: '' - headers: - User-Agent: - - Teaching Vacancies Service teaching.vacancies@education.gov.uk - Accept-Encoding: - - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 - Accept: - - "*/*" - response: - status: - code: 200 - message: OK - headers: - Content-Type: - - application/json; charset=UTF-8 - Date: - - Mon, 02 Dec 2024 09:19:10 GMT - Pragma: - - no-cache - Expires: - - Fri, 01 Jan 1990 00:00:00 GMT - Cache-Control: - - no-cache, must-revalidate - Access-Control-Allow-Origin: - - "*" - Content-Security-Policy-Report-Only: - - script-src 'none'; form-action 'none'; frame-src 'none'; report-uri https://csp.withgoogle.com/csp/scaffolding/msaifdggmnwc:229:0 - Cross-Origin-Opener-Policy-Report-Only: - - same-origin; report-to=coop_reporting - Report-To: - - '{"group":"coop_reporting","max_age":2592000,"endpoints":[{"url":"https://csp.withgoogle.com/csp/report-to/scaffolding/msaifdggmnwc:229:0"}],}' - Server: - - mafe - Content-Length: - - '2350' - X-Xss-Protection: - - '0' - X-Frame-Options: - - SAMEORIGIN - Server-Timing: - - gfet4t7; dur=54 - Alt-Svc: - - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 - body: - encoding: UTF-8 - string: "{\n \"results\" : \n [\n {\n \"address_components\" - : \n [\n {\n \"long_name\" : \"Basildon\",\n - \ \"short_name\" : \"Basildon\",\n \"types\" : - \n [\n \"locality\",\n \"political\"\n - \ ]\n },\n {\n \"long_name\" - : \"Basildon\",\n \"short_name\" : \"Basildon\",\n \"types\" - : \n [\n \"postal_town\"\n ]\n - \ },\n {\n \"long_name\" : \"Essex\",\n - \ \"short_name\" : \"Essex\",\n \"types\" : \n - \ [\n \"administrative_area_level_2\",\n \"political\"\n - \ ]\n },\n {\n \"long_name\" - : \"England\",\n \"short_name\" : \"England\",\n \"types\" - : \n [\n \"administrative_area_level_1\",\n - \ \"political\"\n ]\n },\n {\n - \ \"long_name\" : \"United Kingdom\",\n \"short_name\" - : \"GB\",\n \"types\" : \n [\n \"country\",\n - \ \"political\"\n ]\n }\n ],\n - \ \"formatted_address\" : \"Basildon, UK\",\n \"geometry\" - : \n {\n \"bounds\" : \n {\n \"northeast\" - : \n {\n \"lat\" : 51.5952192,\n \"lng\" - : 0.5250789\n },\n \"southwest\" : \n {\n - \ \"lat\" : 51.552998,\n \"lng\" : 0.4001419\n - \ }\n },\n \"location\" : \n {\n - \ \"lat\" : 51.57608399999999,\n \"lng\" : 0.488736\n - \ },\n \"location_type\" : \"APPROXIMATE\",\n \"viewport\" - : \n {\n \"northeast\" : \n {\n \"lat\" - : 51.5952192,\n \"lng\" : 0.5250789\n },\n - \ \"southwest\" : \n {\n \"lat\" - : 51.552998,\n \"lng\" : 0.4001419\n }\n }\n - \ },\n \"place_id\" : \"ChIJc8LrZATE2EcRidD0x-o6xFs\",\n \"types\" - : \n [\n \"locality\",\n \"political\"\n ]\n - \ }\n ],\n \"status\" : \"OK\"\n}" - recorded_at: Mon, 02 Dec 2024 09:19:10 GMT -recorded_with: VCR 6.3.1 diff --git a/spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_small_radius/finds_just_basildon.yml b/spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_small_radius/finds_just_basildon.yml deleted file mode 100644 index 873089336d..0000000000 --- a/spec/fixtures/vcr/Subscription/_vacancies_matching/with_vacancies/with_location/with_a_polygon_Basildon_/with_a_small_radius/finds_just_basildon.yml +++ /dev/null @@ -1,86 +0,0 @@ ---- -http_interactions: -- request: - method: get - uri: https://maps.googleapis.com/maps/api/geocode/json?address=basildon&components=country:gb&key=&language=en&sensor=false - body: - encoding: US-ASCII - string: '' - headers: - User-Agent: - - Teaching Vacancies Service teaching.vacancies@education.gov.uk - Accept-Encoding: - - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 - Accept: - - "*/*" - response: - status: - code: 200 - message: OK - headers: - Content-Type: - - application/json; charset=UTF-8 - Date: - - Mon, 02 Dec 2024 09:19:09 GMT - Pragma: - - no-cache - Expires: - - Fri, 01 Jan 1990 00:00:00 GMT - Cache-Control: - - no-cache, must-revalidate - Access-Control-Allow-Origin: - - "*" - Content-Security-Policy-Report-Only: - - script-src 'none'; form-action 'none'; frame-src 'none'; report-uri https://csp.withgoogle.com/csp/scaffolding/msaifdggmnwc:229:0 - Cross-Origin-Opener-Policy-Report-Only: - - same-origin; report-to=coop_reporting - Report-To: - - '{"group":"coop_reporting","max_age":2592000,"endpoints":[{"url":"https://csp.withgoogle.com/csp/report-to/scaffolding/msaifdggmnwc:229:0"}],}' - Server: - - mafe - Content-Length: - - '2350' - X-Xss-Protection: - - '0' - X-Frame-Options: - - SAMEORIGIN - Server-Timing: - - gfet4t7; dur=71 - Alt-Svc: - - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 - body: - encoding: UTF-8 - string: "{\n \"results\" : \n [\n {\n \"address_components\" - : \n [\n {\n \"long_name\" : \"Basildon\",\n - \ \"short_name\" : \"Basildon\",\n \"types\" : - \n [\n \"locality\",\n \"political\"\n - \ ]\n },\n {\n \"long_name\" - : \"Basildon\",\n \"short_name\" : \"Basildon\",\n \"types\" - : \n [\n \"postal_town\"\n ]\n - \ },\n {\n \"long_name\" : \"Essex\",\n - \ \"short_name\" : \"Essex\",\n \"types\" : \n - \ [\n \"administrative_area_level_2\",\n \"political\"\n - \ ]\n },\n {\n \"long_name\" - : \"England\",\n \"short_name\" : \"England\",\n \"types\" - : \n [\n \"administrative_area_level_1\",\n - \ \"political\"\n ]\n },\n {\n - \ \"long_name\" : \"United Kingdom\",\n \"short_name\" - : \"GB\",\n \"types\" : \n [\n \"country\",\n - \ \"political\"\n ]\n }\n ],\n - \ \"formatted_address\" : \"Basildon, UK\",\n \"geometry\" - : \n {\n \"bounds\" : \n {\n \"northeast\" - : \n {\n \"lat\" : 51.5952192,\n \"lng\" - : 0.5250789\n },\n \"southwest\" : \n {\n - \ \"lat\" : 51.552998,\n \"lng\" : 0.4001419\n - \ }\n },\n \"location\" : \n {\n - \ \"lat\" : 51.57608399999999,\n \"lng\" : 0.488736\n - \ },\n \"location_type\" : \"APPROXIMATE\",\n \"viewport\" - : \n {\n \"northeast\" : \n {\n \"lat\" - : 51.5952192,\n \"lng\" : 0.5250789\n },\n - \ \"southwest\" : \n {\n \"lat\" - : 51.552998,\n \"lng\" : 0.4001419\n }\n }\n - \ },\n \"place_id\" : \"ChIJc8LrZATE2EcRidD0x-o6xFs\",\n \"types\" - : \n [\n \"locality\",\n \"political\"\n ]\n - \ }\n ],\n \"status\" : \"OK\"\n}" - recorded_at: Mon, 02 Dec 2024 09:19:09 GMT -recorded_with: VCR 6.3.1 diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb index 841bdc2990..ed0e0a138e 100644 --- a/spec/models/subscription_spec.rb +++ b/spec/models/subscription_spec.rb @@ -189,7 +189,7 @@ end end - context "with a polygon (Basildon)", :geocode, :vcr do + context "with a polygon (Basildon)" do let(:subscription) { create(:daily_subscription, location: "Basildon", radius: radius) } context "with a small radius" do