dawarich/app/models/trip.rb

55 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class Trip < ApplicationRecord
has_rich_text :notes
2024-11-28 06:00:54 -05:00
belongs_to :user
validates :name, :started_at, :ended_at, presence: true
before_save :calculate_distance
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
2024-11-28 04:40:08 -05:00
def photo_previews
@photo_previews ||= select_dominant_orientation(photos).sample(12)
2024-12-10 12:49:37 -05:00
end
2024-11-28 04:40:08 -05:00
def photo_sources
@photo_sources ||= photos.map { _1[:source] }.uniq
2024-11-28 04:40:08 -05:00
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' }
vertical_photos.count > horizontal_photos.count ? vertical_photos : horizontal_photos
end
def calculate_distance
distance = 0
points.each_cons(2) do |point1, point2|
distance_between = Geocoder::Calculations.distance_between(
point1.to_coordinates, point2.to_coordinates, units: ::DISTANCE_UNIT
)
distance += distance_between
end
self.distance = distance.round
end
end