From 5fe43efc307622494cd962edf56f149c0aed0108 Mon Sep 17 00:00:00 2001 From: John Hutchinson Date: Tue, 13 Jun 2017 22:33:37 -0400 Subject: [PATCH] Issue #112 - adds check on ingredient creation to make sure it's a valid unit --- app/models/ingredient.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index 18dd8f5..69cd435 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -88,18 +88,33 @@ def sub(ing) end def ingreedy_parse(ing_string) + + # ingreedy chokes if you give it an ingredient string that doesn't contain a + # quantity. so let's handle that so our app doesn't crash begin parsed_ing = Ingreedy.parse(ing_string) rescue - # ingreedy chokes if you give it an ingredient string that doesn't contain a - # quantity. so let's handle that so our app doesn't crash parsed_ing = false end + # ruby-units chokes if it's not a recognized unit, and we currently have no way + # of checking valid units ahead of time. so we rescue the exception and handle + # the conditional after. + unity = true + begin + Unit.new(parsed_ing.amount.to_s + " " + parsed_ing.unit.to_s) + rescue + unity = false + end + if parsed_ing - self.name = parsed_ing.ingredient self.amount = parsed_ing.amount - self.unit = parsed_ing.unit + if unity + self.name = parsed_ing.ingredient + self.unit = parsed_ing.unit + else + self.name = parsed_ing.unit.to_s + " " + parsed_ing.ingredient.to_s + end else self.name = ing_string end