Skip to content

Commit

Permalink
Merge pull request #21 from clintonjosephs/recipe
Browse files Browse the repository at this point in the history
Recipe
  • Loading branch information
clintonjosephs authored May 18, 2022
2 parents a20c745 + 38c6954 commit cc8b6ac
Show file tree
Hide file tree
Showing 27 changed files with 412 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ gem 'cancancan', '~> 1.9'
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
gem 'tzinfo-data'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', require: false
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ GEM
faker (2.21.0)
i18n (>= 1.8.11, < 2)
ffi (1.15.5)
ffi (1.15.5-x86-mingw32)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.10.0)
Expand Down Expand Up @@ -161,13 +162,16 @@ GEM
net-protocol
timeout
nio4r (2.5.8)
nokogiri (1.13.6-x86-mingw32)
racc (~> 1.4)
nokogiri (1.13.6-x86_64-darwin)
racc (~> 1.4)
orm_adapter (0.5.0)
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
pg (1.3.5)
pg (1.3.5-x86-mingw32)
public_suffix (4.0.7)
puma (5.6.4)
nio4r (~> 2.0)
Expand Down Expand Up @@ -263,6 +267,8 @@ GEM
railties (>= 6.0.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
tzinfo-data (1.2022.1)
tzinfo (>= 1.0.0)
unicode-display_width (2.1.0)
uniform_notifier (1.16.0)
warden (1.2.9)
Expand All @@ -284,6 +290,7 @@ GEM
zeitwerk (2.5.4)

PLATFORMS
x86-mingw32
x86_64-darwin-19

DEPENDENCIES
Expand Down
Binary file added app/assets/images/no-food-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/recipe-box-banner.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ td img {
margin-right: auto;
}

.recipe-image {
width: 100%;
height: auto;
}

.card-img-top {
width: 100%;
height: 15vw;
Expand All @@ -131,6 +136,11 @@ td img {
padding: 0;
}

.recipe-card {
border-radius: 0 !important;
padding: 4px;
}

.shadow {
-moz-box-shadow: 0 0 3px #ccc !important;
-webkit-box-shadow: 0 0 3px #ccc !important;
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/publics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class PublicsController < ApplicationController
def index; end
end
72 changes: 72 additions & 0 deletions app/controllers/recipe_foods_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class RecipeFoodsController < ApplicationController
load_and_authorize_resource
before_action :authenticate_user!

def new
setup
end

def edit
setup
@recipe_food = RecipeFood.find(params[:id])
end

def create
setup
recipe_food = RecipeFood.new(recipe_food_params)
recipe_food.recipe_id = params[:recipe_id]
if recipe_food.save
flash[:success] = 'Food was successfully added to recipe.'
else
flash[:danger] = 'Food was not added to recipe because <ul class="error-list">'
recipe_food.errors.full_messages.each do |msg|
flash[:danger] += "<li>#{msg}</li>"
end
flash[:danger] += '</ul>'
end
redirect_to new_recipe_recipe_food_path(params[:recipe_id])
end

def destroy
@recipe_food = RecipeFood.find(params[:id])
@recipe_food.destroy
respond_to do |format|
format.html do
flash[:success] = 'Recipe food deleted successfully'
redirect_back_or_to({ action: 'show', id: params[:recipe_id] })
end
end
end

def update
recipe_food = RecipeFood.find(params[:id])

if recipe_food.update(recipe_food_update)
flash[:success] = "quantity for #{recipe_food.food.name} was successfully updated."
else
flash[:danger] = 'operation failed <ul class="error-list">'
recipe_food.errors.full_messages.each do |msg|
flash[:danger] += "<li>#{msg}</li>"
end
flash[:danger] += '</ul>'
end
redirect_to recipe_path(params[:recipe_id])
end

def setup
@recipe_food = RecipeFood.new
@recipe_food_ids = RecipeFood.all.where(recipe_id: params[:recipe_id]).pluck(:food_id)
@foods = Food.all.where(user_id: current_user.id).where.not(id: @recipe_food_ids).order(name: :asc)
@recipe = Recipe.find(params[:recipe_id])
end

private

def recipe_food_params
params.require(:recipe_food).permit(:food_id, :quantity)
end

def recipe_food_update
params.require(:recipe_food).permit(:quantity)
end
end
67 changes: 66 additions & 1 deletion app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,68 @@
class RecipesController < ApplicationController
def index; end
before_action :authenticate_user!, except: [:public_recipes]
load_and_authorize_resource

def index
@recipes = Recipe.all.where(user_id: current_user).order(created_at: :desc).with_attached_image
end

def show
data = Recipe.where(id: params[:id]).with_attached_image
@recipe = data[0]
@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
end

def new
@user = current_user
@recipe = @user.recipes.new
end

def update
Recipe.find(params[:id]).update(public: params[:recipe][:public])
end

def create
@user = current_user
@recipe = @user.recipes.new(recipe_params)
@recipe.preparation_time = "#{params[:recipe][:preparation_time]} minute(s)"
@recipe.cooking_time = "#{params[:recipe][:cooking_time]} minute(s)"

respond_to do |format|
format.html do
if @recipe.save
flash[:success] = 'Recipe created successfully'
redirect_to recipes_url
else
flash.now[:danger] = 'Recipe was not created because <ul class="error-list">'
@recipe.errors.full_messages.each do |msg|
flash.now[:danger] += "<li>#{msg}</li>"
end
flash.now[:danger] += '</ul>'
render :new
end
end
end
end

def destroy
recipe = Recipe.find(params[:id])
if recipe.destroy
flash[:success] = 'Recipe deleted successfully'
else
flash.now[:danger] = 'Recipe deleted because <ul class="error-list">'
recipe.errors.full_messages.each do |msg|
flash[:danger] += "<li>#{msg}</li>"
end
flash[:danger] += '</ul>'
end
redirect_to recipes_url
end

private

def recipe_params
params.require(:recipe).permit(:name, :preparation_time, :cooking_time, :public, :description, :image)
end
end
2 changes: 2 additions & 0 deletions app/helpers/public_recipe_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PublicRecipeHelper
end
2 changes: 2 additions & 0 deletions app/helpers/recipe_food_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module RecipeFoodHelper
end
3 changes: 3 additions & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def initialize(user)
can :destroy, Inventory do |inventory|
inventory.user_id == user.id
end
can :destroy, Recipe do |recipe|
recipe.user_id == user.id
end
else
can :read, :all
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
class Recipe < ApplicationRecord
belongs_to :user
has_many :recipe_foods, dependent: :destroy

validates :name, presence: true, length: { maximum: 100 }
validates :user_id, presence: true
validates :preparation_time, presence: true
validates :cooking_time, presence: true

has_one_attached :image
end
4 changes: 4 additions & 0 deletions app/models/recipe_food.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class RecipeFood < ApplicationRecord
belongs_to :recipe
belongs_to :food

validates :recipe_id, presence: true
validates :food_id, presence: true
validates :quantity, presence: true, numericality: { greater_than: 0 }
end
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg mb-3" style="background-color: var(--accent-color)">
<div class="container">
<%= link_to "RecipeBook", public_recipes_path, :"data-turbolinks" => false, class: "navbar-brand display-6" %>
<%= link_to "RecipeBook", root_path, :"data-turbolinks" => false, class: "navbar-brand display-6" %>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<i class="fa fa-bars" aria-hidden="true"></i>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<%= link_to "Public Recipes", public_recipes_path, :"data-turbolinks" => false, class: "nav-link" %>
<%= link_to "Public Recipes", publics_path, :"data-turbolinks" => false, class: "nav-link" %>
</li>
<% if current_user %>
<li class="nav-item dropdown">
Expand Down
1 change: 1 addition & 0 deletions app/views/publics/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<H1>pUBLIC</H1>
1 change: 1 addition & 0 deletions app/views/recipe_foods/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "shared/recipe_food_form", locals: {recipe: @recipe, foods: @foods, method: 'put', recipe_food: @recipe_food, type: 'edit', url: recipe_recipe_food_path} %>
1 change: 1 addition & 0 deletions app/views/recipe_foods/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "shared/recipe_food_form", locals: {recipe: @recipe, foods: @foods, method: 'post', recipe_food: @recipe_food, type: 'new', url: recipe_recipe_foods_path} %>
16 changes: 15 additions & 1 deletion app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
<h1>Recipes</h1>

<h5 class="mb-2">Your Recipies (<%= @recipes.length %>)</h5>
<hr />
<div class="row">
<% @recipes.each do |recipe| %>
<%= render partial: "shared/recipe_display", locals: {recipe: recipe} %>
<% end %>
</div>







39 changes: 39 additions & 0 deletions app/views/recipes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<div class="row container-fluid">
<div class="mb-3 col-md-8 offset-md-2 inventory-card shadow">
<%= image_tag("recipe-box-banner.jpeg", class: "card-img-top") %>
<div class="card-body">
<h5 class="card-title">😊 Create a new recipe!!</h5>
<p class="card-text"> Would'nt it be nice to store a recipe of your favourite food? Go ahead!</p>
<%= form_with model: @recipe, url: recipes_path do |f| %>
<div class="mb-3">
<%= f.file_field :image, class: 'form-control' %>
<%= f.label :image, 'Choose an image that would appear as banner for recipe', class: 'mb-2' %>
</div>
<div class="form-floating mb-3">
<%= f.text_field :name, class: 'form-control', required: true %>
<%= f.label :name, "Recipe Name*" %>
</div>
<div class="form-floating mb-3">
<%= f.number_field :preparation_time, class: 'form-control', required: true %>
<%= f.label :preparation_time, "Preparation Time in mins*" %>
</div>
<div class="form-floating mb-3">
<%= f.number_field :cooking_time, class: 'form-control', required: true %>
<%= f.label :cooking_time, "Cooking Time in mins*" %>
</div>
<div class="form-floating mb-3">
<%= f.text_area :description, class: 'form-control' %>
<%= f.label :description, "Description" %>
</div>
<div class="form-check form-switch">
<%= f.check_box :public, class: "form-check-input" %>
<%= f.label :public, 'Mark public', class: "form-check-label" %> <br />
<i style="color: var(--primary-color)"> This allows other users to see your recipe</i>
</div>
<div class="form-group mt-3">
<%= f.submit '🥣 Create Recipe', class: "btn", style: "background-color: var(--primary-color); color: var(--white);", data: {disable_with: "Loading..."} %>
</div>
<% end %>
</div>
</div>
</div>
Loading

0 comments on commit cc8b6ac

Please sign in to comment.