22 lines
873 B
Ruby
22 lines
873 B
Ruby
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 }
|
|
validates :baseline_odometer, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
|
validates :baseline_date, presence: true
|
|
validates :active, inclusion: { in: [ true, false ] }
|
|
validate :baseline_date_cannot_be_in_the_future
|
|
|
|
scope :active, -> { where(active: true) }
|
|
|
|
private
|
|
|
|
def baseline_date_cannot_be_in_the_future
|
|
return if baseline_date.blank? || baseline_date <= Date.current
|
|
|
|
errors.add(:baseline_date, "can't be in the future")
|
|
end
|
|
end
|