dawarich/app/controllers/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

88 lines
2.3 KiB
Ruby

# frozen_string_literal: true
class TripsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_active_user!, only: %i[new create]
before_action :set_trip, only: %i[show edit update destroy]
before_action :set_coordinates, only: %i[show edit]
def index
@trips = current_user.trips.order(started_at: :desc).page(params[:page]).per(6)
end
def show
@photo_previews = Rails.cache.fetch("trip_photos_#{@trip.id}", expires_in: 1.day) do
@trip.photo_previews
end
@photo_sources = @trip.photo_sources
return unless @trip.path.blank? || @trip.distance.blank? || @trip.visited_countries.blank?
Trips::CalculateAllJob.perform_later(@trip.id, current_user.safe_settings.distance_unit)
end
def new
@trip = Trip.new
@coordinates = []
end
def edit; end
def create
@trip = current_user.trips.build(trip_params)
if @trip.save
redirect_to @trip, notice: 'Trip was successfully created. Data is being calculated in the background.'
else
render :new, status: :unprocessable_content
end
end
def update
# Handle sharing settings update
handle_sharing_update if params[:sharing]
# Handle regular trip update
if @trip.update(trip_params)
redirect_to @trip, notice: 'Trip was successfully updated.', status: :see_other
else
render :edit, status: :unprocessable_content
end
end
def destroy
@trip.destroy!
redirect_to trips_url, notice: 'Trip was successfully destroyed.', status: :see_other
end
private
def set_trip
@trip = current_user.trips.find(params[:id])
end
def set_coordinates
@coordinates = @trip.points.pluck(
:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id,
:country
).map { [_1.to_f, _2.to_f, _3.to_s, _4.to_s, _5.to_s, _6.to_s, _7.to_s, _8.to_s] }
end
def handle_sharing_update
if params[:sharing][:enabled] == '1'
sharing_options = {
expiration: params[:sharing][:expiration] || '24h',
share_notes: params[:sharing][:share_notes] == '1',
share_photos: params[:sharing][:share_photos] == '1'
}
@trip.enable_sharing!(**sharing_options)
else
@trip.disable_sharing!
end
end
def trip_params
params.require(:trip).permit(:name, :started_at, :ended_at, :notes)
end
end