parsley/app/models/concerns/density_validator.rb
Dan Elbert 3b4134f684 units
2016-01-14 15:22:15 -06:00

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