Files
car-tracker/app/models/vehicle.rb
T
2026-09-15 20:28:07 -06:00

23 lines
944 B
Ruby

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