parsley/app/models/task_list.rb

35 lines
958 B
Ruby
Raw Normal View History

2018-08-27 17:46:33 -05:00
class TaskList < ApplicationRecord
belongs_to :user
has_many :task_items, dependent: :delete_all
validates :name,
presence: true,
uniqueness: { case_sensitive: false }
scope :for_user, -> (user) { where(user_id: user) }
2021-05-09 23:31:44 -05:00
def add_recipe_ingredients(recipe, recurse_depth = 0)
if recurse_depth > 10
raise "This shouldn't be. Did you make a recipe loop?"
end
2022-02-02 15:55:24 -06:00
2021-05-09 23:31:44 -05:00
recipe.recipe_ingredients.each do |ri|
if ri.ingredient.is_a?(Recipe)
add_recipe_ingredients(ri.ingredient, recurse_depth + 1)
else
2022-02-02 15:55:24 -06:00
item = self.task_items.detect { |i| i.name.downcase == ri.name.downcase } || task_items.build(name: ri.name)
2021-05-09 23:31:44 -05:00
quantity_str = [ri.quantity, ri.units].delete_if { |i| i.blank? }.join(' ')
if item.quantity.blank?
item.quantity = quantity_str
else
item.quantity += (', ' + quantity_str)
end
2022-02-02 15:55:24 -06:00
2021-05-09 23:31:44 -05:00
item.save
end
end
end
2022-02-02 15:55:24 -06:00
2018-08-27 17:46:33 -05:00
end