-
Notifications
You must be signed in to change notification settings - Fork 21
Description
I have logic in place to prevent my users from trying to convert between incompatible units. For example: pounds --> gallons. I currently throw an error message, which explains that incompatible unit conversion is probably the issue. But I can't be certain, because the way I'm validating is by simply attempting the conversion and rescueing if it fails:
begin
Measurement.parse("#{self.quantity} #{self.unit}").convert_to(self.ingredient.unit)
rescue
errors.add(:unit, "is incompatible with associated ingredient unit (#{ingredient.unit}) in: #{ingredient.recipe.name}. You are probably trying to mix weight and volume units.")
end
So this same validation error could also occur if a user attempted a nonsense unit. Ex: 37 blrblbs
I'm trying to figure out a way to be even more clear, and say something like:
"Impossible conversion! Pounds are a unit of weight but gallons are a unit of volume..."
I suspect that the library is already tracking this classification of units somewhere, but it does not appear to be exposed anywhere in the API? Would this be hard to do?