Files
bionickatana 87d2141ed0
CI / scan_ruby (push) Failing after 7s
CI / scan_js (push) Failing after 7s
CI / lint (push) Failing after 8s
Finished phase006
2026-09-16 16:37:03 -06:00

99 lines
2.4 KiB
Ruby

class VehiclesController < ApplicationController
before_action :set_vehicle, only: %i[show edit update destroy]
def index
@status = params[:status]
@vehicles = vehicles_for_status(@status).order(:make, :model, :year)
end
def show
prepare_show
end
def new
@vehicle = Vehicle.new
end
def create
@vehicle = Vehicle.new(vehicle_params)
if @vehicle.save
redirect_to @vehicle, notice: "Vehicle created."
else
render :new, status: :unprocessable_entity
end
end
def edit
end
def update
if @vehicle.update(vehicle_params)
redirect_to @vehicle, notice: "Vehicle updated."
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@vehicle.update(active: false)
redirect_to vehicles_path, notice: "Vehicle deactivated."
end
private
def set_vehicle
@vehicle = Vehicle.find(params[:id])
end
def vehicles_for_status(status)
case status
when "inactive"
Vehicle.where(active: false)
when "all"
Vehicle.all
else
Vehicle.where(active: true)
end
end
def vehicle_params
params.require(:vehicle).permit(
:make,
:model,
:year,
:color,
:vin,
:licence_plate,
:current_odometer,
:fuel_tank_size,
:active
)
end
def prepare_show
@maintenance_schedules = @vehicle.maintenance_schedules.active.order(:maintenance_type)
@maintenance_schedule_statuses = maintenance_schedule_statuses(@maintenance_schedules)
@fuel_entries = @vehicle.fuel_entries.includes(:updated_by_user).order(date: :desc, created_at: :desc)
@maintenance_entries = @vehicle.maintenance_entries.includes(:updated_by_user).order(date: :desc, created_at: :desc)
@fuel_entry ||= @vehicle.fuel_entries.build(date: Date.current)
@maintenance_entry ||= @vehicle.maintenance_entries.build(date: Date.current)
end
def maintenance_schedule_statuses(schedules)
schedules.index_with do |schedule|
latest_entry = latest_maintenance_entry_for(schedule)
status = MaintenanceScheduleStatus.new(schedule, latest_entry, current_odometer: @vehicle.current_odometer)
MaintenanceNotification.record_status_change(schedule, status)
status
end
end
def latest_maintenance_entry_for(schedule)
@vehicle.maintenance_entries
.where("lower(name) = ?", schedule.maintenance_type.downcase)
.order(date: :desc, created_at: :desc)
.first
end
end