-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from clintonjosephs/recipe
Recipe
- Loading branch information
Showing
27 changed files
with
412 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class PublicsController < ApplicationController | ||
def index; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module PublicRecipeHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module RecipeFoodHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<H1>pUBLIC</H1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.