mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-12 10:11:38 -05:00
Major improvements: 1. Use Turbo for sharing updates - no page reload, modal stays open 2. Add Stimulus copy button controller - clean implementation with 'Copied!' feedback 3. Allow updating notes/photos toggles without disabling sharing 4. Add 'Update Sharing' button to save changes while keeping sharing enabled 5. Use 'true'/'false' strings consistently instead of '1'/'0' 6. Update all request specs to use 'true'/'false' values Technical changes: - Wrap form in turbo_frame_tag for seamless updates - Controller responds with turbo_stream to replace form content - Create copy_button_controller.js for proper copy feedback - Checkboxes now editable when sharing is enabled - Separate 'Update Sharing' and 'Disable Sharing' actions
102 lines
2.8 KiB
Ruby
102 lines
2.8 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
|
|
if params[:trip] && params[:trip][:sharing]
|
|
handle_sharing_update
|
|
|
|
respond_to do |format|
|
|
format.turbo_stream { render turbo_stream: turbo_stream.replace("sharing_form_#{@trip.id}", partial: 'trips/sharing', locals: { trip: @trip }) }
|
|
format.html { redirect_to @trip, notice: 'Trip was successfully updated.', status: :see_other }
|
|
end
|
|
return
|
|
end
|
|
|
|
# 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
|
|
sharing_params = params[:trip][:sharing]
|
|
|
|
if to_boolean(sharing_params[:enabled])
|
|
sharing_options = {
|
|
expiration: sharing_params[:expiration] || '24h',
|
|
share_notes: to_boolean(sharing_params[:share_notes]),
|
|
share_photos: to_boolean(sharing_params[:share_photos])
|
|
}
|
|
|
|
@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
|
|
|
|
def to_boolean(value)
|
|
ActiveModel::Type::Boolean.new.cast(value)
|
|
end
|
|
end
|