Skip to content

Commit

Permalink
Merge pull request #37 from beteland123/food_list
Browse files Browse the repository at this point in the history
Food list, shopping view
  • Loading branch information
beteland123 authored Sep 27, 2023
2 parents c366843 + 6699ec3 commit 18dd5fc
Show file tree
Hide file tree
Showing 11 changed files with 3,397 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
node_modules/
7 changes: 7 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@
*
*= require_tree .
*= require_self
https://www.freepik.com/free-photos-vectors/recipe-ingredients
https://img.freepik.com/premium-photo/various-fresh-vegetables-herbs-healthy-eating-concept_51524-4701.jpg?size=626&ext=jpg&ga=GA1.1.1216120637.1695808448&semt=ais
background-image: url('https://img.freepik.com/free-photo/top-view-fresh-vegetables-various-spices-small-bowls-wooden-spoons-table-free-space_140725-147473.jpg?size=626&ext=jpg&ga=GA1.1.1216120637.1695808448&semt=ais');
*/

/* background-image: url('https://img.freepik.com/premium-photo/cooking-ingredient-white-table-background_798854-433.jpg?size=626&ext=jpg&ga=GA1.1.1216120637.1695808448&semt=ais');
background-repeat: no-repeat;
background-size: cover; */
31 changes: 29 additions & 2 deletions app/controllers/foods_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class FoodsController < ApplicationController
before_action :authenticate_user!, except: [:index]
before_action :authenticate_user!
def index
@foods = current_user.foods
end
Expand All @@ -14,7 +14,7 @@ def create

if @food.save
flash[:success] = 'Food saved successfully'
redirect_to foods_url
redirect_to foods_path
else
flash.now[:error] = 'Error: Food could not be saved'
render 'new'
Expand All @@ -28,6 +28,33 @@ def destroy
redirect_to foods_url
end

def general_shopping_list
@recipes = Recipe.where(user: current_user)
@general_food_list = Food.where(user: current_user)
@missing_food_items = []
total_food_items = 0
total_price = 0

@recipes.each do |recipe|
recipe.recipe_foods.each do |recipe_food|
general_food = @general_food_list.find_by(id: recipe_food.food_id)

next unless general_food.nil? || general_food.quantity < recipe_food.quantity

@missing_food_items << {
food_name: recipe_food.food.name,
quantity_needed: recipe_food.quantity,
price: recipe_food.food.price
}
total_food_items += recipe_food.quantity
total_price += (recipe_food.food.price * recipe_food.quantity)
end
end

@total_food_items = total_food_items
@total_price = total_price
end

private

def food_params
Expand Down
19 changes: 19 additions & 0 deletions app/views/foods/general_shopping_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1>General Shopping List</h1>

<% if @missing_food_items.any? %>
<p>Amount of food to buy: <%= @total_food_items %></p>
<p>Total value of food need: <%= @total_price %></p>
<ul>
<% @missing_food_items.each do |item| %>
<li>
Food Name: <%= item[:food_name] %><br>
Quantity Needed: <%= item[:quantity_needed] %><br>
Price: <%= item[:price] %>
</li>
<% end %>
</ul>


<% else %>
<p>No missing food items.</p>
<% end %>
29 changes: 27 additions & 2 deletions app/views/foods/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<p>index page</p>
<h2>Food index page</h2>
<div class='sign_in_user_bar'>
<% unless user_signed_in? %>
<div class="user-sign-in-link">
Expand All @@ -10,4 +10,29 @@
<%= link_to 'Sign out', custom_destroy_user_session_path, method: :delete %>
</div>
<% end %>
</div>
</div>
<%= link_to 'Add Food', 'foods/new', class: "add-button"%>
<% if @foods.any? %>
<table>
<thead>
<tr>
<th>Food </th>
<th>Measurement unit</th>
<th>Unit price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<% @foods.each do |food| %>
<td><%= food.name %></td>
<td><%= food.measurement_unit %></td>
<td><%= food.price %></td>
<td><%= button_to "Delete Food", food_path(food), method: :delete, data: { confirm: "Are you sure?" } %></td>
<% end %>
</tr>
</tbody>
</table>
<% else %>
<p>There is no food you have.</p>
<% end %>
20 changes: 20 additions & 0 deletions app/views/foods/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1>New Food</h1>
<div class="container">
<%= form_with model: @food, url: foods_path, method: :post, local: true do |f| %>
<div>
<%= f.label :name %>
<%= f.text_field :name, placeholder: "Chicken breasts" %>
</div>
<div>
<%= f.label :measurement_unit %>
<%= f.select :measurement_unit, options_for_select(["grams", "ounces", "pounds", "kilograms", "milligrams","Unit"]), placeholder: "Select a measurement unit" %>
</div>
<div>
<%= f.label :price, "Price in $" %>
<%= f.text_field :price, placeholder: "$ Enter the price" %>
</div>
<div>
<%= f.submit "Add food" %>
</div>
<% end %>
</div>
13 changes: 13 additions & 0 deletions app/views/recipe_foods/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1 class="title">Edit recipe food</h1>

<div>
<%= form_with model: @recipe_food, url: recipe_recipe_food_path(@recipe.id, @recipe_food.id), class: "form-container", method: :patch do |f| %>
<div >
<%= f.label :quantity %>
<%= f.number_field :quantity, placeholder: 'New Quantity', min: 0 %>
</div>
<div>
<%= f.submit 'Update quantity' %>
</div>
<% end %>
</div>
20 changes: 20 additions & 0 deletions app/views/recipe_foods/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class='container'>
<h2>Add a new ingredient</h2>
<%= form_with(model: [@recipe, @recipe_food], url: recipe_recipe_foods_path(@recipe), local: true) do |f| %>

<div class='field'>
<%= f.label :food_id, 'Select food item' %>
<%= f.collection_select(:food_id, available_foods, :id, :name, {}, {}) %>
</div>
<div class='field'>
<%= f.label :quantity %>
<%= f.text_field :quantity %>
</div>

<%= form.hidden_field :recipe_id, value: recipe.id %>

<div>
<%= form.submit "Add ingredient" %>
</div>
<% end %>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
resources :foods, only: [ :index, :new, :create, :destroy ]
resources :users
resources :recipes
get '/general_shopping_list', to: 'foods#general_shopping_list', as: 'general_shopping_list'

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

Expand Down
Loading

0 comments on commit 18dd5fc

Please sign in to comment.