-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix shopping list controller and view
Update styles
- Loading branch information
Showing
5 changed files
with
51 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,40 @@ | ||
class GeneralShoppingListsController < ApplicationController | ||
def index | ||
@recipes = current_user.recipes.includes(:foods, :recipe_foods).all | ||
@remaining_stock = calculate_remaining_stock(@recipes) | ||
@total_quantity = calculate_total_quantity(@recipes) | ||
@total_price = calculate_total_price(@recipes, @remaining_stock) | ||
end | ||
|
||
private | ||
|
||
def calculate_stock_food_price(recipes) | ||
stock_food_price = 0 | ||
recipes.each do |recipe| | ||
recipe.recipe_foods.each do |recipe_food| | ||
food = Food.find_by(id: recipe_food.food_id) | ||
stock_food_price += food.price * food.quantity if food | ||
end | ||
required_foods = calculate_required_foods | ||
|
||
@total_price = 0 | ||
@shopping_list = {} | ||
required_foods.each do |food_id, quantity| | ||
food = Food.find_by(id: food_id) | ||
next unless food.quantity < quantity | ||
|
||
final_quantity = quantity - food.quantity | ||
@total_price += final_quantity * food.price | ||
|
||
@shopping_list[food_id] = { | ||
name: food.name, | ||
quantity: final_quantity, | ||
unit_price: food.price, | ||
total_price: final_quantity * food.price | ||
} | ||
end | ||
stock_food_price | ||
end | ||
|
||
def calculate_total_quantity(recipes) | ||
total_quantity = 0 | ||
recipes.each do |recipe| | ||
recipe.recipe_foods.each do |recipe_food| | ||
total_quantity += recipe_food.quantity | ||
end | ||
end | ||
total_quantity | ||
end | ||
|
||
def calculate_total_price(recipes, remaining_stock) | ||
total_price = 0 | ||
recipes.each do |recipe| | ||
recipe.recipe_foods.each do |recipe_food| | ||
food = Food.find_by(id: recipe_food.food_id) | ||
next unless food | ||
|
||
required_quantity = recipe_food.quantity | ||
available_quantity = [required_quantity, remaining_stock[food.name]].min | ||
total_price += food.price * available_quantity | ||
end | ||
end | ||
total_price | ||
end | ||
|
||
def calculate_remaining_stock(recipes) | ||
remaining_stock = {} | ||
private | ||
|
||
recipes.each do |recipe| | ||
def calculate_required_foods | ||
required_foods = {} | ||
@recipes.each do |recipe| | ||
recipe.recipe_foods.each do |recipe_food| | ||
food = Food.find_by(id: recipe_food.food_id) | ||
next unless food | ||
|
||
required_quantity = recipe_food.quantity | ||
available_quantity = food.quantity | ||
remaining_stock[food.name] = if available_quantity < required_quantity | ||
required_quantity - available_quantity | ||
else | ||
available_quantity - required_quantity | ||
end | ||
if required_foods[recipe_food.food_id].nil? | ||
required_foods[recipe_food.food_id] = recipe_food.quantity | ||
else | ||
required_foods[recipe_food.food_id] += recipe_food.quantity | ||
end | ||
end | ||
end | ||
|
||
remaining_stock | ||
required_foods | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
<div class="bg-warning-subtle border border-black column-gap-3 d-flex justify-content-evenly my-3 p-3 rounded-3"> | ||
<div class="bg-warning-subtle border border-black column-gap-3 d-flex justify-content-evenly my-3 p-3 rounded-3 align-items-center"> | ||
<div class="fs-4"> | ||
<%= link_to recipe.name, recipe_path(recipe.id) %> | ||
</spam> <br> By: <%= recipe.user.name %> | ||
<br> By: <%= recipe.user.name %> | ||
</div> | ||
<div> | ||
<ul> | ||
<div class=""> | ||
<% total_price = 0 %> | ||
<% food_count = recipe.foods.count %> | ||
<% recipe.foods.each do |food| %> | ||
<p><% food.price %></p> | ||
<% total_price += food.price %> | ||
<% recipe.recipe_foods.each do |recipe_food| %> | ||
<% total_price += recipe_food.quantity * recipe_food.food.price %> | ||
<% end %> | ||
</ul> | ||
<span>Total Food Items: <%= food_count %></span> <br> | ||
<span>Total Price: <%= total_price %></span> | ||
<span>Total Food Items: <%= food_count %></span> <br> | ||
<span>Total Price: $<%= total_price %></span> | ||
</div> | ||
</div> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
<div class="container"> | ||
<div class="row"> | ||
<div class="border border-black d-flex p-3 rounded-3 m-3"> | ||
<div class="align-items-center col-6 d-flex flex-column fs-3"> | ||
<%= link_to recipe.name, recipe_path(recipe.id) %> | ||
<%= delete_button(recipe) %> | ||
</div> | ||
<div class="fs-5 align-items-center"><%= trim_text(recipe.description) %></div> | ||
</div> | ||
<div class="border border-black d-flex p-3 rounded-3 m-3"> | ||
<div class="align-items-center col-6 d-flex flex-column fs-3"> | ||
<%= link_to recipe.name, recipe_path(recipe.id) %> | ||
<%= delete_button(recipe) %> | ||
</div> | ||
</div> | ||
<div class="fs-5 align-items-center"><%= trim_text(recipe.description) %></div> | ||
</div> |
This file contains 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