dawarich/app/models/trip.rb
2025-01-24 15:58:44 +01:00

59 lines
1.4 KiB
Ruby

# frozen_string_literal: true
class Trip < ApplicationRecord
has_rich_text :notes
belongs_to :user
validates :name, :started_at, :ended_at, presence: true
before_save :create_path!
def create_path!
trip_path = Tracks::BuildPath.new(points.pluck(:latitude, :longitude)).call
self.distance = calculate_distance
self.path = trip_path
end
def points
user.tracked_points.where(timestamp: started_at.to_i..ended_at.to_i).order(:timestamp)
end
def countries
points.pluck(:country).uniq.compact
end
def photo_previews
@photo_previews ||= select_dominant_orientation(photos).sample(12)
end
def photo_sources
@photo_sources ||= photos.map { _1[:source] }.uniq
end
private
def photos
@photos ||= Trips::Photos.new(self, user).call
end
def select_dominant_orientation(photos)
vertical_photos = photos.select { |photo| photo[:orientation] == 'portrait' }
horizontal_photos = photos.select { |photo| photo[:orientation] == 'landscape' }
# this is ridiculous, but I couldn't find my way around frontend
# to show all photos in the same height
vertical_photos.count > horizontal_photos.count ? vertical_photos : horizontal_photos
end
def calculate_distance
distance = 0
points.each_cons(2) do |point1, point2|
distance += DistanceCalculator.new(point1, point2).call
end
self.distance = distance.round
end
end