Files
car-tracker/app/controllers/vehicles_controller.rb
T
bionickatana 0826d267f2
CI / scan_ruby (push) Failing after 1m5s
CI / scan_js (push) Failing after 6s
CI / lint (push) Failing after 1m29s
Finished phase005
2026-09-16 15:34:50 -06:00

82 lines
1.8 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.order(:maintenance_type)
@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
end