35 lines
982 B
Ruby
35 lines
982 B
Ruby
class TaskList < ApplicationRecord
|
|
|
|
belongs_to :user
|
|
has_many :task_items, dependent: :delete_all, inverse_of: :task_list
|
|
|
|
validates :name,
|
|
presence: true,
|
|
uniqueness: { case_sensitive: false }
|
|
|
|
scope :for_user, -> (user) { where(user_id: user) }
|
|
|
|
def add_recipe_ingredients(recipe, recurse_depth = 0)
|
|
if recurse_depth > 10
|
|
raise "This shouldn't be. Did you make a recipe loop?"
|
|
end
|
|
|
|
recipe.recipe_ingredients.each do |ri|
|
|
if ri.ingredient.is_a?(Recipe)
|
|
add_recipe_ingredients(ri.ingredient, recurse_depth + 1)
|
|
else
|
|
item = self.task_items.detect { |i| i.name.downcase == ri.name.downcase } || task_items.build(name: ri.name)
|
|
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
|
|
|
|
item.save
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|