18 lines
449 B
Ruby
18 lines
449 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 = UnitConversion::density? unit
|
||
|
rescue UnitConversion::UnparseableUnitError => e
|
||
|
valid = false
|
||
|
msg = e.message
|
||
|
end
|
||
|
|
||
|
unless valid
|
||
|
record.errors[attribute] << (options[:message] || msg)
|
||
|
end
|
||
|
end
|
||
|
end
|