Skip to content

Commit

Permalink
in memory polygon support
Browse files Browse the repository at this point in the history
  • Loading branch information
starswan committed Dec 4, 2024
1 parent cc8ce5b commit f28c39f
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 267 deletions.
3 changes: 0 additions & 3 deletions app/models/location_polygon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion spec/models/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f28c39f

Please sign in to comment.