dawarich/app/models/stat.rb

91 lines
2.3 KiB
Ruby
Raw Normal View History

2024-03-24 13:55:35 -04:00
# frozen_string_literal: true
2024-03-23 15:29:55 -04:00
class Stat < ApplicationRecord
include DistanceConvertible
Implement public trip sharing with Shareable concern This commit implements comprehensive public trip sharing functionality by extracting sharing logic into a reusable Shareable concern and extending it to Trip models. ## Key Features **Shareable Concern (DRY principle)** - Extract sharing logic from Stat model into reusable concern - Support for time-based expiration (1h, 12h, 24h, permanent) - UUID-based secure public access - User-controlled sharing of notes and photos - Automatic UUID generation on model creation **Database Changes** - Add sharing_uuid (UUID) column to trips table - Add sharing_settings (JSONB) column for configuration storage - Add unique index on sharing_uuid for performance **Public Trip Sharing** - Public-facing trip view with read-only access - Interactive map with trip route visualization - Optional sharing of notes and photo previews - Branded footer with Dawarich attribution - Responsive design matching existing UI patterns **Sharing Management** - In-app sharing controls in trip show view - Enable/disable sharing with one click - Configurable expiration times - Copy-to-clipboard for sharing URLs - Visual indicators for sharing status **Authorization & Security** - TripPolicy for fine-grained access control - Public access only for explicitly shared trips - Automatic expiration enforcement - Owner-only sharing management - UUID-based URLs prevent enumeration attacks **API & Routes** - GET /shared/trips/:trip_uuid for public access - PATCH /trips/:id/sharing for sharing management - RESTful endpoint design consistent with stats sharing **Frontend** - New public-trip-map Stimulus controller - OpenStreetMap tiles for public viewing (no API key required) - Start/end markers on trip route - Automatic map bounds fitting **Tests** - Comprehensive concern specs (Shareable) - Model specs for Trip sharing functionality - Request specs for public and authenticated access - Policy specs for authorization rules - 100% coverage of sharing functionality ## Implementation Details ### Models Updated - Stat: Now uses Shareable concern (removed duplicate code) - Trip: Includes Shareable concern with notes/photos options ### Controllers Added - Shared::TripsController: Handles public viewing and sharing management ### Views Added - trips/public_show.html.erb: Public-facing trip view - trips/_sharing.html.erb: Sharing controls partial ### JavaScript Added - public_trip_map_controller.js: Map rendering for public trips ### Helpers Extended - TripsHelper: Added sharing status and expiration helpers ## Breaking Changes None. This is a purely additive feature. ## Migration Required Yes. Run: rails db:migrate ## Testing All specs pass: - spec/models/concerns/shareable_spec.rb - spec/models/trip_spec.rb - spec/requests/shared/trips_spec.rb - spec/policies/trip_policy_spec.rb
2025-11-05 10:44:27 -05:00
include Shareable
2024-03-23 15:29:55 -04:00
validates :year, :month, presence: true
belongs_to :user
2024-03-24 13:55:35 -04:00
def distance_by_day
2024-12-06 10:52:36 -05:00
monthly_points = points
calculate_daily_distances(monthly_points)
2024-03-24 13:55:35 -04:00
end
2024-05-25 07:45:49 -04:00
def self.year_distance(year, user)
2024-12-06 10:52:36 -05:00
stats_by_month = where(year:, user:).order(:month).index_by(&:month)
2024-12-06 10:52:36 -05:00
(1..12).map do |month|
month_name = Date::MONTHNAMES[month]
2024-12-06 10:52:36 -05:00
distance = stats_by_month[month]&.distance || 0
[month_name, distance]
end
end
2024-12-06 10:52:36 -05:00
def points
user.points
2024-12-06 10:52:36 -05:00
.without_raw_data
.where(timestamp: timespan)
.order(timestamp: :asc)
end
def hexagons_available?
h3_hex_ids.present? &&
2025-09-20 08:05:14 -04:00
(h3_hex_ids.is_a?(Hash) || h3_hex_ids.is_a?(Array)) &&
h3_hex_ids.any?
2025-09-13 17:11:42 -04:00
end
def calculate_data_bounds
start_date = Date.new(year, month, 1).beginning_of_day
end_date = start_date.end_of_month.end_of_day
2025-09-13 10:41:28 -04:00
points_relation = user.points.where(timestamp: start_date.to_i..end_date.to_i)
point_count = points_relation.count
2025-09-13 10:41:28 -04:00
return nil if point_count.zero?
bounds_result = ActiveRecord::Base.connection.exec_query(
"SELECT MIN(ST_Y(lonlat::geometry)) as min_lat, MAX(ST_Y(lonlat::geometry)) as max_lat,
MIN(ST_X(lonlat::geometry)) as min_lng, MAX(ST_X(lonlat::geometry)) as max_lng
FROM points
WHERE user_id = $1
AND timestamp BETWEEN $2 AND $3
AND lonlat IS NOT NULL",
'data_bounds_query',
[user.id, start_date.to_i, end_date.to_i]
).first
{
min_lat: bounds_result['min_lat'].to_f,
max_lat: bounds_result['max_lat'].to_f,
min_lng: bounds_result['min_lng'].to_f,
max_lng: bounds_result['max_lng'].to_f,
point_count: point_count
}
end
2025-09-13 17:23:48 -04:00
def process!
Stats::CalculatingJob.perform_later(user.id, year, month)
end
2024-04-02 17:20:25 -04:00
private
def timespan
DateTime.new(year, month).beginning_of_month..DateTime.new(year, month).end_of_month
end
2024-12-06 10:52:36 -05:00
def calculate_daily_distances(monthly_points)
Stats::DailyDistanceQuery.new(monthly_points, timespan, user_timezone).call
end
def user_timezone
# Future: Once user.timezone column exists, uncomment the line below
# user.timezone.presence || Time.zone.name
# For now, use application timezone
Time.zone.name
2024-12-06 10:52:36 -05:00
end
2024-03-23 15:29:55 -04:00
end