18 lines
444 B
Ruby
18 lines
444 B
Ruby
class DensityValidator < ActiveModel::EachValidator
|
|
def validate_each(record, attribute, value)
|
|
valid = false
|
|
msg = 'is not a unit of density'
|
|
|
|
begin
|
|
unit = UnitConversion::parse(value)
|
|
valid = unit.density?
|
|
rescue UnitConversion::UnparseableUnitError => e
|
|
valid = false
|
|
msg = e.message
|
|
end
|
|
|
|
unless valid
|
|
record.errors.add(attribute, message: (options[:message] || msg))
|
|
end
|
|
end
|
|
end |