Class: VehicleModelValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- VehicleModelValidator
- Defined in:
- lib/vehicles/validators/vehicle_model_validator.rb
Overview
Drop-in validator: ‘validates :model, vehicle_model: { make: :make }`.
Checks that the value is a real model OF the make held in another attribute. Pass ‘make:` pointing at the attribute that holds the make (defaults to :make).
validates :model, vehicle_model: { make: :car_make }
Defensive by design: blank model passes; an unknown/blank make can’t disprove the model, so it passes (let the make’s own validator flag that); errors never raise out of a form submission.
Instance Method Summary collapse
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/vehicles/validators/vehicle_model_validator.rb', line 14 def validate_each(record, attribute, value) return if value.blank? make_attribute = [:make] || :make make_value = record.respond_to?(make_attribute) ? record.public_send(make_attribute) : nil make = Vehicles.make(make_value) return if make.nil? # unknown make -> defer to the make validator return if make.model(value) record.errors.add(attribute, [:message] || "is not a recognized #{make.name} model") rescue StandardError => e Vehicles.logger&.error("[vehicles] vehicle_model validation error: #{e.}") record.errors.add(attribute, [:message] || "is not a recognized vehicle model") end |