18 lines
433 B
Ruby
18 lines
433 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[attribute] << (options[:message] || msg)
|
|
end
|
|
end
|
|
end |