Skip to content

Commit

Permalink
modelled general shopping list
Browse files Browse the repository at this point in the history
  • Loading branch information
clintonjosephs committed May 18, 2022
1 parent cc8b6ac commit c2c5dda
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 8 deletions.
Binary file added app/assets/images/create-inventory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions app/controllers/foods_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,44 @@ def new
@food = Food.new
end

def shopping_list
@recipe = Recipe.find(params[:recipe_id])
@inventory = Inventory.find(params[:inventory_id])

# get all inventory foods
# get all recipe foods
# get all recipe foods that are not in inventory
# get all recipe foods that the quantity is higher than the inventory quantity
# a shopping list is made up of a combination of items that do not exist in the inventory and items that are not enough in the inventory

@recipe_foods = Food.all.joins('INNER JOIN recipe_foods ON foods.id = recipe_foods.food_id')
.order(created_at: :desc).select('foods.*, recipe_foods.quantity, recipe_foods.id as recipe_foods_id')
.where(recipe_foods: { recipe_id: params[:recipe_id] })

@inventory_foods = Food.all.joins('INNER JOIN inventory_foods ON foods.id = inventory_foods.food_id').select('foods.*, inventory_foods.quantity, inventory_foods.id as inventory_foods_id')
.where(inventory_foods: { inventory_id: params[:inventory_id] })

@recipe_food_with_quantity_greater_than_inventory_food_quantity = []
@recipe_foods.select do |recipe_food|
@inventory_foods.select do |inventory_food|
if recipe_food.id == inventory_food.id && recipe_food.quantity > inventory_food.quantity
recipe_food.quantity = recipe_food.quantity - inventory_food.quantity
@recipe_food_with_quantity_greater_than_inventory_food_quantity << recipe_food
end
end
end

@recipe_foods_not_in_inventory = @recipe_foods - @inventory_foods

@shopping_list = @recipe_foods_not_in_inventory + @recipe_food_with_quantity_greater_than_inventory_food_quantity

@cost = 0
@shopping_list.each do |list|
@cost += list.price * list.quantity
end

end

def create
new_food = Food.new(food_params)
new_food.user_id = current_user.id
Expand Down
1 change: 1 addition & 0 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def show
@recipe_foods = Food.all.joins('INNER JOIN recipe_foods ON foods.id = recipe_foods.food_id')
.order(created_at: :desc).select('foods.*, recipe_foods.quantity, recipe_foods.id as recipe_foods_id')
.where(recipe_foods: { recipe_id: params[:id] }).with_attached_image
@inventory_hash = Inventory.all.where(user_id: current_user.id).map { |inventory| [inventory.name, inventory.id] }
end

def new
Expand Down
1 change: 1 addition & 0 deletions app/models/food.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Food < ApplicationRecord
belongs_to :user
has_many :inventory_foods, dependent: :destroy
has_many :recipe_foods, dependent: :destroy
has_one_attached :image

validates :name, presence: true, length: { maximum: 100 }
Expand Down
47 changes: 47 additions & 0 deletions app/views/foods/shopping_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<%= link_to ' << Recipe', recipe_path(@recipe.id), class: "btn btn-outline-success mb-2" %>
<div class="card col-md-6 offset-md-3">
<div class="card-body">
<div class="container">
<p style="font-size: 30px; text-align:center; color: var(--primary-color)">Shopping list</p>
<div class="row">
<ul class="list-unstyled">
<li class="text-black"><%= @recipe.name %></li>
<li class="text-muted mt-1"><%= @inventory.name %></li>
<li class="text-black mt-1">Items to purchase: <%= @shopping_list.length %></li>
<li class="text-black mt-1">Total Cost: <%= "$#{@cost}" %></li>
</ul>
</div>
<div class="row">
<div class="col-xl-5">
<b>Item</b>
</div>
<div class="col-xl-5">
<b>Quantity</b>
</div>
<div class="col-xl-2">
<b class="float-end">Price </b>
</div>
<hr>
</div>
<% @shopping_list.each do |food| %>
<div class="row">
<div class="col-xl-5">
<p><%= food.name %></p>
</div>
<div class="col-xl-5">
<p><%= "#{food.quantity} #{food.measurement_unit}" %></p>
</div>
<div class="col-xl-2">
<p class="float-end"> $<%= food.quantity * food.price %> </p>
</div>
<hr>
</div>
<% end %>
<div class="text-center" style="margin-top: 10px;">
<a onclick="window.print(); return false;" style="cursor:pointer"><u class="text-primary"><i class="fa fa-print" aria-hidden="true"></i>Print</u></a>
<p>Thank you for using our product</p>
</div>

</div>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/inventories/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="row">
<div class="mb-3 col-md-8 offset-md-2 inventory-card shadow">
<img src="https://foodenginepos.com/img/features/im.png" class="card-img-top" alt="inventory-img">
<%= image_tag("create-inventory.png", class: "card-img-top") %>
<div class="card-body">
<h5 class="card-title">Add New Inventory</h5>
<p class="card-text">Use this to add storage for a complete list of food items available to make recipes </p>
Expand Down
8 changes: 5 additions & 3 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= link_to ' << Public Recipes', public_recipes_path, class: "btn btn-outline mb-2"%>
<%= link_to ' << Public Recipes', root_path, class: "btn btn-outline mb-2"%>
<%= link_to ' << Recipes', recipes_path, class: "btn btn-outline mb-2" if current_user %>
<div class="container">
<div class="card text-center ">
Expand All @@ -22,7 +22,9 @@
</div>
</div>
<div class="mt-4">
<%= link_to 'Generate shopping list', new_recipe_recipe_food_path(@recipe.id), class: "btn btn-outline-info mb-2 flex-start" %>
<button type="button"class='btn btn-outline-primary mb-2 flex-start' data-bs-toggle="modal" data-bs-target="#listBackdrop">
<i class="fa fa-shopping-basket" aria-hidden="true"></i> Generate shopping list
</button>
<span style="float: right">
<%= link_to '<i class="fa fa-plus" aria-hidden="true"></i> Add food ingredient'.html_safe, new_recipe_recipe_food_path(@recipe.id), class: "btn btn-outline-success mb-2 flex-end" %>
</span>
Expand Down Expand Up @@ -65,4 +67,4 @@
</tbody>
</table>
</div>

<%= render partial: "shared/shopping_list_setup", locals: { list: @inventory_hash, recipe: @recipe.id } %>
4 changes: 2 additions & 2 deletions app/views/shared/_recipe_display.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<div class="row g-0">
<div class="col-md-3">
<% if recipe.image.present? %>
<%= image_tag recipe.image, class: "card-img-top" %>
<%= image_tag recipe.image, class: "img-fluid" %>
<% else %>
<%= image_tag "no-food-image.png", class: "card-img-top recipe-image" %>
<%= image_tag "no-food-image.png", class: "img-fluid" %>
<% end %>
</div>
<div class="col-md-9">
Expand Down
26 changes: 26 additions & 0 deletions app/views/shared/_shopping_list_setup.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="modal fade" id="listBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="listBackdropLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="listBackdropLabel">
<i class="fa fa-shopping-basket" aria-hidden="true"></i>
Generating Shopping List <br />
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<%= form_with url: shopping_list_path, method: :get do |form| %>
<div class="modal-body">
<h6>Select Inventory to use</h6>
<%= form.select :inventory_id, list, {}, {:class => 'form-control'} %>
<%= form.hidden_field :recipe_id, value: recipe %>
</div>
<div class="modal-footer">
<%=button_tag type: 'submit', class: "btn btn-outline-success popover-test", title: "This gives you a list of items that you need to purchase to make your recipe based on store inventory", data: {disable_with: "Loading..."}, name: nil do %>
<i class="fa fa-list-ol" aria-hidden="true"></i> Get List
<% end %>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
<% end %>
</div>
</div>
</div>
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
resources :foods, only: [:index, :show, :new, :destroy, :create]

resources :publics, only: [:index]

get "/general_shopping_list", to: "foods#general_shopping_list"
get "/shopping_list", to: "foods#shopping_list"

end

0 comments on commit c2c5dda

Please sign in to comment.