dawarich/app/models/place.rb

56 lines
1.1 KiB
Ruby
Raw Normal View History

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
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
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? }
validates :name, presence: true
validates :latitude, :longitude, presence: true
2025-11-16 18:23:48 -05:00
validates :lonlat, presence: true
2024-09-05 15:01:59 -04:00
enum :source, { manual: 0, photon: 1 }
2024-08-05 15:23:08 -04: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
def osm_id
2025-05-13 14:21:18 -04:00
geodata.dig('properties', 'osm_id')
end
def osm_key
2025-05-13 14:21:18 -04:00
geodata.dig('properties', 'osm_key')
end
def osm_value
2025-05-13 14:21:18 -04:00
geodata.dig('properties', 'osm_value')
end
def osm_type
2025-05-13 14:21:18 -04:00
geodata.dig('properties', 'osm_type')
end
private
def build_lonlat
self.lonlat = "POINT(#{longitude} #{latitude})"
end
2024-08-05 15:23:08 -04:00
end