2016-01-14 15:22:15 -06:00
|
|
|
class DensityValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
valid = false
|
|
|
|
msg = 'is not a unit of density'
|
|
|
|
|
|
|
|
begin
|
|
|
|
unit = UnitConversion::parse(value)
|
2016-02-02 15:48:20 -06:00
|
|
|
valid = unit.density?
|
2016-01-14 15:22:15 -06:00
|
|
|
rescue UnitConversion::UnparseableUnitError => e
|
|
|
|
valid = false
|
|
|
|
msg = e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
unless valid
|
|
|
|
record.errors[attribute] << (options[:message] || msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|