Created
March 21, 2012 17:50
-
-
Save vpamulap/2150185 to your computer and use it in GitHub Desktop.
how do I test this?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ShoppingList < ActiveRecord::Base | |
belongs_to :user | |
has_many :shopping_list_ingredients | |
has_many :ingredients, through: :shopping_list_ingredients | |
serialize :current_mqrs | |
# updates the cart in the case that the meal queue has changed | |
def update_cart | |
self.current_mqrs = [] if current_mqrs == nil | |
mqrs = self.user.meal_queue.meal_queue_recipes | |
slis = self.shopping_list_ingredients | |
mqr_ids = mqrs.map(&:id) | |
new_mqr_ids = mqr_ids - current_mqrs | |
deleted_mqrs = current_mqrs - mqr_ids | |
# check if meal queue has changed | |
if mqr_ids.to_set != current_mqrs.to_set | |
# if it has changed | |
# find deleted mqrs | |
# subtract quantities from items of deleted mqrs | |
self.shopping_list_ingredients.each do |sli| | |
sli_mqrs = sli.get_mqrs | |
logger.info "ingredient is:" | |
logger.info sli_mqrs.to_s | |
sli_mqr_ids = sli_mqrs.map do |mqr| | |
logger | |
mqr.fetch("id") | |
end | |
# find deleted mqrs in sli mqrs | |
del_mqrs = sli_mqr_ids - (current_mqrs - deleted_mqrs) | |
# subtract quantity of each deleted mqr from ingredient | |
del_mqrs.each do |del_mqr| | |
sli.quantity -= get_quantity(sli_mqrs, del_mqr) | |
# if quantity is 0, remove item | |
if sli.quantity > 0.0 | |
sli_mqrs = sli.meal_queue_recipes | |
sli_mqrs.delete_if { |sli_mqr| sli_mqr["id"] == del_mqr } | |
sli.meal_queue_recipes = sli_mqrs | |
sli.save | |
else | |
sli.destroy | |
end | |
end | |
end | |
self.shopping_list_ingredients(true) | |
# find new mqrs | |
new_mqrs = [] | |
mqrs.each do |mqr| | |
new_mqrs << mqr if new_mqr_ids.include?(mqr.id) | |
end | |
# add new ingredients / quantities | |
new_mqrs.each do |mqr| | |
mqr.recipe.recipe_ingredients.each do |ri| | |
i = ri.ingredient.id | |
new_sli = ShoppingListIngredient.find_or_create_by_shopping_list_id_and_ingredient_id(self.id, i) | |
if new_sli.quantity == nil | |
# new sli | |
new_sli.quantity = ri.quantity | |
new_sli.meal_queue_recipes = [{"quantity" => ri.quantity, "id" => mqr.id}] | |
new_sli.save | |
self.shopping_list_ingredients << new_sli | |
else | |
# existing sli | |
new_sli.quantity += ri.quantity | |
sli_mqrs = new_sli.meal_queue_recipes | |
sli_mqrs << {"quantity" => ri.quantity, "id" => mqr.id} | |
new_sli.meal_queue_recipes = sli_mqrs | |
new_sli.save | |
end | |
end | |
end | |
end | |
cmqrs = self.current_mqrs | |
cmqrs = cmqrs.concat(new_mqr_ids) | |
cmqrs.delete_if { |cmqr| deleted_mqrs.include?(cmqr) } | |
self.current_mqrs = cmqrs | |
self.save | |
self.shopping_list_ingredients(true) | |
end | |
private | |
# returns the quantity of an shopping_list_ingredient from a meal queue recipe | |
def get_quantity(sli_mqrs, mqr_id) | |
sli_mqrs.each do |mqr| | |
return mqr["quantity"] if mqr["id"] == mqr_id | |
end | |
return nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment