From 9cc3d59910a9ba980c2099065768e12064e65cf5 Mon Sep 17 00:00:00 2001 From: John Hutchinson Date: Tue, 13 Jun 2017 22:09:32 -0400 Subject: [PATCH] Issue #112 - restructured add/sub functions in grocery list, moving to ingredient model, avoids checking compatibility for non-united ingredients. --- app/models/grocery_list.rb | 27 ++++++------------------ app/models/ingredient.rb | 43 +++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/app/models/grocery_list.rb b/app/models/grocery_list.rb index 727987c..481fca5 100644 --- a/app/models/grocery_list.rb +++ b/app/models/grocery_list.rb @@ -11,16 +11,8 @@ def add_ingredient(add_i) self.ingredients.each do |list_i| if list_i.matched?(add_i) - if list_i.unitless? || add_i.unitless? - if list_i.unitless? && add_i.unitless? - list_i.amount = sum_i(list_i, add_i) - return list_i.save - end - else - if list_i.unitized_amount.compatible?(add_i.unitized_amount) - list_i.amount = Unit.new( list_i.unitized_amount + add_i.unitized_amount ).scalar - return list_i.save - end + if list_i.sum(add_i) + return true end end end @@ -29,23 +21,16 @@ def add_ingredient(add_i) def subtract_ingredient(sub_i) # search for the ingredient by name, then subtract the recipe's amount from the grocery list - # if amount = 0, remove entirely + # if resultant amount would be <= 0, remove entirely self.ingredients.each do |list_i| if list_i.matched?(sub_i) - if (list_i.unitized_amount > sub_i.unitized_amount) && list_i.unitized_amount.compatible?(sub_i.unitized_amount) - list_i.amount = Unit.new( list_i.unitized_amount - sub_i.unitized_amount ).scalar - return list_i.save - else - return self.ingredients.delete(list_i) + if list_i.sub(sub_i) + return true end end + return self.ingredients.delete(list_i) end end - private - - def sum_i(list_i, add_i) - Unit.new( list_i.unitized_amount + add_i.unitized_amount ).scalar - end end diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index 6d0308c..18dd8f5 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -46,10 +46,51 @@ def matched?(ing) return self.name.singularize == ing.name.singularize end + def sum(ing) + # verify that ingredients are compatibly unit'd (or not unit'd) + # could probably be combined with sub(ing) with the math operator sent as a + # param but I don't know how to do that. + + if self.unitless? || ing.unitless? + if self.unitless? && ing.unitless? + self.amount = Unit.new( self.unitized_amount + ing.unitized_amount ).scalar + return self.save + end + else + if self.unitized_amount.compatible?(ing.unitized_amount) + self.amount = Unit.new( self.unitized_amount + ing.unitized_amount ).scalar + return self.save + end + end + return false + end + + def sub(ing) + # verify that ingredients are compatibly unit'd (or not unit'd) and then subtract + # could probably be combined with sum(ing) with the math operator sent as a param + # but I don't know how to do that. + + if self.unitless? || ing.unitless? + if self.unitless? && ing.unitless? + if (self.amount > ing.amount) + self.amount = Unit.new( self.unitized_amount - ing.unitized_amount ).scalar + return self.save + end + end + else + if self.unitized_amount.compatible?(ing.unitized_amount) + if (self.unitized_amount > ing.unitized_amount) + self.amount = Unit.new( self.unitized_amount - ing.unitized_amount ).scalar + return self.save + end + end + end + end + def ingreedy_parse(ing_string) begin parsed_ing = Ingreedy.parse(ing_string) - rescue + 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