Skip to content

Commit

Permalink
Fix shopping list controller and view
Browse files Browse the repository at this point in the history
Update styles
  • Loading branch information
PabloBona committed Oct 12, 2023
1 parent 485981d commit ea042f9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 98 deletions.
81 changes: 27 additions & 54 deletions app/controllers/general_shopping_lists_controller.rb
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
31 changes: 9 additions & 22 deletions app/views/general_shopping_lists/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
<%= render partial: 'devise/shared/navbar' %>
<div class="container mt-5">
<h2 class="text-center my-3">General Shopping List</h2>
<% total_quantity = 0 %>
<% total_price = 0 %>
<% @recipes.each do |recipe| %>
<% recipe.foods.each do |food| %>
<% total_quantity += food.quantity %>
<% total_price += food.price * food.quantity%>
<% end %>
<% end %>

<div class="d-flex justify-content-between mt-3">
<div class="d-flex">
<p class="fw-bold me-3">Total Amount of Food Needed:</p>
<% @remaining_stock.each do |food_name, quantity| %>
<div class="text-danger px-3"><%= "#{food_name}: #{quantity.to_i} Units" %></div>
<% end %>
<p class="fw-bold me-3">Amount of Food Item To Buy: <%= @shopping_list.count %> </p>
</div>
<div class="d-flex">
<p class="fw-bold me-3">Total Value of Food Needed:</p>
Expand All @@ -29,19 +18,17 @@
<thead class="table-primary">
<tr>
<th>Food</th>
<th>Quantity In Stock</th>
<th>Price Per Unit</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<% @recipes.each do |recipe| %>
<% recipe.foods.each_with_index do |food, index| %>
<tr class="<%= index.odd? ? 'table-warning' : '' %>">
<td><%= food.name %></td>
<td><%= food.quantity %></td>
<td>$<%= food.price %></td>
</tr>
<% end %>
<% @shopping_list.each do |item| %>
<tr>
<td><%= item[1][:name] %></td> <!-- Access 'name' from the hash using item[1][:name] -->
<td><%= item[1][:quantity].to_i %></td>
<td>$<%= item[1][:total_price].to_i %></td>
</tr>
<% end %>
</tbody>
</table>
Expand Down
17 changes: 7 additions & 10 deletions app/views/public_recipes/_recipe_card.html.erb
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>
16 changes: 6 additions & 10 deletions app/views/recipes/_recipe_card.html.erb
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>
4 changes: 2 additions & 2 deletions app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<%= render 'devise/shared/navbar' %>
<section class="p-3 text-center d-flex flex-column row-gap-3">
<div class="container">
<div class="container mt-5">
<div class="row">
<div class="col">
<h1><%= @user.name %>'s Recipes</h1>
<h2><%= @user.name %>'s Recipes</h2>
<span class="d-flex justify-content-end m-3"><%= link_to 'Create new recipe', new_recipe_path, class: 'btn btn-primary' %></span>
</div>
<div>
Expand Down

0 comments on commit ea042f9

Please sign in to comment.