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.
|
# Pilots and above: set absolute tach/hobbs readings.
|
||||||
def update_hours
|
def update_hours
|
||||||
delta = HoursService.new(@aircraft).update_hours!(
|
before = { tach: @aircraft.tach_time.to_d, hobbs: @aircraft.hobbs_time.to_d }
|
||||||
tach_time: params.require(:tach_time), hobbs_time: params[:hobbs_time]
|
HoursService.new(@aircraft).update_hours!(tach_time: params.require(:tach_time), hobbs_time: params[:hobbs_time])
|
||||||
)
|
redirect_to @aircraft, notice: hours_notice(before)
|
||||||
redirect_to @aircraft, notice: t("flash.aircraft.hours_updated", delta: "#{'+' if delta >= 0}#{delta}")
|
|
||||||
rescue AircraftGroundedError => e
|
rescue AircraftGroundedError => e
|
||||||
redirect_to @aircraft, alert: e.message
|
redirect_to @aircraft, alert: e.message
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
@@ -64,6 +63,19 @@ class AircraftController < ApplicationController
|
|||||||
|
|
||||||
private
|
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_param = params[:id]
|
||||||
|
|
||||||
def aircraft_params
|
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">
|
<div class="space-y-5">
|
||||||
<section class="card">
|
<section class="card">
|
||||||
<div class="card-header"><h2 class="card-title"><%= t(".update_hours") %></h2></div>
|
<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 class="grid grid-cols-2 gap-3">
|
||||||
<div><%= f.label :tach_time, t(".tach"), class: "label" %>
|
<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" %>
|
<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>
|
</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>
|
<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" %>
|
<%= f.submit t(".update_hours"), class: "btn-primary" %>
|
||||||
|
|||||||
+10
-1
@@ -800,9 +800,12 @@ en:
|
|||||||
added: "%{tail_number} added."
|
added: "%{tail_number} added."
|
||||||
aircraft_updated: Aircraft updated.
|
aircraft_updated: Aircraft updated.
|
||||||
deleted: "%{tail_number} deleted."
|
deleted: "%{tail_number} deleted."
|
||||||
hours_updated: Hours updated (%{delta}).
|
hours_updated: "%{changes}. In-service components moved %{delta} hrs; no flight was logged."
|
||||||
enter_a_valid_tach: Enter a valid tach reading.
|
enter_a_valid_tach: Enter a valid tach reading.
|
||||||
aircraft_details_updated: Aircraft details updated
|
aircraft_details_updated: Aircraft details updated
|
||||||
|
meter_changed: "%{meter} %{from} → %{to} (%{delta})"
|
||||||
|
hours_corrected: 'Corrected: %{changes}. In-service components moved %{delta} hrs; no flight was logged.'
|
||||||
|
hours_unchanged: The readings didn't change.
|
||||||
sso:
|
sso:
|
||||||
try_again_later: Try again later.
|
try_again_later: Try again later.
|
||||||
there_s_no_account: There's no account for %{email} here yet. Ask an administrator to add you, or sign in with your password and link %{provider} from your profile.
|
there_s_no_account: There's no account for %{email} here yet. Ask an administrator to add you, or sign in with your password and link %{provider} from your profile.
|
||||||
@@ -3845,6 +3848,12 @@ en:
|
|||||||
ios_install_first: On iPhone and iPad, install the app first (above); notifications can then be turned on in it.
|
ios_install_first: On iPhone and iPad, install the app first (above); notifications can then be turned on in it.
|
||||||
failed: Couldn't turn notifications on. Try again.
|
failed: Couldn't turn notifications on. Try again.
|
||||||
test_sent: Test notification sent; it should arrive in a few seconds.
|
test_sent: Test notification sent; it should arrive in a few seconds.
|
||||||
|
hours:
|
||||||
|
tach: Tach %{from} → %{to} (%{delta})
|
||||||
|
hobbs: Hobbs %{from} → %{to} (%{delta})
|
||||||
|
effect: Every in-service component's hours change by the same amount, and no flight is logged. To record a flight, use Log Flight instead.
|
||||||
|
lower: 'This lowers the reading: use it only to correct a mistake.'
|
||||||
|
no_change: 'These are the current readings: nothing to update.'
|
||||||
counts:
|
counts:
|
||||||
problem:
|
problem:
|
||||||
one: "%{count} problem"
|
one: "%{count} problem"
|
||||||
|
|||||||
@@ -31,6 +31,20 @@ class PwaFeaturesTest < ActionDispatch::IntegrationTest
|
|||||||
assert_select "form[action='#{session_path}'][data-controller=sign-out]", minimum: 1
|
assert_select "form[action='#{session_path}'][data-controller=sign-out]", minimum: 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "updating the meters without a flight asks for confirmation, showing the change" do
|
||||||
|
sign_in @owner
|
||||||
|
get aircraft_path(@a)
|
||||||
|
assert_select "form[action='#{hours_aircraft_path(@a)}'][data-controller=hours-confirm][data-action='submit->hours-confirm#confirm'][data-hours-confirm-tach-value='1000.0']" do
|
||||||
|
assert_select "input[name=tach_time][data-hours-confirm-target=tach]"
|
||||||
|
assert_select "input[name=hobbs_time][data-hours-confirm-target=hobbs]"
|
||||||
|
end
|
||||||
|
|
||||||
|
patch hours_aircraft_path(@a), params: { tach_time: "1001.4", hobbs_time: "1.2" }
|
||||||
|
assert_equal "Tach 1,000.0 → 1,001.4 (+1.4) · Hobbs 0.0 → 1.2 (+1.2). In-service components moved +1.4 hrs; no flight was logged.", flash[:notice]
|
||||||
|
patch hours_aircraft_path(@a), params: { tach_time: "1001.0" }
|
||||||
|
assert_equal "Corrected: Tach 1,001.4 → 1,001.0 (−0.4). In-service components moved −0.4 hrs; no flight was logged.", flash[:notice]
|
||||||
|
end
|
||||||
|
|
||||||
test "every page lists what to save ahead of time for working offline: each of your aircraft's forms and pages" do
|
test "every page lists what to save ahead of time for working offline: each of your aircraft's forms and pages" do
|
||||||
other = create_aircraft(tail_number: "N99999") # not this pilot's
|
other = create_aircraft(tail_number: "N99999") # not this pilot's
|
||||||
sign_in @pilot
|
sign_in @pilot
|
||||||
|
|||||||
Reference in new issue
Block a user