dawarich/app/models/point.rb

60 lines
1.5 KiB
Ruby
Raw Normal View History

2024-05-23 14:12:23 -04:00
# frozen_string_literal: true
2024-03-15 18:27:31 -04:00
class Point < ApplicationRecord
2024-07-12 15:59:03 -04:00
reverse_geocoded_by :latitude, :longitude
2024-08-22 16:40:27 -04:00
belongs_to :import, optional: true, counter_cache: true
2024-07-21 14:32:29 -04:00
belongs_to :visit, optional: true
2024-07-12 15:59:03 -04:00
belongs_to :user
2024-03-15 18:27:31 -04:00
2024-04-06 13:09:38 -04:00
validates :latitude, :longitude, :timestamp, presence: true
2024-03-15 18:27:31 -04:00
2024-09-05 15:01:59 -04:00
enum :battery_status, { unknown: 0, unplugged: 1, charging: 2, full: 3 }, suffix: true
enum :trigger, {
2024-03-15 18:27:31 -04:00
unknown: 0, background_event: 1, circular_region_event: 2, beacon_event: 3,
report_location_message_event: 4, manual_event: 5, timer_based_event: 6,
settings_monitoring_event: 7
2024-09-05 15:01:59 -04:00
}, suffix: true
enum :connection, { mobile: 0, wifi: 1, offline: 2, unknown: 4 }, suffix: true
2024-03-15 18:27:31 -04:00
2024-08-20 14:14:17 -04:00
scope :reverse_geocoded, -> { where.not(geodata: {}) }
scope :not_reverse_geocoded, -> { where(geodata: {}) }
scope :visited, -> { where.not(visit_id: nil) }
scope :not_visited, -> { where(visit_id: nil) }
2024-03-15 18:27:31 -04:00
after_create :async_reverse_geocode
after_create_commit :broadcast_coordinates
2024-03-15 18:27:31 -04:00
2024-05-23 14:12:23 -04:00
def self.without_raw_data
select(column_names - ['raw_data'])
end
def recorded_at
Time.zone.at(timestamp)
end
2024-03-15 18:27:31 -04:00
def async_reverse_geocode
return unless REVERSE_GEOCODING_ENABLED
2024-08-05 15:23:08 -04:00
ReverseGeocodingJob.perform_later(self.class.to_s, id)
2024-03-15 18:27:31 -04:00
end
private
def broadcast_coordinates
PointsChannel.broadcast_to(
user,
[
latitude.to_f,
longitude.to_f,
battery.to_s,
altitude.to_s,
timestamp.to_s,
velocity.to_s,
id.to_s,
country.to_s
]
)
end
2024-03-15 18:27:31 -04:00
end