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>
92 lines
3.7 KiB
Ruby
92 lines
3.7 KiB
Ruby
class ComponentsController < ApplicationController
|
|
include AircraftScoped
|
|
requires_permission :manage_components, except: %i[index show]
|
|
before_action :set_component, only: %i[show edit update destroy reset_service]
|
|
|
|
def index
|
|
all = @aircraft.components.includes(:component_type, :parent_component).order(:install_location)
|
|
@show_disposed = params[:disposed].present?
|
|
all = all.where.not(status: "DISPOSED") unless @show_disposed
|
|
# Depth-first tree order: parents followed by their children.
|
|
by_parent = all.group_by(&:parent_component_id)
|
|
@rows = []
|
|
walk = ->(parent_id, depth) do
|
|
(by_parent[parent_id] || []).sort_by { [ _1.component_type.name, _1.install_location.to_s ] }.each do |c|
|
|
@rows << [ c, depth ]
|
|
walk.(c.id, depth + 1)
|
|
end
|
|
end
|
|
walk.(nil, 0)
|
|
orphans = all.to_a - @rows.map(&:first) # children whose parent is filtered out
|
|
orphans.each { @rows << [ _1, 0 ] }
|
|
end
|
|
|
|
def show
|
|
@squawks = @component.squawks.by_priority.with_attached_attachments
|
|
ids = @component.inspection_type_ids
|
|
@inspection_rows = Compliance.inspection_rows(@aircraft).select { ids.include?(_1.type.id) }
|
|
end
|
|
|
|
def new
|
|
@component = @aircraft.components.new(date_in_service: Date.current, status: "IN-USE",
|
|
parent_component_id: params[:parent_id])
|
|
end
|
|
|
|
def create
|
|
@component = @aircraft.components.new(component_params)
|
|
if @component.save
|
|
log_event("component", t("flash.components.component_added_2", component: @component))
|
|
redirect_to aircraft_components_path(@aircraft), notice: t("flash.components.component_added")
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def edit; end
|
|
|
|
def update
|
|
if @component.update(component_params)
|
|
log_event("component", t("flash.components.component_updated_2", component: @component))
|
|
redirect_to aircraft_components_path(@aircraft), notice: t("flash.components.component_updated")
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@component.destroy!
|
|
log_event("component", t("flash.components.component_deleted_2", component: @component))
|
|
redirect_to aircraft_components_path(@aircraft), notice: t("flash.components.component_deleted")
|
|
end
|
|
|
|
# Always zeroes hours since overhaul; reset_in_service=1 also zeroes the
|
|
# in-service clock (use when the part was physically replaced).
|
|
def reset_service
|
|
replaced = params[:reset_in_service] == "1"
|
|
@component.reset_service!(reset_in_service: replaced)
|
|
log_event("component", t(replaced ? "flash.components.replaced_event" : "flash.components.serviced_event", component: @component))
|
|
redirect_to aircraft_components_path(@aircraft), notice: t(replaced ? "flash.components.replaced" : "flash.components.serviced", component: @component)
|
|
end
|
|
|
|
private
|
|
|
|
def set_component
|
|
@component = @aircraft.components.find(params[:id])
|
|
end
|
|
|
|
def component_params
|
|
params.require(:component).permit(
|
|
:component_type_id, :parent_component_id, :manufacturer, :model, :serial_number,
|
|
:install_location, :notes, :status, :date_in_service, :hours_in_service,
|
|
:hours_since_overhaul, :overhaul_date, :overhaul_kind, :tbo_hours, :tbo_days, :replacement_hours, :replacement_days, :replacement_every, :replacement_unit, :tbo_every, :tbo_unit,
|
|
:tbo_critical, :on_condition, :replacement_critical, :replacement_from_install, :expires_on
|
|
).tap do |p|
|
|
# Owners can create a component type inline instead of asking an admin.
|
|
if (name = params[:new_component_type].to_s.strip).present?
|
|
type = ComponentType.where("lower(name) = ?", name.downcase).first || ComponentType.create!(name: name)
|
|
p[:component_type_id] = type.id
|
|
end
|
|
end
|
|
end
|
|
end
|