mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Confirm Update Hours and say what changed
- Updating the meters without a flight asks first, showing each reading's change, that every in-service component moves and no flight is logged, and a warning when a reading goes down; unchanged readings aren't sent - The flash lists each changed reading (old -> new, difference) and how far the components moved; a lowered reading is labelled a correction Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f534541467
commit
cce1ce51b5
5 files changed
+75
-8
No files matched your search
@@ -52,10 +52,9 @@ class AircraftController < ApplicationController
|
||||
|
||||
# Pilots and above: set absolute tach/hobbs readings.
|
||||
def update_hours
|
||||
delta = HoursService.new(@aircraft).update_hours!(
|
||||
tach_time: params.require(:tach_time), hobbs_time: params[:hobbs_time]
|
||||
)
|
||||
redirect_to @aircraft, notice: t("flash.aircraft.hours_updated", delta: "#{'+' if delta >= 0}#{delta}")
|
||||
before = { tach: @aircraft.tach_time.to_d, hobbs: @aircraft.hobbs_time.to_d }
|
||||
HoursService.new(@aircraft).update_hours!(tach_time: params.require(:tach_time), hobbs_time: params[:hobbs_time])
|
||||
redirect_to @aircraft, notice: hours_notice(before)
|
||||
rescue AircraftGroundedError => e
|
||||
redirect_to @aircraft, alert: e.message
|
||||
rescue ArgumentError
|
||||
@@ -64,6 +63,19 @@ class AircraftController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
# "Tach 4,211.6 → 4,213.0 (+1.4). In-service components moved +1.4 hrs; no flight was logged."
|
||||
def hours_notice(before)
|
||||
after = { tach: @aircraft.tach_time.to_d, hobbs: @aircraft.hobbs_time.to_d }
|
||||
signed = ->(d) { "#{d.negative? ? '−' : '+'}#{helpers.hours(d.abs)}" }
|
||||
changes = %i[tach hobbs].filter_map do |meter|
|
||||
d = after[meter] - before[meter]
|
||||
t("flash.aircraft.meter_changed", meter: Aircraft::TIME_BASES[meter.to_s], from: helpers.hours(before[meter]), to: helpers.hours(after[meter]), delta: signed.(d)) unless d.zero?
|
||||
end
|
||||
moved = @aircraft.hobbs? ? after[:hobbs] - before[:hobbs] : after[:tach] - before[:tach]
|
||||
return t("flash.aircraft.hours_unchanged") if changes.empty?
|
||||
t(moved.negative? ? "flash.aircraft.hours_corrected" : "flash.aircraft.hours_updated", changes: changes.join(" · "), delta: signed.(moved))
|
||||
end
|
||||
|
||||
def aircraft_param = params[:id]
|
||||
|
||||
def aircraft_params
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
import { t, locale } from "i18n"
|
||||
|
||||
// Update Hours (Overview): confirms the new meter readings first, since they
|
||||
// change every in-service component's hours and log no flight.
|
||||
export default class extends Controller {
|
||||
static targets = [ "tach", "hobbs" ]
|
||||
static values = { tach: Number, hobbs: Number }
|
||||
|
||||
confirm(event) {
|
||||
const lines = [ this.line("tach", this.tachValue, this.tachTarget.value),
|
||||
this.hasHobbsTarget ? this.line("hobbs", this.hobbsValue, this.hobbsTarget.value) : null ].filter(Boolean)
|
||||
if (!lines.length) {
|
||||
event.preventDefault()
|
||||
return alert(t("hours.no_change"))
|
||||
}
|
||||
const lower = parseFloat(this.tachTarget.value) < this.tachValue
|
||||
const message = [ ...lines, "", t("hours.effect"), lower ? t("hours.lower") : null ].filter((l) => l !== null).join("\n")
|
||||
if (!confirm(message)) event.preventDefault()
|
||||
}
|
||||
|
||||
// "Tach 4,211.6 → 4,213.0 (+1.4)", or null when unchanged or blank.
|
||||
line(meter, now, entered) {
|
||||
const value = parseFloat(entered)
|
||||
if (Number.isNaN(value) || Math.abs(value - now) < 0.05) return null
|
||||
const fmt = (n) => n.toLocaleString(locale(), { minimumFractionDigits: 1, maximumFractionDigits: 1 })
|
||||
const delta = value - now
|
||||
return t(`hours.${meter}`, { from: fmt(now), to: fmt(value), delta: `${delta > 0 ? "+" : "−"}${fmt(Math.abs(delta))}` })
|
||||
}
|
||||
}
|
||||
@@ -59,12 +59,14 @@
|
||||
<div class="space-y-5">
|
||||
<section class="card">
|
||||
<div class="card-header"><h2 class="card-title"><%= t(".update_hours") %></h2></div>
|
||||
<%= form_with url: hours_aircraft_path(@aircraft), method: :patch, class: "card-body space-y-3" do |f| %>
|
||||
<%= form_with url: hours_aircraft_path(@aircraft), method: :patch, class: "card-body space-y-3",
|
||||
data: { controller: "hours-confirm", action: "submit->hours-confirm#confirm",
|
||||
hours_confirm_tach_value: @aircraft.tach_time.to_f, hours_confirm_hobbs_value: @aircraft.hobbs_time.to_f } do |f| %>
|
||||
<div class="grid grid-cols-2 gap-3">
|
||||
<div><%= f.label :tach_time, t(".tach"), class: "label" %>
|
||||
<%= f.number_field :tach_time, value: @aircraft.tach_time, step: 0.1, class: "input", required: true %></div>
|
||||
<%= f.number_field :tach_time, value: @aircraft.tach_time, step: 0.1, class: "input", required: true, data: { hours_confirm_target: "tach" } %></div>
|
||||
<div><%= f.label :hobbs_time, t(".hobbs"), class: "label" %>
|
||||
<%= f.number_field :hobbs_time, value: @aircraft.hobbs_time, step: 0.1, class: "input" %></div>
|
||||
<%= f.number_field :hobbs_time, value: @aircraft.hobbs_time, step: 0.1, class: "input", data: { hours_confirm_target: "hobbs" } %></div>
|
||||
</div>
|
||||
<p class="hint"><%= t(".enter_the_current_meter_readings_html", link: (link_to t(".log_flight"), new_aircraft_flight_log_path(@aircraft), class: "link")) %></p>
|
||||
<%= f.submit t(".update_hours"), class: "btn-primary" %>
|
||||
|
||||
Reference in new issue
Block a user