mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Initial commit: Rails port of Simple Aircraft Manager
Rails 8.1 + SQLite rewrite of marbindrakon/simple-aircraft-manager, designed to self-host as a single container (Solid Queue/Cache/Cable, Active Storage on disk, Hotwire + importmap, Tailwind). - Airworthiness engine ported from health/services.py (ADs, grounding squawks, inspection recurrency, component replacement) - Hours updates and flight logs cascading to in-service components, optional airworthiness enforcement - Components, squawks, ADs, inspections, logbook, documents, major records, oil/fuel consumption stats and charts, activity log - Admin/owner/pilot RBAC and public share links (status/maintenance) - Docker Compose and Kamal deployment, env-driven production config - Service and integration tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
38df19c331
264 files changed
+8365
No files matched your search
@@ -0,0 +1,57 @@
|
||||
class AdCompliancesController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!
|
||||
before_action :set_compliance, only: %i[edit update destroy]
|
||||
|
||||
def new
|
||||
ad = @aircraft.applicable_ads.find_by(id: params[:ad_id])
|
||||
@compliance = AdCompliance.new(aircraft: @aircraft, airworthiness_directive: ad, date_complied: Date.current,
|
||||
aircraft_hours_at_compliance: @aircraft.tach_time)
|
||||
if ad&.recurring? && ad.recurring_hours.to_d > 0
|
||||
@compliance.next_due_at_time = @aircraft.current_hours + ad.recurring_hours
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
@compliance = AdCompliance.new(compliance_params.merge(aircraft: @aircraft))
|
||||
if @compliance.save
|
||||
log_event("ad", "Compliance recorded: #{@compliance.airworthiness_directive.name}")
|
||||
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "Compliance recorded."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @compliance.update(compliance_params)
|
||||
log_event("ad", "Compliance updated: #{@compliance.airworthiness_directive.name}")
|
||||
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "Compliance updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@compliance.destroy!
|
||||
log_event("ad", "Compliance deleted: #{@compliance.airworthiness_directive.name}")
|
||||
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "Compliance record deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_compliance = @compliance = AdCompliance.for_aircraft(@aircraft).find(params[:id])
|
||||
|
||||
def compliance_params
|
||||
p = params.require(:ad_compliance).permit(
|
||||
:airworthiness_directive_id, :date_complied, :compliance_notes, :permanent, :next_due_at_time,
|
||||
:aircraft_hours_at_compliance, :logbook_entry_id, :inspection_record_id, :component_id
|
||||
)
|
||||
p[:airworthiness_directive_id] = scoped_id(@aircraft.applicable_ads, p[:airworthiness_directive_id])
|
||||
p[:logbook_entry_id] = scoped_id(@aircraft.logbook_entries, p[:logbook_entry_id])
|
||||
p[:inspection_record_id] = scoped_id(InspectionRecord.for_aircraft(@aircraft), p[:inspection_record_id])
|
||||
p[:component_id] = scoped_id(@aircraft.components, p[:component_id])
|
||||
p
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,44 @@
|
||||
class Admin::ComponentTypesController < ApplicationController
|
||||
before_action :require_admin!
|
||||
before_action :set_type, only: %i[edit update destroy]
|
||||
|
||||
def index
|
||||
@component_types = ComponentType.ordered
|
||||
end
|
||||
|
||||
def new
|
||||
@component_type = ComponentType.new
|
||||
end
|
||||
|
||||
def create
|
||||
@component_type = ComponentType.new(type_params)
|
||||
if @component_type.save
|
||||
redirect_to admin_component_types_path, notice: "Component type created."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @component_type.update(type_params)
|
||||
redirect_to admin_component_types_path, notice: "Component type updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
if @component_type.destroy
|
||||
redirect_to admin_component_types_path, notice: "Component type deleted."
|
||||
else
|
||||
redirect_to admin_component_types_path, alert: @component_type.errors.full_messages.to_sentence
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_type = @component_type = ComponentType.find(params[:id])
|
||||
def type_params = params.require(:component_type).permit(:name, :consumable)
|
||||
end
|
||||
@@ -0,0 +1,50 @@
|
||||
class Admin::UsersController < ApplicationController
|
||||
before_action :require_admin!
|
||||
before_action :set_user, only: %i[edit update destroy]
|
||||
|
||||
def index
|
||||
@users = User.order(:email_address).includes(aircraft_roles: :aircraft)
|
||||
end
|
||||
|
||||
def new
|
||||
@user = User.new
|
||||
end
|
||||
|
||||
def create
|
||||
@user = User.new(user_params)
|
||||
@user.admin = admin_flag
|
||||
if @user.save
|
||||
redirect_to admin_users_path, notice: "User #{@user.email_address} created."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
attrs = user_params
|
||||
attrs = attrs.except(:password, :password_confirmation) if attrs[:password].blank?
|
||||
if @user == current_user && !admin_flag
|
||||
return redirect_to admin_users_path, alert: "You can't remove your own admin rights."
|
||||
end
|
||||
@user.admin = admin_flag
|
||||
if @user.update(attrs)
|
||||
redirect_to admin_users_path, notice: "User updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
return redirect_to admin_users_path, alert: "You can't delete yourself." if @user == current_user
|
||||
@user.destroy!
|
||||
redirect_to admin_users_path, notice: "User deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_user = @user = User.find(params[:id])
|
||||
def user_params = params.require(:user).permit(:name, :email_address, :password, :password_confirmation)
|
||||
def admin_flag = params.dig(:user, :admin) == "1"
|
||||
end
|
||||
@@ -0,0 +1,74 @@
|
||||
class AircraftController < ApplicationController
|
||||
include AircraftScoped
|
||||
skip_before_action :set_aircraft, only: %i[index new create]
|
||||
before_action :require_owner!, only: %i[edit update destroy]
|
||||
|
||||
def index
|
||||
@aircraft_list = current_user.accessible_aircraft.ordered.with_attached_picture
|
||||
end
|
||||
|
||||
def show
|
||||
@airworthiness = @aircraft.airworthiness
|
||||
@active_squawks = @aircraft.squawks.active.by_priority.includes(:component, :reported_by)
|
||||
@recent_events = @aircraft.events.includes(:user).limit(8)
|
||||
@notes = @aircraft.notes.recent_first.includes(:user).limit(5)
|
||||
@oil = ConsumptionStats.new(@aircraft, "oil")
|
||||
@fuel = ConsumptionStats.new(@aircraft, "fuel")
|
||||
end
|
||||
|
||||
def new
|
||||
@aircraft = Aircraft.new
|
||||
end
|
||||
|
||||
def create
|
||||
@aircraft = Aircraft.new(aircraft_params)
|
||||
Aircraft.transaction do
|
||||
@aircraft.save!
|
||||
@aircraft.roles.create!(user: current_user, role: "owner")
|
||||
@aircraft.log_event!("aircraft", "Aircraft created")
|
||||
end
|
||||
redirect_to @aircraft, notice: "#{@aircraft.tail_number} added."
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @aircraft.update(aircraft_params)
|
||||
log_event("aircraft", "Aircraft details updated")
|
||||
redirect_to @aircraft, notice: "Aircraft updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@aircraft.destroy!
|
||||
redirect_to root_path, notice: "#{@aircraft.tail_number} deleted."
|
||||
end
|
||||
|
||||
# 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: "Hours updated (#{delta >= 0 ? '+' : ''}#{delta})."
|
||||
rescue AircraftGroundedError => e
|
||||
redirect_to @aircraft, alert: e.message
|
||||
rescue ArgumentError
|
||||
redirect_to @aircraft, alert: "Enter a valid tach reading."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def aircraft_param = params[:id]
|
||||
|
||||
def aircraft_params
|
||||
params.require(:aircraft).permit(
|
||||
:tail_number, :make, :model, :serial_number, :description, :purchased, :status,
|
||||
:tach_time, :tach_time_offset, :hobbs_time, :hobbs_time_offset,
|
||||
:airworthiness_enforcement, :picture
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,54 @@
|
||||
# Owner-only: manage who can access the aircraft, and its public share links.
|
||||
class AircraftRolesController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!
|
||||
|
||||
def index
|
||||
@roles = @aircraft.roles.includes(:user).order(:role, :created_at)
|
||||
@share_tokens = @aircraft.share_tokens.order(created_at: :desc).includes(:created_by)
|
||||
end
|
||||
|
||||
def create
|
||||
user = User.find_by(email_address: params[:email_address].to_s.strip.downcase)
|
||||
unless user
|
||||
return redirect_to aircraft_aircraft_roles_path(@aircraft),
|
||||
alert: "No user with that email. Ask an administrator to create their account first."
|
||||
end
|
||||
role = @aircraft.roles.new(user: user, role: params[:role])
|
||||
if role.save
|
||||
log_event("role", "#{user.display_name} added as #{role.role}")
|
||||
redirect_to aircraft_aircraft_roles_path(@aircraft), notice: "#{user.display_name} added."
|
||||
else
|
||||
redirect_to aircraft_aircraft_roles_path(@aircraft), alert: role.errors.full_messages.to_sentence
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
role = @aircraft.roles.find(params[:id])
|
||||
if demoting_last_owner?(role, params[:role])
|
||||
return redirect_to aircraft_aircraft_roles_path(@aircraft), alert: "An aircraft must keep at least one owner."
|
||||
end
|
||||
role.update!(role: params[:role])
|
||||
log_event("role", "#{role.user.display_name} changed to #{role.role}")
|
||||
redirect_to aircraft_aircraft_roles_path(@aircraft), notice: "Role updated."
|
||||
end
|
||||
|
||||
def destroy
|
||||
role = @aircraft.roles.find(params[:id])
|
||||
if role.user == current_user
|
||||
return redirect_to aircraft_aircraft_roles_path(@aircraft), alert: "You can't remove yourself."
|
||||
end
|
||||
if demoting_last_owner?(role, nil)
|
||||
return redirect_to aircraft_aircraft_roles_path(@aircraft), alert: "An aircraft must keep at least one owner."
|
||||
end
|
||||
role.destroy!
|
||||
log_event("role", "#{role.user.display_name} removed")
|
||||
redirect_to aircraft_aircraft_roles_path(@aircraft), notice: "Access removed."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def demoting_last_owner?(role, new_role)
|
||||
role.role == "owner" && new_role != "owner" && @aircraft.roles.owners.count <= 1
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,74 @@
|
||||
class AirworthinessDirectivesController < ApplicationController
|
||||
include AircraftScoped
|
||||
include Applicability
|
||||
before_action :require_owner!, except: :index
|
||||
before_action :set_ad, only: %i[edit update destroy]
|
||||
|
||||
def index
|
||||
@rows = Compliance.ad_rows(@aircraft)
|
||||
@history = AdCompliance.for_aircraft(@aircraft).recent_first.includes(:airworthiness_directive, :component)
|
||||
@attachable = AirworthinessDirective.where.not(id: @aircraft.applicable_ads.select(:id)).ordered if owner?
|
||||
end
|
||||
|
||||
def new
|
||||
@ad = AirworthinessDirective.new
|
||||
@applies_to_airframe = true
|
||||
end
|
||||
|
||||
def create
|
||||
@ad = AirworthinessDirective.new(ad_params)
|
||||
AirworthinessDirective.transaction do
|
||||
@ad.save!
|
||||
apply_applicability(@ad, to_airframe: applies_to_airframe?, component_ids: applicable_component_ids)
|
||||
end
|
||||
log_event("ad", "#{@ad.bulletin_label} added: #{@ad.name}")
|
||||
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "#{@ad.bulletin_label} added."
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
@applies_to_airframe = applies_to_airframe?
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
# Link an AD that already exists (e.g. tracked on another aircraft).
|
||||
def attach
|
||||
ad = AirworthinessDirective.find(params[:airworthiness_directive_id])
|
||||
ad.aircraft << @aircraft unless ad.aircraft.include?(@aircraft)
|
||||
log_event("ad", "#{ad.bulletin_label} linked: #{ad.name}")
|
||||
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "#{ad.name} linked."
|
||||
end
|
||||
|
||||
def edit
|
||||
@applies_to_airframe = @ad.aircraft.include?(@aircraft)
|
||||
end
|
||||
|
||||
def update
|
||||
AirworthinessDirective.transaction do
|
||||
@ad.update!(ad_params)
|
||||
apply_applicability(@ad, to_airframe: applies_to_airframe?, component_ids: applicable_component_ids)
|
||||
end
|
||||
log_event("ad", "#{@ad.bulletin_label} updated: #{@ad.name}")
|
||||
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "#{@ad.bulletin_label} updated."
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
@applies_to_airframe = applies_to_airframe?
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
# Removes the AD from this aircraft (the shared AD record survives if other aircraft use it).
|
||||
def destroy
|
||||
detach_from_aircraft(@ad)
|
||||
log_event("ad", "#{@ad.bulletin_label} removed: #{@ad.name}")
|
||||
redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "#{@ad.name} removed from #{@aircraft.tail_number}."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_ad = @ad = @aircraft.applicable_ads.find(params[:id])
|
||||
|
||||
def ad_params
|
||||
p = params.require(:airworthiness_directive).permit(
|
||||
:name, :short_description, :required_action, :compliance_type, :trigger_condition, :recurring,
|
||||
:recurring_hours, :recurring_months, :recurring_days, :bulletin_type, :mandatory, :document_id
|
||||
)
|
||||
p[:document_id] = scoped_id(@aircraft.documents, p[:document_id])
|
||||
p
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
default_form_builder AppFormBuilder
|
||||
include Authentication
|
||||
allow_browser versions: :modern
|
||||
|
||||
prepend_before_action :redirect_to_setup
|
||||
helper_method :current_user
|
||||
|
||||
rescue_from ActiveRecord::RecordNotFound do
|
||||
respond_to do |format|
|
||||
format.html { render "errors/not_found", status: :not_found, layout: (authenticated? ? "application" : "public") }
|
||||
format.any { head :not_found }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def current_user = Current.user
|
||||
|
||||
# First run: no users yet → create the initial admin.
|
||||
def redirect_to_setup
|
||||
redirect_to new_setup_path if !User.exists? && !is_a?(SetupController)
|
||||
end
|
||||
|
||||
def require_admin!
|
||||
redirect_to root_path, alert: "Admins only." unless current_user&.admin?
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,90 @@
|
||||
class ComponentsController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!, 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
|
||||
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", "Component added: #{@component}")
|
||||
redirect_to aircraft_components_path(@aircraft), notice: "Component added."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @component.update(component_params)
|
||||
log_event("component", "Component updated: #{@component}")
|
||||
redirect_to aircraft_components_path(@aircraft), notice: "Component updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@component.destroy!
|
||||
log_event("component", "Component deleted: #{@component}")
|
||||
redirect_to aircraft_components_path(@aircraft), notice: "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", "#{replaced ? 'Replaced' : 'Serviced'}: #{@component}")
|
||||
redirect_to aircraft_components_path(@aircraft), notice: "#{@component} #{replaced ? 'replaced' : 'serviced'}."
|
||||
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, :tbo_hours, :tbo_days, :inspection_hours,
|
||||
:inspection_days, :replacement_hours, :replacement_days, :tbo_critical, :on_condition,
|
||||
:inspection_critical, :replacement_critical
|
||||
).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
|
||||
Whitespace-only changes.
@@ -0,0 +1,46 @@
|
||||
# Scopes a controller to one aircraft the current user can access and exposes
|
||||
# role helpers. Unknown and inaccessible aircraft both 404 (enumeration-safe).
|
||||
#
|
||||
# Access matrix (mirrors the original):
|
||||
# admin – everything
|
||||
# owner – everything on their aircraft
|
||||
# pilot – view all; update hours; create squawks, notes, oil/fuel, flight logs
|
||||
module AircraftScoped
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
before_action :set_aircraft
|
||||
helper_method :current_role, :owner?, :can_write?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def aircraft_param = params[:aircraft_id]
|
||||
|
||||
def set_aircraft
|
||||
@aircraft = current_user.accessible_aircraft.find(aircraft_param)
|
||||
end
|
||||
|
||||
def current_role
|
||||
@current_role ||= current_user.role_for(@aircraft)
|
||||
end
|
||||
|
||||
# Owner-level write access (admins included).
|
||||
def owner? = current_role.in?(%w[admin owner])
|
||||
alias_method :can_write?, :owner?
|
||||
|
||||
def require_owner!
|
||||
return if owner?
|
||||
redirect_back fallback_location: aircraft_path(@aircraft),
|
||||
alert: "Only owners can do that."
|
||||
end
|
||||
|
||||
# Returns id only if it belongs to the given (aircraft-scoped) relation; else nil.
|
||||
def scoped_id(scope, id)
|
||||
id.present? && scope.where(id: id).exists? ? id : nil
|
||||
end
|
||||
|
||||
def log_event(category, name, notes: nil)
|
||||
@aircraft.log_event!(category, name, notes: notes)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,38 @@
|
||||
# ADs and inspection types are shared reference data that can apply to many
|
||||
# aircraft. From an aircraft's page we only ever touch *this* aircraft's links:
|
||||
# the airframe itself and/or some of its components.
|
||||
module Applicability
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
private
|
||||
|
||||
def apply_applicability(record, to_airframe:, component_ids:)
|
||||
mine = @aircraft.components.where(id: Array(component_ids).compact_blank).to_a
|
||||
others = record.components.where.not(aircraft_id: @aircraft.id).to_a
|
||||
record.components = others + mine
|
||||
if to_airframe
|
||||
record.aircraft << @aircraft unless record.aircraft.include?(@aircraft)
|
||||
else
|
||||
record.aircraft.delete(@aircraft)
|
||||
end
|
||||
# Never let a record silently drop off this aircraft entirely.
|
||||
record.aircraft << @aircraft if mine.empty? && !to_airframe
|
||||
end
|
||||
|
||||
def detach_from_aircraft(record)
|
||||
record.aircraft.delete(@aircraft)
|
||||
record.components.delete(*record.components.where(aircraft_id: @aircraft.id))
|
||||
record.destroy if record.aircraft.empty? && record.components.empty? && orphan_deletable?(record)
|
||||
end
|
||||
|
||||
def orphan_deletable?(record)
|
||||
case record
|
||||
when AirworthinessDirective then record.ad_compliances.none?
|
||||
when InspectionType then record.inspection_records.none?
|
||||
else false
|
||||
end
|
||||
end
|
||||
|
||||
def applies_to_airframe? = params[:applies_to_airframe] == "1"
|
||||
def applicable_component_ids = params[:component_ids]
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
module Authentication
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
before_action :require_authentication
|
||||
helper_method :authenticated?
|
||||
end
|
||||
|
||||
class_methods do
|
||||
def allow_unauthenticated_access(**options)
|
||||
skip_before_action :require_authentication, **options
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def authenticated?
|
||||
resume_session
|
||||
end
|
||||
|
||||
def require_authentication
|
||||
resume_session || request_authentication
|
||||
end
|
||||
|
||||
def resume_session
|
||||
Current.session ||= find_session_by_cookie
|
||||
end
|
||||
|
||||
def find_session_by_cookie
|
||||
Session.find_by(id: cookies.signed[:session_id]) if cookies.signed[:session_id]
|
||||
end
|
||||
|
||||
def request_authentication
|
||||
session[:return_to_after_authenticating] = request.url
|
||||
redirect_to new_session_path
|
||||
end
|
||||
|
||||
def after_authentication_url
|
||||
session.delete(:return_to_after_authenticating) || root_url
|
||||
end
|
||||
|
||||
def start_new_session_for(user)
|
||||
user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session|
|
||||
Current.session = session
|
||||
cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax }
|
||||
end
|
||||
end
|
||||
|
||||
def terminate_session
|
||||
Current.session.destroy
|
||||
cookies.delete(:session_id)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,53 @@
|
||||
class ConsumableRecordsController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!, only: %i[edit update destroy]
|
||||
before_action :set_record, only: %i[edit update destroy]
|
||||
|
||||
def index
|
||||
@oil = ConsumptionStats.new(@aircraft, "oil")
|
||||
@fuel = ConsumptionStats.new(@aircraft, "fuel")
|
||||
@records = @aircraft.consumable_records.recent_first.limit(200)
|
||||
end
|
||||
|
||||
def new
|
||||
type = params[:type].in?(ConsumableRecord::TYPES.keys) ? params[:type] : "oil"
|
||||
@record = @aircraft.consumable_records.new(record_type: type, date: Date.current,
|
||||
flight_hours: @aircraft.tach_time)
|
||||
end
|
||||
|
||||
def create
|
||||
@record = @aircraft.consumable_records.new(record_params)
|
||||
if @record.save
|
||||
log_event(@record.record_type, "#{@record.record_type.capitalize} added: #{@record.quantity_added} #{@record.unit}")
|
||||
redirect_to aircraft_consumable_records_path(@aircraft), notice: "Record added."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @record.update(record_params)
|
||||
log_event(@record.record_type, "#{@record.record_type.capitalize} record updated")
|
||||
redirect_to aircraft_consumable_records_path(@aircraft), notice: "Record updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@record.destroy!
|
||||
log_event(@record.record_type, "#{@record.record_type.capitalize} record deleted")
|
||||
redirect_to aircraft_consumable_records_path(@aircraft), notice: "Record deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_record = @record = @aircraft.consumable_records.find(params[:id])
|
||||
|
||||
def record_params
|
||||
params.require(:consumable_record).permit(:record_type, :date, :quantity_added, :level_after,
|
||||
:consumable_type, :flight_hours, :notes, :excluded_from_averages)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,41 @@
|
||||
class DocumentCollectionsController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!
|
||||
before_action :set_collection, only: %i[edit update destroy]
|
||||
|
||||
def new
|
||||
@collection = @aircraft.document_collections.new
|
||||
end
|
||||
|
||||
def create
|
||||
@collection = @aircraft.document_collections.new(collection_params)
|
||||
if @collection.save
|
||||
log_event("document", "Collection created: #{@collection.name}")
|
||||
redirect_to aircraft_documents_path(@aircraft), notice: "Collection created."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @collection.update(collection_params)
|
||||
log_event("document", "Collection updated: #{@collection.name}")
|
||||
redirect_to aircraft_documents_path(@aircraft), notice: "Collection updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@collection.destroy!
|
||||
log_event("document", "Collection deleted: #{@collection.name}")
|
||||
redirect_to aircraft_documents_path(@aircraft), notice: "Collection and its documents deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_collection = @collection = @aircraft.document_collections.find(params[:id])
|
||||
def collection_params = params.require(:document_collection).permit(:name, :description, :visibility, :starred)
|
||||
end
|
||||
@@ -0,0 +1,62 @@
|
||||
class DocumentsController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!, except: %i[index show]
|
||||
before_action :set_document, only: %i[show edit update destroy]
|
||||
|
||||
def index
|
||||
@collections = @aircraft.document_collections.ordered.includes(documents: { pages_attachments: :blob })
|
||||
@uncollected = @aircraft.documents.where(document_collection_id: nil).ordered.includes(pages_attachments: :blob)
|
||||
end
|
||||
|
||||
def show
|
||||
@page = (params[:page] || 1).to_i.clamp(1, [ @document.pages.size, 1 ].max)
|
||||
end
|
||||
|
||||
def new
|
||||
@document = @aircraft.documents.new(document_collection_id: params[:collection_id])
|
||||
end
|
||||
|
||||
def create
|
||||
@document = @aircraft.documents.new(document_params)
|
||||
if @document.save
|
||||
log_event("document", "Document added: #{@document.name}")
|
||||
redirect_to aircraft_document_path(@aircraft, @document), notice: "Document added."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
attrs = document_params
|
||||
new_pages = attrs.delete(:pages)
|
||||
@document.assign_attributes(attrs)
|
||||
@document.pages.attach(new_pages) if new_pages.present?
|
||||
Array(params[:remove_page_ids]).each { |id| @document.pages.find_by(id: id)&.purge_later }
|
||||
if @document.save
|
||||
log_event("document", "Document updated: #{@document.name}")
|
||||
redirect_to aircraft_document_path(@aircraft, @document), notice: "Document updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@document.destroy!
|
||||
log_event("document", "Document deleted: #{@document.name}")
|
||||
redirect_to aircraft_documents_path(@aircraft), notice: "Document deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_document = @document = @aircraft.documents.find(params[:id])
|
||||
|
||||
def document_params
|
||||
p = params.require(:document).permit(:name, :description, :doc_type, :visibility, :document_collection_id, pages: [])
|
||||
p[:pages]&.compact_blank!
|
||||
p.delete(:pages) if p[:pages].blank?
|
||||
p[:document_collection_id] = scoped_id(@aircraft.document_collections, p[:document_collection_id])
|
||||
p
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
class EventsController < ApplicationController
|
||||
include AircraftScoped
|
||||
|
||||
def index
|
||||
@category = params[:category].presence_in(AircraftEvent::CATEGORIES.keys)
|
||||
scope = @aircraft.events.includes(:user)
|
||||
scope = scope.where(category: @category) if @category
|
||||
@events = scope.limit(200)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,47 @@
|
||||
class FlightLogsController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!, only: :destroy
|
||||
|
||||
def index
|
||||
@flight_logs = @aircraft.flight_logs.recent_first.includes(:user)
|
||||
end
|
||||
|
||||
def show
|
||||
@flight_log = @aircraft.flight_logs.find(params[:id])
|
||||
end
|
||||
|
||||
def new
|
||||
@flight_log = @aircraft.flight_logs.new(date: Date.current, tach_out: @aircraft.tach_time,
|
||||
hobbs_out: @aircraft.hobbs_time)
|
||||
end
|
||||
|
||||
def create
|
||||
@flight_log = @aircraft.flight_logs.new(flight_log_params)
|
||||
if @flight_log.valid?
|
||||
HoursService.new(@aircraft).log_flight!(@flight_log)
|
||||
redirect_to aircraft_flight_logs_path(@aircraft), notice: "Flight logged (#{@flight_log.tach_time} hrs)."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
rescue AircraftGroundedError => e
|
||||
flash.now[:alert] = e.message
|
||||
render :new, status: :conflict
|
||||
end
|
||||
|
||||
# Deleting a flight does not roll back hours; correct those with an hours update.
|
||||
def destroy
|
||||
@aircraft.flight_logs.find(params[:id]).destroy!
|
||||
log_event("flight", "Flight log deleted")
|
||||
redirect_to aircraft_flight_logs_path(@aircraft), notice: "Flight log deleted. Aircraft hours were not changed."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def flight_log_params
|
||||
params.require(:flight_log).permit(
|
||||
:date, :tach_time, :tach_out, :tach_in, :hobbs_time, :hobbs_out, :hobbs_in,
|
||||
:departure_location, :destination_location, :route, :oil_added, :oil_added_type,
|
||||
:oil_level_after, :fuel_added, :fuel_added_type, :fuel_level_after, :notes, :track_log
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,51 @@
|
||||
class InspectionRecordsController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!
|
||||
before_action :set_record, only: %i[edit update destroy]
|
||||
|
||||
def new
|
||||
@record = InspectionRecord.new(aircraft: @aircraft, date: Date.current,
|
||||
aircraft_hours: @aircraft.tach_time,
|
||||
inspection_type_id: params[:inspection_type_id])
|
||||
end
|
||||
|
||||
def create
|
||||
@record = InspectionRecord.new(record_params.merge(aircraft: @aircraft))
|
||||
if @record.save
|
||||
log_event("inspection", "Inspection completed: #{@record.inspection_type.name}")
|
||||
redirect_to aircraft_inspections_path(@aircraft), notice: "Inspection recorded."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @record.update(record_params)
|
||||
log_event("inspection", "Inspection record updated: #{@record.inspection_type.name}")
|
||||
redirect_to aircraft_inspections_path(@aircraft), notice: "Inspection record updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@record.destroy!
|
||||
log_event("inspection", "Inspection record deleted: #{@record.inspection_type.name}")
|
||||
redirect_to aircraft_inspections_path(@aircraft), notice: "Inspection record deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_record = @record = InspectionRecord.for_aircraft(@aircraft).find(params[:id])
|
||||
|
||||
def record_params
|
||||
p = params.require(:inspection_record).permit(:inspection_type_id, :date, :aircraft_hours, :notes,
|
||||
:logbook_entry_id, :component_id)
|
||||
p[:inspection_type_id] = scoped_id(@aircraft.applicable_inspection_types, p[:inspection_type_id])
|
||||
p[:logbook_entry_id] = scoped_id(@aircraft.logbook_entries, p[:logbook_entry_id])
|
||||
p[:component_id] = scoped_id(@aircraft.components, p[:component_id])
|
||||
p
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,56 @@
|
||||
class InspectionTypesController < ApplicationController
|
||||
include AircraftScoped
|
||||
include Applicability
|
||||
before_action :require_owner!
|
||||
before_action :set_type, only: %i[edit update destroy]
|
||||
|
||||
def new
|
||||
@inspection_type = InspectionType.new(required: true, recurring: true)
|
||||
@applies_to_airframe = true
|
||||
end
|
||||
|
||||
def create
|
||||
@inspection_type = InspectionType.new(type_params)
|
||||
InspectionType.transaction do
|
||||
@inspection_type.save!
|
||||
apply_applicability(@inspection_type, to_airframe: applies_to_airframe?, component_ids: applicable_component_ids)
|
||||
end
|
||||
log_event("inspection", "Inspection type added: #{@inspection_type.name}")
|
||||
redirect_to aircraft_inspections_path(@aircraft), notice: "Inspection type added."
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
@applies_to_airframe = applies_to_airframe?
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
def edit
|
||||
@applies_to_airframe = @inspection_type.aircraft.include?(@aircraft)
|
||||
end
|
||||
|
||||
def update
|
||||
InspectionType.transaction do
|
||||
@inspection_type.update!(type_params)
|
||||
apply_applicability(@inspection_type, to_airframe: applies_to_airframe?, component_ids: applicable_component_ids)
|
||||
end
|
||||
log_event("inspection", "Inspection type updated: #{@inspection_type.name}")
|
||||
redirect_to aircraft_inspections_path(@aircraft), notice: "Inspection type updated."
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
@applies_to_airframe = applies_to_airframe?
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
def destroy
|
||||
detach_from_aircraft(@inspection_type)
|
||||
log_event("inspection", "Inspection type removed: #{@inspection_type.name}")
|
||||
redirect_to aircraft_inspections_path(@aircraft), notice: "Inspection removed from #{@aircraft.tail_number}."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_type
|
||||
@inspection_type = @aircraft.applicable_inspection_types.find(params[:id])
|
||||
end
|
||||
|
||||
def type_params
|
||||
params.require(:inspection_type).permit(:name, :recurring, :required, :recurring_hours, :recurring_days, :recurring_months)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
class InspectionsController < ApplicationController
|
||||
include AircraftScoped
|
||||
|
||||
def index
|
||||
@rows = Compliance.inspection_rows(@aircraft)
|
||||
@history = InspectionRecord.for_aircraft(@aircraft).recent_first.includes(:inspection_type, :component, :logbook_entry)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,57 @@
|
||||
class LogbookEntriesController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!, except: %i[index show]
|
||||
before_action :set_entry, only: %i[show edit update destroy]
|
||||
|
||||
def index
|
||||
@query = params[:q]
|
||||
@log_type = params[:log_type].presence
|
||||
scope = @aircraft.logbook_entries.search(@query).recent_first.includes(:document)
|
||||
scope = scope.where(log_type: @log_type) if @log_type
|
||||
@entries = scope.limit(500)
|
||||
end
|
||||
|
||||
def show; end
|
||||
|
||||
def new
|
||||
@entry = @aircraft.logbook_entries.new(date: Date.current, aircraft_hours_at_entry: @aircraft.tach_time)
|
||||
end
|
||||
|
||||
def create
|
||||
@entry = @aircraft.logbook_entries.new(entry_params)
|
||||
if @entry.save
|
||||
log_event("logbook", "Logbook entry added (#{@entry.date})")
|
||||
redirect_to aircraft_logbook_entries_path(@aircraft), notice: "Logbook entry added."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @entry.update(entry_params)
|
||||
log_event("logbook", "Logbook entry updated (#{@entry.date})")
|
||||
redirect_to aircraft_logbook_entry_path(@aircraft, @entry), notice: "Logbook entry updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@entry.destroy!
|
||||
log_event("logbook", "Logbook entry deleted (#{@entry.date})")
|
||||
redirect_to aircraft_logbook_entries_path(@aircraft), notice: "Logbook entry deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_entry = @entry = @aircraft.logbook_entries.find(params[:id])
|
||||
|
||||
def entry_params
|
||||
p = params.require(:logbook_entry).permit(:log_type, :entry_type, :date, :text, :signoff_person,
|
||||
:signoff_location, :aircraft_hours_at_entry, :document_id, :page_number)
|
||||
p[:document_id] = scoped_id(@aircraft.documents, p[:document_id])
|
||||
p
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,59 @@
|
||||
class MajorRecordsController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!, except: %i[index show]
|
||||
before_action :set_record, only: %i[show edit update destroy]
|
||||
|
||||
def index
|
||||
@records = @aircraft.major_records.recent_first.includes(:component)
|
||||
end
|
||||
|
||||
def show; end
|
||||
|
||||
def new
|
||||
@record = @aircraft.major_records.new(record_type: params[:type] || "repair", date_performed: Date.current,
|
||||
aircraft_hours: @aircraft.tach_time)
|
||||
end
|
||||
|
||||
def create
|
||||
@record = @aircraft.major_records.new(record_params)
|
||||
if @record.save
|
||||
log_event("major_record", "#{@record.type_label} added: #{@record.title}")
|
||||
redirect_to aircraft_major_records_path(@aircraft), notice: "Record added."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @record.update(record_params)
|
||||
log_event("major_record", "#{@record.type_label} updated: #{@record.title}")
|
||||
redirect_to aircraft_major_record_path(@aircraft, @record), notice: "Record updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@record.destroy!
|
||||
log_event("major_record", "#{@record.type_label} deleted: #{@record.title}")
|
||||
redirect_to aircraft_major_records_path(@aircraft), notice: "Record deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_record = @record = @aircraft.major_records.find(params[:id])
|
||||
|
||||
def record_params
|
||||
p = params.require(:major_record).permit(
|
||||
:record_type, :title, :description, :date_performed, :performed_by, :component_id,
|
||||
:form_337_document_id, :stc_number, :stc_holder, :stc_document_id, :logbook_entry_id,
|
||||
:aircraft_hours, :has_ica, :ica_notes, :notes
|
||||
)
|
||||
p[:component_id] = scoped_id(@aircraft.components, p[:component_id])
|
||||
p[:logbook_entry_id] = scoped_id(@aircraft.logbook_entries, p[:logbook_entry_id])
|
||||
%i[form_337_document_id stc_document_id].each { |k| p[k] = scoped_id(@aircraft.documents, p[k]) }
|
||||
p
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,45 @@
|
||||
class NotesController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!, only: %i[edit update destroy]
|
||||
before_action :set_note, only: %i[edit update destroy]
|
||||
|
||||
def index
|
||||
@notes = @aircraft.notes.recent_first.includes(:user)
|
||||
end
|
||||
|
||||
def new
|
||||
@note = @aircraft.notes.new
|
||||
end
|
||||
|
||||
def create
|
||||
@note = @aircraft.notes.new(note_params.merge(user: current_user))
|
||||
if @note.save
|
||||
log_event("note", "Note added")
|
||||
redirect_to aircraft_notes_path(@aircraft), notice: "Note added."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @note.update(note_params.merge(edited_at: Time.current))
|
||||
log_event("note", "Note updated")
|
||||
redirect_to aircraft_notes_path(@aircraft), notice: "Note updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@note.destroy!
|
||||
log_event("note", "Note deleted")
|
||||
redirect_to aircraft_notes_path(@aircraft), notice: "Note deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_note = @note = @aircraft.notes.find(params[:id])
|
||||
def note_params = params.require(:aircraft_note).permit(:text, :public)
|
||||
end
|
||||
@@ -0,0 +1,33 @@
|
||||
class PasswordsController < ApplicationController
|
||||
allow_unauthenticated_access
|
||||
before_action :set_user_by_token, only: %i[ edit update ]
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
if user = User.find_by(email_address: params[:email_address])
|
||||
PasswordsMailer.reset(user).deliver_later
|
||||
end
|
||||
|
||||
redirect_to new_session_path, notice: "Password reset instructions sent (if user with that email address exists)."
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def update
|
||||
if @user.update(params.permit(:password, :password_confirmation))
|
||||
redirect_to new_session_path, notice: "Password has been reset."
|
||||
else
|
||||
redirect_to edit_password_path(params[:token]), alert: "Passwords did not match."
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def set_user_by_token
|
||||
@user = User.find_by_password_reset_token!(params[:token])
|
||||
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
||||
redirect_to new_password_path, alert: "Password reset link is invalid or has expired."
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,16 @@
|
||||
class ProfilesController < ApplicationController
|
||||
def edit
|
||||
@user = current_user
|
||||
end
|
||||
|
||||
def update
|
||||
@user = current_user
|
||||
attrs = params.require(:user).permit(:name, :email_address, :password, :password_confirmation)
|
||||
attrs = attrs.except(:password, :password_confirmation) if attrs[:password].blank?
|
||||
if @user.update(attrs)
|
||||
redirect_to root_path, notice: "Profile updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
class SessionsController < ApplicationController
|
||||
allow_unauthenticated_access only: %i[ new create ]
|
||||
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_url, alert: "Try again later." }
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
if user = User.authenticate_by(params.permit(:email_address, :password))
|
||||
start_new_session_for user
|
||||
redirect_to after_authentication_url
|
||||
else
|
||||
redirect_to new_session_path, alert: "Try another email address or password."
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
terminate_session
|
||||
redirect_to new_session_path
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
class SetupController < ApplicationController
|
||||
allow_unauthenticated_access
|
||||
before_action { redirect_to root_path if User.exists? }
|
||||
layout "public"
|
||||
|
||||
def new
|
||||
@user = User.new
|
||||
end
|
||||
|
||||
def create
|
||||
@user = User.new(params.require(:user).permit(:name, :email_address, :password, :password_confirmation))
|
||||
@user.admin = true
|
||||
if @user.save
|
||||
start_new_session_for @user
|
||||
redirect_to root_path, notice: "Welcome! You're the administrator. Add your first aircraft."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
class ShareTokensController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!
|
||||
|
||||
def create
|
||||
token = @aircraft.share_tokens.new(params.require(:share_token).permit(:label, :privilege, :expires_at))
|
||||
token.created_by = current_user
|
||||
if token.save
|
||||
log_event("aircraft", "Share link created (#{token.privilege})", notes: token.label)
|
||||
redirect_to aircraft_aircraft_roles_path(@aircraft), notice: "Share link created."
|
||||
else
|
||||
redirect_to aircraft_aircraft_roles_path(@aircraft), alert: token.errors.full_messages.to_sentence
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
token = @aircraft.share_tokens.find(params[:id])
|
||||
token.destroy!
|
||||
log_event("aircraft", "Share link revoked", notes: token.label)
|
||||
redirect_to aircraft_aircraft_roles_path(@aircraft), notice: "Share link revoked."
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,30 @@
|
||||
# Read-only public view of an aircraft via a share link — no login required.
|
||||
# 'status' links see current state; 'maintenance' links also see full history.
|
||||
class SharedController < ApplicationController
|
||||
allow_unauthenticated_access
|
||||
skip_before_action :redirect_to_setup
|
||||
layout "public"
|
||||
|
||||
def show
|
||||
@share = ShareToken.find_active(params[:token]) or raise ActiveRecord::RecordNotFound
|
||||
@aircraft = @share.aircraft
|
||||
@airworthiness = @aircraft.airworthiness
|
||||
@components = @aircraft.components.where.not(status: "DISPOSED").includes(:component_type).order(:install_location)
|
||||
@active_squawks = @aircraft.squawks.active.by_priority
|
||||
@notes = @aircraft.notes.visible_publicly.recent_first
|
||||
@ad_rows = Compliance.ad_rows(@aircraft)
|
||||
@inspection_rows = Compliance.inspection_rows(@aircraft)
|
||||
@oil = ConsumptionStats.new(@aircraft, "oil")
|
||||
@fuel = ConsumptionStats.new(@aircraft, "fuel")
|
||||
@documents = @aircraft.documents.includes(:document_collection, pages_attachments: :blob)
|
||||
.select { |d| d.visible_to_share?(@share) }
|
||||
|
||||
if @share.maintenance?
|
||||
@logbook = @aircraft.logbook_entries.recent_first
|
||||
@resolved_squawks = @aircraft.squawks.resolved.order(resolved_at: :desc)
|
||||
@major_records = @aircraft.major_records.recent_first
|
||||
@inspection_history = InspectionRecord.for_aircraft(@aircraft).recent_first.includes(:inspection_type)
|
||||
@ad_history = AdCompliance.for_aircraft(@aircraft).recent_first.includes(:airworthiness_directive)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,62 @@
|
||||
class SquawksController < ApplicationController
|
||||
include AircraftScoped
|
||||
# Pilots may create squawks; editing/resolving needs an owner.
|
||||
before_action :require_owner!, except: %i[index new create]
|
||||
before_action :set_squawk, only: %i[edit update destroy resolve reopen]
|
||||
|
||||
def index
|
||||
@active = @aircraft.squawks.active.by_priority.includes(:component, :reported_by)
|
||||
@resolved = @aircraft.squawks.resolved.order(resolved_at: :desc).includes(:component).limit(100)
|
||||
end
|
||||
|
||||
def new
|
||||
@squawk = @aircraft.squawks.new(priority: 1)
|
||||
end
|
||||
|
||||
def create
|
||||
@squawk = @aircraft.squawks.new(squawk_params.merge(reported_by: current_user))
|
||||
if @squawk.save
|
||||
log_event("squawk", "Squawk reported: #{@squawk.priority_label}", notes: @squawk.issue_reported.truncate(200))
|
||||
redirect_to aircraft_squawks_path(@aircraft), notice: "Squawk reported."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @squawk.update(squawk_params)
|
||||
log_event("squawk", "Squawk updated")
|
||||
redirect_to aircraft_squawks_path(@aircraft), notice: "Squawk updated."
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def resolve
|
||||
@squawk.resolve!(notes: params[:resolution_notes])
|
||||
log_event("squawk", "Squawk resolved", notes: @squawk.issue_reported.truncate(200))
|
||||
redirect_back fallback_location: aircraft_squawks_path(@aircraft), notice: "Squawk resolved."
|
||||
end
|
||||
|
||||
def reopen
|
||||
@squawk.update!(resolved: false, resolved_at: nil)
|
||||
log_event("squawk", "Squawk reopened")
|
||||
redirect_to aircraft_squawks_path(@aircraft), notice: "Squawk reopened."
|
||||
end
|
||||
|
||||
def destroy
|
||||
@squawk.destroy!
|
||||
log_event("squawk", "Squawk deleted")
|
||||
redirect_to aircraft_squawks_path(@aircraft), notice: "Squawk deleted."
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_squawk = @squawk = @aircraft.squawks.find(params[:id])
|
||||
|
||||
def squawk_params
|
||||
params.require(:squawk).permit(:priority, :issue_reported, :component_id, :notes, :attachment)
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user