dawarich/app/views/trips/show.html.erb
Claude ce5e57a691
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 15:44:27 +00:00

70 lines
2.4 KiB
Text

<% content_for :title, @trip.name %>
<%= turbo_stream_from "trip_#{@trip.id}" %>
<div class="container mx-auto px-4 my-5">
<div class="bg-base-100 p-4">
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div class="w-full block" id="trip_path">
<%= render "trips/path", trip: @trip, current_user: current_user %>
</div>
<div class="w-full">
<div class="text-center mb-8">
<h1 class="text-4xl font-bold mb-2"><%= @trip.name %></h1>
<p class="text-md text-base-content/60 mb-4">
<%= human_date(@trip.started_at) %> - <%= human_date(@trip.ended_at) %>
</p>
<%= render "trips/countries", trip: @trip, current_user: current_user, distance_unit: current_user.safe_settings.distance_unit %>
</div>
<div>
<%= @trip.notes.body %>
</div>
<!-- Photos Grid Section -->
<% if @photo_previews.any? %>
<% @photo_previews.each_slice(3) do |slice| %>
<div class="h-48 flex gap-4 mt-4 justify-center">
<% slice.each do |photo| %>
<div class="flex-1 h-full overflow-hidden rounded-lg transition-transform duration-300 hover:scale-105 hover:shadow-lg">
<img
src="<%= photo[:url] %>"
loading='lazy'
class="h-full w-full object-cover"
>
</div>
<% end %>
</div>
<% end %>
<% end %>
<% if @photo_sources.any? %>
<div class="text-center mt-6">
<% @photo_sources.each do |source| %>
<%= link_to "More photos on #{source}", photo_search_url(source, current_user.settings, @trip.started_at, @trip.ended_at), class: "btn btn-primary mt-2", target: '_blank' %>
<% end %>
</div>
<% end %>
</div>
</div>
</div>
<!-- Action Buttons Section -->
<div class="bg-base-100 items-center">
<div class="flex flex-wrap gap-2 justify-center">
<%= link_to "Edit this trip", edit_trip_path(@trip), class: "btn" %>
<%= link_to "Destroy this trip",
trip_path(@trip),
data: {
turbo_confirm: "Are you sure?",
turbo_method: :delete
},
class: "btn" %>
<%= link_to "Back to trips", trips_path, class: "btn" %>
</div>
</div>
<!-- Sharing Section -->
<%= render "trips/sharing", trip: @trip %>
</div>