Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore 'very invalid' location polygons #7398

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,41 +75,49 @@ def vacancies_matching(default_scope)
scope = default_scope
# ignore legacy sorting criteria - legacy job_title is too specific and will typically filter everything
criteria = search_criteria.symbolize_keys.except(:jobs_sort, :job_title)
scope, criteria = handle_location(scope, criteria)
scope, criteria = self.class.handle_location(scope, criteria)

scope.select do |vacancy|
criteria.all? { |criterion, value| FILTERS.fetch(criterion).call(vacancy, value) }
end
end

private

extend DistanceHelper

# These polygons seem to be extremely invalid - they respond to the 'invalid_reason' call by throwing an exception,
# as opposed to the other 31 invalid ones in the production database, which are just 'invalid'
INVALID_POLYGONS = ["somerset, bath and bristol",
"devon, plymouth and torbay",
"essex, southend and thurrock",
"leicestershire and rutland",
"lincolnshire and lincoln",
"derbyshire and derby"].freeze

class << self
def limit_by_location(vacancies, location, radius_in_miles)
if location.blank? || LocationQuery::NATIONWIDE_LOCATIONS.include?(location)
vacancies
polygon_area = LocationPolygon.buffered(radius_in_miles).with_name(location)&.area
if !location.in?(INVALID_POLYGONS) && polygon_area.present? && polygon_area.invalid_reason.nil?
vacancies.select { |v| v.organisations.map(&:geopoint).any? { |point| polygon_area.contains?(point) } }
else
polygon_area = LocationPolygon.buffered(radius_in_miles).with_name(location)&.area
if polygon_area.present? && polygon_area.invalid_reason.nil?
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(location).coordinates
search_point = RGeo::Geographic.spherical_factory(srid: 4326).point(coordinates.second, coordinates.first)
vacancies.select { |v| v.organisations.map(&:geopoint).any? { |point| search_point.distance(point) < radius_in_metres } }
end
radius_in_metres = convert_miles_to_metres radius_in_miles
coordinates = Geocoding.new(location).coordinates
search_point = RGeo::Geographic.spherical_factory(srid: 4326).point(coordinates.second, coordinates.first)
vacancies.select { |v| v.organisations.map(&:geopoint).any? { |point| search_point.distance(point) < radius_in_metres } }
end
end
end

def handle_location(scope, criteria)
if criteria.key?(:location)
[self.class.limit_by_location(scope, criteria[:location].strip.downcase, criteria[:radius] || 10), criteria.except(:location, :radius)]

else
[scope, criteria]
def handle_location(scope, criteria)
if criteria.key?(:location)
location = criteria[:location].strip.downcase
if location.blank? || LocationQuery::NATIONWIDE_LOCATIONS.include?(location)
[scope, criteria.except(:location, :radius)]
else
[limit_by_location(scope, location, criteria[:radius] || 10), criteria.except(:location, :radius)]
end
else
# ignore 'radius' keys that don't have a 'location' key
[scope, criteria.except(:radius)]
end
end
end
end
Loading