Skip to content

Commit

Permalink
Resolve merging conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Enning94 committed Sep 7, 2023
2 parents 7a89241 + 1aabc76 commit 4c4231c
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 81 deletions.
21 changes: 21 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@ h1 {
p.alert {
font-size: larger;
}

hr {
width: auto;
height: 3.5px !important;
background-color: #000;
padding: 0;
margin-top: 0.25rem !important;
margin-bottom: 0 !important;
top: 20px;
border: none;
border-radius: 35%;
}

.no-record {
font-size: 1.35rem;
font-weight: 500;
line-height: 1.2;
margin-bottom: 0.5em;
margin-top: 45px;
color: blue;
}
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ class ApplicationController < ActionController::Base
before_action :authenticate_user!
before_action :configure_permitted_parameters, if: :devise_controller?

def after_sign_out_path_for(*)
new_user_session_path
end

protected

def configure_permitted_parameters
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/inventories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class InventoriesController < ApplicationController

def index
@inventories = current_user.inventories.includes(:inventory_foods)
flash.delete(:notice)
flash.delete(:notice) unless request.referrer == new_inventory_url
end

def show; end
Expand Down
50 changes: 29 additions & 21 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
class RecipesController < ApplicationController
load_and_authorize_resource

def index
@recipes = current_user.recipes
@recipes = current_user.recipes.includes(:recipe_foods)
flash.delete(:notice) unless request.referrer == new_recipe_url
end

def show
@recipe = Recipe.find(params[:id])
@recipe = current_user.recipes.find(params[:id])
end

def new
Expand All @@ -13,36 +16,41 @@ def new

def create
@recipe = Recipe.new(recipe_params)
@recipe.user = current_user
if @recipe.save
redirect_to @recipe, notice: 'Recipe was successfully created.'
else
puts @recipe.errors.full_messages
render :new
end
end
@recipe.user_id = current_user.id
@recipe.public = params[:public] == 'Public'

def update
@recipe = Recipe.find(params[:id])
if @recipe.update(recipe_params)
redirect_to @recipe, notice: 'Recipe was successfully updated.'
else
render :edit
respond_to do |format|
if @recipe.save
format.html { redirect_to recipes_path, notice: 'Recipe was successfully created.' }
format.json { render :show, status: :created, location: @recipe }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @recipe.errors, status: unprocessable_entity }
end
end
end

def edit; end
def update; end

def destroy
@recipe = Recipe.find(params[:id])
if @recipe.destroy
flash[:notice] = 'Recipe deleted!'
else
flash[:alert] = 'Error! Please try again later.'

respond_to do |format|
if can? :destroy, @recipe
@recipe.destroy
format.html { redirect_to recipes_path, notice: 'Recipe was successfully deleted.' }
format.json { head :no_content }
else
format.html { redirect_to recipes_path, alert: 'Recipe was not deleted.' }
format.json { render json: @recipe.errors, status: :unprocessable_entity }
end
end
end

private

def recipe_params
params.require(:recipe).permit(:name, :description, :preparation_time, :cooking_time, :public)
params.require(:recipe).permit(:name, :preparation_time, :cooking_time, :description, :public)
end
end
4 changes: 2 additions & 2 deletions app/models/inventory_food.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class InventoryFood < ApplicationRecord
belongs_to :inventory
belongs_to :food
belongs_to :Inventory, class_name: 'Inventory', foreign_key: 'inventory_id'
belongs_to :Food, class_name: 'Food', foreign_key: 'food_id'
end
7 changes: 6 additions & 1 deletion app/models/recipe.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
class Recipe < ApplicationRecord
belongs_to :user
belongs_to :User, class_name: 'User', foreign_key: 'user_id'
has_many :recipe_foods, dependent: :destroy
has_many :foods, through: :recipe_foods

validates :name, presence: true, length: { minimum: 3, maximum: 50 }
validates :description, presence: true, length: { minimum: 3, maximum: 500 }
validates :preparation_time, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :cooking_time, presence: true, numericality: { only_integer: true, greater_than: 0 }
end
4 changes: 2 additions & 2 deletions app/models/recipe_food.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class RecipeFood < ApplicationRecord
belongs_to :recipe
belongs_to :food
belongs_to :recipe, class_name: 'Recipe', foreign_key: 'recipe_id'
belongs_to :food, class_name: 'Food', foreign_key: 'food_id'
end
2 changes: 1 addition & 1 deletion app/views/home/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<li class="nav-item">
<% if current_page?(recipes_path) %>
<%= link_to "Create new Recipe", new_recipe_path, class: "nav-link" %>
<%= link_to "New Recipe", new_recipe_path, class: "nav-link" %>
<% else %>
<%= link_to "Recipes", recipes_path, class: "nav-link" %>
<% end %>
Expand Down
73 changes: 73 additions & 0 deletions app/views/recipes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<%= form_with(model: @recipe, url: recipes_path, local: true) do |form| %>
<div class = "mt-4 p-2">
<%= select_tag(:public, options_for_select([['Select Status', 'Status'], ['Public', 'Public'], ['Private', 'Private']], 'Status'),
class: 'form-select')
%>
<% if @recipe.errors.any? %>
<p class="text-danger mb-0">
<%= @recipe.errors[:status].first %>
</p>
<% end %>
</div>

<div class = "mt-2 p-2">
<%= form.text_field :name,
autofocus: true,
autocomplete: 'name',
placeholder: 'New inventory name',
class: 'form-control'
%>
<% if @recipe.errors.any? %>
<p class="text-danger mb-0">
<%= @recipe.errors[:name].first %>
</p>
<% end %>
</div>

<div class = "mt-2 p-2">
<%= form.number_field :preparation_time,
autofocus: true,
autocomplete: 'preparation_time',
placeholder: 'Preparation time in minutes',
class: 'form-control'
%>
<% if @recipe.errors.any? %>
<p class="text-danger mb-0">
<%= @recipe.errors[:preparation_time].first %>
</p>
<% end %>
</div>

<div class = "mt-2 p-2">
<%= form.number_field :cooking_time,
autofocus: true,
autocomplete: 'cooking_time',
placeholder: 'Cooking time in minutes',
class: 'form-control'
%>
<% if @recipe.errors.any? %>
<p class="text-danger mb-0">
<%= @recipe.errors[:cooking_time].first %>
</p>
<% end %>
</div>

<div class = "mt-2 p-2">
<%= form.text_area :description, rows: "5",
autofocus: true,
autocomplete: 'description',
placeholder: 'Description of the inventory',
class: 'form-control'
%>
<% if @recipe.errors.any? %>
<p class="text-danger mb-0">
<%= @recipe.errors[:description].first %>
</p>
<% end %>
</div>

<div class="actions text-center">
<%= form.submit class: 'btn btn-outline-dark btn-sm mt-4' %>
</div>
<% end %>

51 changes: 37 additions & 14 deletions app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
<h1> List of all recipes </h1>
<div class="container">
<div class="row">
<div class="col-md-12">

<ul>
<% @recipes.each do |recipe| %>
<div class ="recipe-wrapper">
<div class = "recipe-list">
<li class = "recipe-title">
<a href="/recipes/<%= recipe.id %>"><%=recipe.name%></a>
</li>
<%= button_to 'Remove', recipe_path(recipe), class: "remove-cmt-btn", method: :delete %>
</div>
<li class = "recipe-description"><%=recipe.description%></li>
</div>
<% end %>
</ul>
<div class="d-flex justify-content-end mt-2 mb-2 p-2">
<%= link_to 'New Recipe', new_recipe_path, class: "btn btn-primary text-right" %>
</div>

<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger">
<%= alert %>
</p>
<% elsif @recipes.empty? %>
<p class="alert alert-info">
No recipes added yet. Please add new recipe.
</p>
<% else %>
<h1 class="text-center">Recipes List</h1>
<% @recipes.each do |recipe| %>
<div class="row border mb-4">
<div class="col-4 p-2 text-start">
<%= link_to recipe.name, recipe_path(recipe), class: 'nav-link mb-3 fs-4 link-dark'%>
<% if can? :destroy, recipe %>
<%= button_to "Remove", recipe_path(recipe), method: :delete, data: { confirm: "Are you sure you want to delete this inventory?" }, class: 'btn btn-danger mx-3 fs-5 p-1 custom-button-width' %>
<% end %>
</div>
<div class="col p-2 text-start">
<p><%= recipe.description %></p>
</div>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
11 changes: 8 additions & 3 deletions app/views/recipes/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<h1>Add New Recipe</h1>

<%=render 'shared/recipe_form', recipe: @recipe %>
<div class="container">
<div class="row d-flex justify-content-center">
<div class="col-md-6 mt-4 p-2">
<h1 class="text-center">New Recipe</h1>
<%= render 'form' %>
</div>
</div>
</div>
94 changes: 64 additions & 30 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,34 +1,68 @@
<h1> Here is a list of specific recipe </h1>
<h2><%=@recipe.name%></h2>
<ul class ="recipe-container">
<div class ="spec-recipe-wrapper">
<div class = "recipe-duration">
<li>Peparation Time: <%=@recipe.preparation_time%></li>
<div class="container">
<div class="row d-flex justify-content-center">
<div class="col-md-6 mt-4 p-2">
<h1 class="text-center">
<%= @recipe.name %>
</h1>

<div class="d-flex justify-content-between align-items-center mb-3">
<span>Preparation Time: <%= @recipe.preparation_time %></span>
<div class="form-check form-switch form-check-reverse">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckReverse">
<label class="form-check-label" for="flexSwitchCheckReverse">Public</label>
</div>
</div>

<li>Cooking Time: <%=@recipe.cooking_time%></li>

</div>
<div class="label-container">
<label class="switch">
<div >
<p class ="public">Public:</p>
</div>
<div>
<%= form_for @recipe, url: recipe_path(@recipe), method: :patch do |f| %>
<%= f.check_box :public, {}, true, false %>
<span class="slider"></span>
<% end %>
</div>
</label>
</div>
</div>
<p>Steps goes here...</p>
<div class = "row-4">
<%= button_to 'Generate shopping list', recipe_path(@recipe), class: "remove-cmt-btn", remote: true %>
<%= button_to 'Add ingredient', recipe_path(@recipe), class: "remove-cmt-btn", remote: false %>
</div>
</ul>
<span class="d-block mb-3">Cooking Time: <%= @recipe.cooking_time %></span>
<span class="steps">Steps goes here...</span>

<div class="d-flex justify-content-between align-items-center mt-3">
<div class="add-btn">
<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal" class="btn btn-secondary">
Generate shopping list
</button>
</div>

<div class="add-btn">
<%= link_to 'Add ingredient', new_recipe_recipe_food_path(recipe_id: @recipe.id), class: 'btn btn-primary' %>
</div>
</div>
<hr/>

<% if @recipe.recipe_foods.empty? %>
<p class="no-record text-center">
No food ingredient added to this recipe yet.
</p>
<% else %>
<table class="table table-striped table-bordered">
<% if @recipe.recipe_foods.any? %>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Food</th>
<th scope="col">Quantity</th>
<th scope="col">Value</th>
<th scope="col" class="food-action">Actions</th>
</tr>
</thead>
<tbody>
<% @recipe.recipe_foods.each_with_index do |food_recipe, index| %>
<tr>
<td><%= index + 1 %></td>
<td><%=food_recipe.food.name %></td>
<td><%=food_recipe.quantity %></td>
<td><%=food_recipe.quantity * food_recipe.food.price %></td>
<td class="d-flex">
<%= link_to 'Modify', edit_recipe_recipe_food_path(@recipe, food_recipe), class: 'btn btn-danger btn-sm' %>
<%= form_with(url: recipe_recipe_food_path(@recipe, food_recipe), method: :delete, data: { confirm: 'Are you sure?' }) do |f| %>
<%= f.submit 'Remove', class: 'btn btn-danger btn-sm' %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
<% end %>
</table>
<% end %>
</div>
</div>
Loading

0 comments on commit 4c4231c

Please sign in to comment.