phase002 complete

This commit is contained in:
2026-09-15 20:28:07 -06:00
parent 9793cd08bc
commit a3a65c24ed
28 changed files with 656 additions and 13 deletions
+18
View File
@@ -0,0 +1,18 @@
class FuelEntry < ApplicationRecord
belongs_to :vehicle
belongs_to :updated_by_user, class_name: "User"
validates :odometer, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :gallons_pumped, presence: true, numericality: { greater_than: 0 }
validates :price_paid, presence: true, numericality: { greater_than: 0 }
validates :date, presence: true
validate :date_cannot_be_in_the_future
private
def date_cannot_be_in_the_future
return if date.blank? || date <= Date.current
errors.add(:date, "can't be in the future")
end
end
+30
View File
@@ -0,0 +1,30 @@
class MaintenanceEntry < ApplicationRecord
belongs_to :vehicle
belongs_to :updated_by_user, class_name: "User"
validates :name, :notes, :date, presence: true
validates :odometer, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :cost, presence: true, numericality: { greater_than_or_equal_to: 0 }
validate :date_cannot_be_in_the_future
validate :name_matches_vehicle_schedule
private
def date_cannot_be_in_the_future
return if date.blank? || date <= Date.current
errors.add(:date, "can't be in the future")
end
def name_matches_vehicle_schedule
return if vehicle.blank? || name.blank?
scheduled = vehicle.maintenance_schedules.any? do |schedule|
schedule.maintenance_type.casecmp?(name)
end
scheduled ||= vehicle.maintenance_schedules.where("lower(maintenance_type) = ?", name.downcase).exists? if vehicle.persisted?
return if scheduled
errors.add(:name, "must match a maintenance schedule for the vehicle")
end
end
+7
View File
@@ -0,0 +1,7 @@
class MaintenanceSchedule < ApplicationRecord
belongs_to :vehicle
validates :maintenance_type, presence: true, uniqueness: { scope: :vehicle_id, case_sensitive: false }
validates :mileage_interval, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :time_interval, presence: true, numericality: { only_integer: true, greater_than: 0 }
end
+15
View File
@@ -0,0 +1,15 @@
class User < ApplicationRecord
has_many :fuel_entries, foreign_key: :updated_by_user_id, dependent: :restrict_with_error, inverse_of: :updated_by_user
has_many :maintenance_entries, foreign_key: :updated_by_user_id, dependent: :restrict_with_error, inverse_of: :updated_by_user
before_validation :normalize_email
validates :name, presence: true
validates :email, presence: true, uniqueness: { case_sensitive: false }
private
def normalize_email
self.email = email.to_s.strip.downcase if email.present?
end
end
+22
View File
@@ -0,0 +1,22 @@
class Vehicle < ApplicationRecord
has_many :maintenance_schedules, dependent: :restrict_with_error
has_many :fuel_entries, dependent: :restrict_with_error
has_many :maintenance_entries, dependent: :restrict_with_error
before_validation :normalize_vin
validates :make, :model, :color, :vin, :licence_plate, presence: true
validates :vin, uniqueness: true
validates :licence_plate, uniqueness: { case_sensitive: false }
validates :year, presence: true,
numericality: { only_integer: true, greater_than_or_equal_to: 1980, less_than_or_equal_to: ->(_vehicle) { Date.current.year } }
validates :current_odometer, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :fuel_tank_size, presence: true, numericality: { greater_than: 0 }
validates :active, inclusion: { in: [ true, false ] }
private
def normalize_vin
self.vin = vin.to_s.strip.upcase if vin.present?
end
end