Skip to content

Commit

Permalink
Issue #112 - adds check on ingredient creation to make sure it's a va…
Browse files Browse the repository at this point in the history
…lid unit
  • Loading branch information
johnhutch committed Jun 14, 2017
1 parent 9cc3d59 commit 5fe43ef
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions app/models/ingredient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fe43ef

Please sign in to comment.