From 6defd4d8d01fde2dedb4dbeb3228b77c5d86bb23 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Mon, 19 May 2025 19:10:07 +0200 Subject: [PATCH] Update distance unit in trip page --- app/controllers/trips_controller.rb | 2 +- app/jobs/trips/calculate_all_job.rb | 6 +++--- app/jobs/trips/calculate_countries_job.rb | 8 ++++---- app/models/trip.rb | 2 +- app/views/trips/_countries.html.erb | 12 ++++++------ 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/controllers/trips_controller.rb b/app/controllers/trips_controller.rb index 1af33dc1..1880002b 100644 --- a/app/controllers/trips_controller.rb +++ b/app/controllers/trips_controller.rb @@ -17,7 +17,7 @@ class TripsController < ApplicationController @photo_sources = @trip.photo_sources if @trip.path.blank? || @trip.distance.blank? || @trip.visited_countries.blank? - Trips::CalculateAllJob.perform_later(@trip.id) + Trips::CalculateAllJob.perform_later(@trip.id, current_user.safe_settings.distance_unit) end end diff --git a/app/jobs/trips/calculate_all_job.rb b/app/jobs/trips/calculate_all_job.rb index 1564c97d..0500881c 100644 --- a/app/jobs/trips/calculate_all_job.rb +++ b/app/jobs/trips/calculate_all_job.rb @@ -3,9 +3,9 @@ class Trips::CalculateAllJob < ApplicationJob queue_as :default - def perform(trip_id) + def perform(trip_id, distance_unit = 'km') Trips::CalculatePathJob.perform_later(trip_id) - Trips::CalculateDistanceJob.perform_later(trip_id) - Trips::CalculateCountriesJob.perform_later(trip_id) + Trips::CalculateDistanceJob.perform_later(trip_id, distance_unit) + Trips::CalculateCountriesJob.perform_later(trip_id, distance_unit) end end diff --git a/app/jobs/trips/calculate_countries_job.rb b/app/jobs/trips/calculate_countries_job.rb index d6042e4b..e63365d3 100644 --- a/app/jobs/trips/calculate_countries_job.rb +++ b/app/jobs/trips/calculate_countries_job.rb @@ -3,23 +3,23 @@ class Trips::CalculateCountriesJob < ApplicationJob queue_as :default - def perform(trip_id) + def perform(trip_id, distance_unit) trip = Trip.find(trip_id) trip.calculate_countries trip.save! - broadcast_update(trip) + broadcast_update(trip, distance_unit) end private - def broadcast_update(trip) + def broadcast_update(trip, distance_unit) Turbo::StreamsChannel.broadcast_update_to( "trip_#{trip.id}", target: "trip_countries", partial: "trips/countries", - locals: { trip: trip } + locals: { trip: trip, distance_unit: distance_unit } ) end end diff --git a/app/models/trip.rb b/app/models/trip.rb index 8863e748..809ce154 100644 --- a/app/models/trip.rb +++ b/app/models/trip.rb @@ -11,7 +11,7 @@ class Trip < ApplicationRecord after_update :enqueue_calculation_jobs, if: -> { saved_change_to_started_at? || saved_change_to_ended_at? } def enqueue_calculation_jobs - Trips::CalculateAllJob.perform_later(id) + Trips::CalculateAllJob.perform_later(id, user.safe_settings.distance_unit) end def points diff --git a/app/views/trips/_countries.html.erb b/app/views/trips/_countries.html.erb index f4518f33..110d796b 100644 --- a/app/views/trips/_countries.html.erb +++ b/app/views/trips/_countries.html.erb @@ -2,23 +2,23 @@
Distance
-
<%= @trip.distance %> <%= current_user.safe_settings.distance_unit %>
+
<%= trip.distance %> <%= distance_unit %>
Duration
-
<%= trip_duration(@trip) %>
+
<%= trip_duration(trip) %>
Countries
- <% if @trip.countries.any? %> - <%= @trip.countries.join(', ') %> - <% elsif @trip.visited_countries.present? %> - <%= @trip.visited_countries.join(', ') %> + <% if trip.countries.any? %> + <%= trip.countries.join(', ') %> + <% elsif trip.visited_countries.present? %> + <%= trip.visited_countries.join(', ') %> <% else %> Countries are being calculated...