mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
What people can do on an aircraft now comes from their role there, and administrators set what each role allows (Admin → Roles). - Roles are installation-wide sets of permissions (31, grouped as flying, squawks and notes, maintenance records, imports and exports, the aircraft, notifications). Seeing an aircraft's records needs none. - Built in: Owner (always every permission, so an aircraft can always be managed), Mechanic (shop maintenance: logbook, signing, components, inspections, ADs, squawks, reminders), Pilot (what pilots could do before) and Viewer (view only). Administrators can change all but Owner's, and add their own; a role can be deleted once nobody has it. - Existing owners and pilots keep exactly what they could do. - Admin → Users → a user lists their role on every aircraft, to change, remove or add. On the aircraft's Access page, anyone who manages people hands out roles, but only an Owner can give or take away Owner, nobody changes their own (except administrators), and an aircraft always keeps an Owner (AircraftAccess). - Controllers declare the permission each action needs; a request that changes something with none declared is refused (and raises in development and tests), and a test walks every route to check. - Weekly reminders and overdue alerts go to roles that get reminders; the DRS reminder also needs Manage ADs. The API follows the person's role; its owners_only error is now not_permitted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
119 lines
5.3 KiB
Ruby
119 lines
5.3 KiB
Ruby
class OilAnalysesController < ApplicationController
|
|
include AircraftScoped
|
|
requires_permission :record_oil_analyses, only: %i[new create import]
|
|
requires_permission :manage_oil_analyses, only: %i[edit update destroy]
|
|
before_action :set_analysis, only: %i[show edit update destroy]
|
|
|
|
COMPARE_SAMPLES = 6 # columns in the trend table, like the lab report's history
|
|
|
|
def index
|
|
@analyses = @aircraft.oil_analyses.recent_first.includes(:component).with_attached_report
|
|
end
|
|
|
|
def show
|
|
@previous = @aircraft.oil_analyses.where(component_id: @analysis.component_id)
|
|
.where("sample_date < ?", @analysis.sample_date).recent_first.first
|
|
end
|
|
|
|
def new
|
|
engine = default_engine
|
|
@analysis = @aircraft.oil_analyses.new(sample_date: Date.current, component: engine,
|
|
engine_hours: engine&.hours_since_overhaul,
|
|
lab: @aircraft.oil_analyses.recent_first.pick(:lab))
|
|
end
|
|
|
|
def create
|
|
@analysis = @aircraft.oil_analyses.new(analysis_params)
|
|
if @analysis.save
|
|
log_event("oil_analysis", t("flash.oil_analyses.oil_analysis_added_sampled", sample_date: l(@analysis.sample_date, format: :app)))
|
|
redirect_to aircraft_oil_analysis_path(@aircraft, @analysis), notice: t("flash.oil_analyses.oil_analysis_saved")
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
# Create records from a lab report PDF (AvLab or Blackstone). Reports repeat
|
|
# earlier samples, so samples already on file are skipped rather than duplicated.
|
|
def import
|
|
file = params[:report]
|
|
return redirect_to(new_aircraft_oil_analysis_path(@aircraft), alert: t("flash.oil_analyses.choose_a_lab_report")) unless file.respond_to?(:tempfile)
|
|
|
|
report = OilReportParser.from_pdf(file.tempfile)
|
|
file.rewind
|
|
if report.tail_number.present? && normalize_tail(report.tail_number) != normalize_tail(@aircraft.tail_number)
|
|
return redirect_to(new_aircraft_oil_analysis_path(@aircraft),
|
|
alert: t("flash.oil_analyses.that_report_is_for", report_tail: report.tail_number, tail_number: @aircraft.tail_number))
|
|
end
|
|
|
|
engine = engine_for(report)
|
|
created = []
|
|
current_record = nil
|
|
OilAnalysis.transaction do
|
|
report.samples.each do |sample|
|
|
record = existing_sample(sample) ||
|
|
@aircraft.oil_analyses.create!(sample.except(:current).merge(lab: report.lab, oil_type: report.oil_type, component: engine)).tap { created << _1 }
|
|
current_record = record if sample[:current]
|
|
end
|
|
end
|
|
current_record.report.attach(file) if current_record && !current_record.report.attached?
|
|
|
|
skipped = report.samples.size - created.size
|
|
created.each { log_event("oil_analysis", t("flash.oil_analyses.oil_analysis_imported_sampled", sample_date: _1.sample_date)) }
|
|
notice = t("flash.oil_analyses.imported", count: created.size, lab: report.lab)
|
|
notice += " #{t('flash.oil_analyses.already_recorded', count: skipped)}" if skipped.positive?
|
|
redirect_to aircraft_oil_analyses_path(@aircraft), notice: notice
|
|
rescue OilReportParser::Error => e
|
|
redirect_to new_aircraft_oil_analysis_path(@aircraft), alert: t("flash.oil_analyses.couldn_t_import_that", message: e.message)
|
|
end
|
|
|
|
def edit; end
|
|
|
|
def update
|
|
if @analysis.update(analysis_params)
|
|
log_event("oil_analysis", t("flash.oil_analyses.oil_analysis_updated_sampled", sample_date: l(@analysis.sample_date, format: :app)))
|
|
redirect_to aircraft_oil_analysis_path(@aircraft, @analysis), notice: t("flash.oil_analyses.oil_analysis_updated")
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@analysis.destroy!
|
|
log_event("oil_analysis", t("flash.oil_analyses.oil_analysis_deleted_sampled", sample_date: l(@analysis.sample_date, format: :app)))
|
|
redirect_to aircraft_oil_analyses_path(@aircraft), notice: t("flash.oil_analyses.oil_analysis_deleted")
|
|
end
|
|
|
|
private
|
|
|
|
def set_analysis = @analysis = @aircraft.oil_analyses.find(params[:id])
|
|
|
|
def normalize_tail(tail) = tail.to_s.upcase.delete("^A-Z0-9")
|
|
|
|
# Same sample date, and the same lab sample number unless one side has none.
|
|
def existing_sample(sample)
|
|
scope = @aircraft.oil_analyses.where(sample_date: sample[:sample_date])
|
|
scope = scope.where(sample_number: [ sample[:sample_number], nil ]) if sample[:sample_number]
|
|
scope.first
|
|
end
|
|
|
|
# The engine whose serial number is on the report, else the one in service.
|
|
def engine_for(report)
|
|
serial = report.engine_serial.to_s.strip.upcase
|
|
match = @aircraft.components.where.not(serial_number: [ nil, "" ]).find { _1.serial_number.strip.upcase == serial } if serial.present?
|
|
match || default_engine
|
|
end
|
|
|
|
def default_engine
|
|
@aircraft.components.joins(:component_type).where(status: "IN-USE", component_types: { name: "Engine" }).first
|
|
end
|
|
|
|
def analysis_params
|
|
params.require(:oil_analysis).permit(
|
|
:component_id, :sample_date, :analysis_date, :lab, :sample_number, :oil_type, :engine_hours, :oil_hours,
|
|
:filter_hours, :oil_added, :status, :lab_comments, :notes, :report, :excluded_from_averages,
|
|
elements: OilAnalysis::ELEMENTS.keys, element_averages: OilAnalysis::ELEMENTS.keys,
|
|
oil_properties: OilAnalysis::PROPERTIES.keys
|
|
).tap { |p| p[:component_id] = scoped_id(@aircraft.components, p[:component_id]) if p.key?(:component_id) }
|
|
end
|
|
end
|