dawarich/app/controllers/shared/trips_controller.rb
Claude b1cbb5555f
Refactor: Apply Rails best practices to trip sharing implementation
- Remove unused @is_public_view variable from controller
- Simplify conditionals by leveraging methods that return [] when empty
- Move public view from trips/public_show to shared/trips/show (Rails conventions)
- Refactor trips#update to be HTML-only (remove JSON responses)
- Convert sharing form to use proper Rails form helpers
- Move JS controller to shared/ subdirectory with proper namespacing
- Create RSpec shared examples for Shareable concern to eliminate duplication
- Update request specs to match HTML-only controller behavior
- Apply 'render/redirect ... and return' pattern for early returns
2025-11-07 12:05:34 +00:00

33 lines
942 B
Ruby

# frozen_string_literal: true
class Shared::TripsController < ApplicationController
def show
@trip = Trip.find_by(sharing_uuid: params[:trip_uuid])
redirect_to root_path, alert: 'Shared trip not found or no longer available' and return unless @trip&.public_accessible?
@user = @trip.user
@coordinates = extract_coordinates
@photo_previews = fetch_photo_previews
end
private
def extract_coordinates
return [] unless @trip.path&.coordinates
# Convert PostGIS LineString coordinates [lng, lat] to [lat, lng] for Leaflet
@trip.path.coordinates.map { |coord| [coord[1], coord[0]] }
end
def fetch_photo_previews
return [] unless @trip.share_photos?
Rails.cache.fetch("trip_photos_#{@trip.id}", expires_in: 1.day) do
@trip.photo_previews
end
rescue StandardError => e
Rails.logger.error("Failed to fetch photo previews for trip #{@trip.id}: #{e.message}")
[]
end
end