Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into public-recipes
- Loading branch information
Showing
13 changed files
with
225 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,48 @@ | ||
class RecipeFoodsController < ApplicationController | ||
before_action :set_recipe_food, only: %i[edit update destroy] | ||
before_action :set_foods, only: %i[edit new update] | ||
|
||
def new | ||
@recipe = Recipe.find(params[:recipe_id]) | ||
@recipe_food = @recipe.recipe_foods.new | ||
end | ||
|
||
def edit; end | ||
|
||
def create | ||
@recipe_food = RecipeFood.new(recipe_food_params) | ||
|
||
respond_to do |format| | ||
if @recipe_food.save | ||
format.html do | ||
redirect_to recipe_path([:recipe_id]), notice: 'Recipe food was successfully created.' | ||
end | ||
else | ||
set_foods | ||
format.html { render :new, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
def destroy | ||
recipe = @recipe_food.recipe | ||
recipe.foods.delete(@recipe_food.food) | ||
respond_to do |format| | ||
format.html { redirect_to recipe_path(recipe), notice: 'Recipe food was successfully deleted.' } | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_foods | ||
@foods = current_user.foods.map { |food| [food.name, food.id] } | ||
end | ||
|
||
def set_recipe_food | ||
@recipe_food = RecipeFood.includes(:recipe, :food).find(params[:id]) | ||
end | ||
|
||
def recipe_food_params | ||
params.fetch(:recipe_food, {}).permit(:quantity, :food_id, :recipe_id) | ||
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
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,2 +1,16 @@ | ||
class Recipe < ApplicationRecord | ||
belongs_to :user | ||
has_many :recipe_foods, dependent: :destroy | ||
has_many :foods, through: :recipe_foods, dependent: :destroy | ||
has_many :foods, through: :recipe_foods, dependent: :destroy | ||
|
||
validates :name, :preparation_time, :cooking_time, :description, presence: true | ||
|
||
def total_items | ||
recipe_foods.sum(:quantity) | ||
end | ||
|
||
def total_price | ||
recipe_foods.joins(:recipe, :food).sum('price * 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,2 +1,6 @@ | ||
class RecipeFood < ApplicationRecord | ||
belongs_to :recipe | ||
belongs_to :food | ||
validates :food_id, presence: true, uniqueness: { scope: :recipe_id } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<%= form_with(model: recipe_food) do |form| %> | ||
<div class="field container"> | ||
<%= form.label :food, class: "label" %> | ||
<div class="select is-fullwidth"> | ||
<%= form.select :food_id, @foods %> | ||
</div> | ||
</div> | ||
|
||
<div class="field container"> | ||
<%= form.label :quantity, class: "label" %> | ||
<div class="control"> | ||
<%= form.text_field :quantity, class: "input", type: "number", required: true, min: 1 %> | ||
</div> | ||
</div> | ||
|
||
<%= form.hidden_field :recipe_id, value: recipe_food.recipe_id %> | ||
|
||
<div class="actions container"> | ||
<%= form.submit class: "button is-success" %> | ||
</div> | ||
<% 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,30 @@ | ||
<div class="table-container"> | ||
<table class="table is-fullwidth"> | ||
<thead> | ||
<tr> | ||
<th>Food</th> | ||
<th>Quantity</th> | ||
<th>Value</th> | ||
<th>Actions</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% recipe_foods.each do |recipe_food| %> | ||
<tr> | ||
<th><%= recipe_food.food.name %></th> | ||
<td><%= pluralize(recipe_food.quantity, recipe_food.food.measurement_unit) %></td> | ||
<td>$<%= recipe_food.quantity * recipe_food.food.price %></td> | ||
<% if can? :update, recipe_food.recipe %> | ||
<td> | ||
<div class="buttons"> | ||
<%= link_to 'Edit', edit_recipe_food_path(recipe_food), class: "button is-small is-success is-light" %> | ||
<%= link_to 'Delete', recipe_food_path(recipe_food), method: :delete, data: { confirm: 'Are you sure?' }, class: "button is-small is-danger is-light" %> | ||
</div> | ||
</td> | ||
<% end %> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</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,9 @@ | ||
|
||
<div class="columns is-justify-content-center"> | ||
<div class="column is-two-fifths-desktop"> | ||
<h1 class="title is-4">Edit Ingredient</h1> | ||
|
||
<%= render 'form', recipe_food: @recipe_food %> | ||
<%= link_to 'Back', url_for(:back), class: "button is-light mt-2" %> | ||
</div> | ||
</div> |
This file was deleted.
Oops, something went wrong.
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,29 @@ | ||
<div class="container w-50 text-center"> | ||
|
||
<%= form_with(model: @recipe_food) do |form| %> | ||
<div class="card form"> | ||
<div class="card-body"> | ||
<div class="field container"> | ||
<%= form.label :food, class: "form-label p-2" %> | ||
<div class="select"> | ||
<%= form.select :food_id, @foods, class: "form-select" %> | ||
</div> | ||
</div> | ||
|
||
<div class="field container"> | ||
<%= form.label :quantity, class: "label", class: "form-label p-2" %> | ||
<div class="control"> | ||
<%= form.text_field :quantity, class: "form-control", type: "number", required: true, min: 1 %> | ||
</div> | ||
</div> | ||
|
||
<%= form.hidden_field :recipe_id, value: @recipe_food.recipe_id %> | ||
|
||
<div class="actions rf-btn p-3"> | ||
<%= form.submit class: "btn btn-primary btn-floating mx-1" %> | ||
</div> | ||
</div> | ||
</div> | ||
<% end %> | ||
|
||
</div> |
This file was deleted.
Oops, something went wrong.
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