2024-07-21 14:32:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class Visit < ApplicationRecord
|
2024-08-12 16:18:11 -04:00
|
|
|
belongs_to :area, optional: true
|
|
|
|
|
belongs_to :place, optional: true
|
2024-07-21 14:32:29 -04:00
|
|
|
belongs_to :user
|
2024-07-22 18:40:48 -04:00
|
|
|
has_many :points, dependent: :nullify
|
2024-08-12 16:18:11 -04:00
|
|
|
has_many :place_visits, dependent: :destroy
|
|
|
|
|
has_many :suggested_places, through: :place_visits, source: :place
|
2024-07-24 14:25:16 -04:00
|
|
|
|
|
|
|
|
validates :started_at, :ended_at, :duration, :name, :status, presence: true
|
|
|
|
|
|
2024-09-05 15:01:59 -04:00
|
|
|
enum :status, { suggested: 0, confirmed: 1, declined: 2 }
|
|
|
|
|
|
|
|
|
|
def reverse_geocoded?
|
|
|
|
|
place.geodata.present?
|
|
|
|
|
end
|
2024-08-05 15:23:08 -04:00
|
|
|
|
|
|
|
|
def coordinates
|
|
|
|
|
points.pluck(:latitude, :longitude).map { [_1[0].to_f, _1[1].to_f] }
|
|
|
|
|
end
|
2024-08-12 16:18:11 -04:00
|
|
|
|
|
|
|
|
def default_name
|
|
|
|
|
name || area&.name || place&.name
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# in meters
|
|
|
|
|
def default_radius
|
|
|
|
|
return area&.radius if area.present?
|
|
|
|
|
|
2024-12-10 13:43:52 -05:00
|
|
|
radius = points.map do |point|
|
2024-12-10 17:10:36 -05:00
|
|
|
Geocoder::Calculations.distance_between(center, [point.latitude, point.longitude])
|
2024-12-10 13:43:52 -05:00
|
|
|
end.max
|
2024-08-12 16:18:11 -04:00
|
|
|
|
2024-08-13 12:25:48 -04:00
|
|
|
radius && radius >= 15 ? radius : 15
|
2024-08-12 16:18:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def center
|
2024-08-25 14:48:00 -04:00
|
|
|
area.present? ? area.to_coordinates : place.to_coordinates
|
2024-08-12 16:18:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def async_reverse_geocode
|
|
|
|
|
return unless REVERSE_GEOCODING_ENABLED
|
|
|
|
|
return if place.blank?
|
|
|
|
|
|
|
|
|
|
ReverseGeocodingJob.perform_later('place', place_id)
|
|
|
|
|
end
|
2024-07-21 14:32:29 -04:00
|
|
|
end
|