dawarich/app/controllers/shared/trips_controller.rb
Claude 429f90e666
Refactor: Apply Rails best practice for early returns
Follow Rails convention of using "render/redirect ... and return"
instead of standalone return statements in controller actions.

## Changes

**Shared::TripsController#show**
Before:
```ruby
unless @trip&.public_accessible?
  return redirect_to root_path, alert: '...'
end
```

After:
```ruby
redirect_to root_path, alert: '...' and return unless @trip&.public_accessible?
```

**TripsController#update**
Before:
```ruby
if params[:sharing]
  return update_sharing
end
```

After:
```ruby
update_sharing and return if params[:sharing]
```

## Benefits
- More idiomatic Rails code
- Clearer intent with single-line guard clauses
- Prevents potential double render issues
- Follows community best practices
2025-11-07 11:49:05 +00:00

34 lines
1,013 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
@is_public_view = true
@coordinates = @trip.path.present? ? extract_coordinates : []
@photo_previews = @trip.share_photos? ? fetch_photo_previews : []
render 'trips/public_show'
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
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