2024-08-05 15:23:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class Place < ApplicationRecord
|
2025-03-03 14:50:49 -05:00
|
|
|
include Nearable
|
|
|
|
|
include Distanceable
|
2025-11-16 09:01:54 -05:00
|
|
|
include Taggable
|
2025-03-03 14:50:49 -05:00
|
|
|
|
2024-08-12 16:18:11 -04:00
|
|
|
DEFAULT_NAME = 'Suggested place'
|
2024-08-05 15:23:08 -04:00
|
|
|
|
2025-11-16 09:01:54 -05:00
|
|
|
belongs_to :user, optional: true # Optional during migration period
|
2024-08-12 16:18:11 -04:00
|
|
|
has_many :visits, dependent: :destroy
|
|
|
|
|
has_many :place_visits, dependent: :destroy
|
2025-03-03 14:50:49 -05:00
|
|
|
has_many :suggested_visits, -> { distinct }, through: :place_visits, source: :visit
|
2024-08-05 15:23:08 -04:00
|
|
|
|
2025-11-16 18:23:48 -05:00
|
|
|
before_validation :build_lonlat, if: -> { latitude.present? && longitude.present? }
|
|
|
|
|
|
2025-11-16 11:28:40 -05:00
|
|
|
validates :name, presence: true
|
|
|
|
|
validates :latitude, :longitude, presence: true
|
2025-11-16 18:23:48 -05:00
|
|
|
validates :lonlat, presence: true
|
2025-11-16 09:01:54 -05:00
|
|
|
|
2024-09-05 15:01:59 -04:00
|
|
|
enum :source, { manual: 0, photon: 1 }
|
2024-08-05 15:23:08 -04:00
|
|
|
|
2025-11-16 09:01:54 -05:00
|
|
|
scope :for_user, ->(user) { where(user: user) }
|
|
|
|
|
scope :ordered, -> { order(:name) }
|
|
|
|
|
|
2025-03-03 14:50:49 -05:00
|
|
|
def lon
|
|
|
|
|
lonlat.x
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def lat
|
|
|
|
|
lonlat.y
|
|
|
|
|
end
|
|
|
|
|
|
2025-03-08 13:40:28 -05:00
|
|
|
def osm_id
|
2025-05-13 14:21:18 -04:00
|
|
|
geodata.dig('properties', 'osm_id')
|
2025-03-08 13:40:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def osm_key
|
2025-05-13 14:21:18 -04:00
|
|
|
geodata.dig('properties', 'osm_key')
|
2025-03-08 13:40:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def osm_value
|
2025-05-13 14:21:18 -04:00
|
|
|
geodata.dig('properties', 'osm_value')
|
2025-03-08 13:40:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def osm_type
|
2025-05-13 14:21:18 -04:00
|
|
|
geodata.dig('properties', 'osm_type')
|
2025-03-08 13:40:28 -05:00
|
|
|
end
|
2025-11-16 11:28:40 -05:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def build_lonlat
|
|
|
|
|
self.lonlat = "POINT(#{longitude} #{latitude})"
|
|
|
|
|
end
|
2024-08-05 15:23:08 -04:00
|
|
|
end
|