Calculate path and distance before saving trip

This commit is contained in:
Eugene Burmakin 2025-01-24 15:03:57 +01:00
parent 1e7efbc9af
commit 380dd9235d
5 changed files with 17 additions and 6 deletions

View file

@ -40,6 +40,7 @@
background-color: white !important;
}
.trix-content {
.trix-content-editor {
min-height: 10rem;
width: 100%;
}

View file

@ -1,3 +1,7 @@
// This controller is being used on:
// - trips/new
// - trips/edit
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {

View file

@ -1,3 +1,8 @@
// This controller is being used on:
// - trips/show
// - trips/edit
// - trips/new
import { Controller } from "@hotwired/stimulus"
import L from "leaflet"
import {
@ -192,7 +197,7 @@ export default class extends Controller {
this.map.fitBounds(bounds, { padding: [50, 50] })
}
// Add this new method to update coordinates and refresh the map
// Update coordinates and refresh the map
updateMapWithCoordinates(newCoordinates) {
// Transform the coordinates to match the expected format
this.coordinates = newCoordinates.map(point => [

View file

@ -7,12 +7,13 @@ class Trip < ApplicationRecord
validates :name, :started_at, :ended_at, presence: true
before_save :calculate_distance
before_save :create_path!
def create_path!
self.path = Tracks::BuildPath.new(points.pluck(:latitude, :longitude)).call
trip_path = Tracks::BuildPath.new(points.pluck(:latitude, :longitude)).call
distance = calculate_distance
save!
update_columns(path: trip_path, distance: distance) # Avoids recursion with `after_save`
end
def points

View file

@ -62,7 +62,7 @@
<div class="form-control">
<%= form.label :notes %>
<%= form.rich_text_area :notes %>
<%= form.rich_text_area :notes, class: 'trix-content-editor' %>
</div>
<div>