Skip to content

Commit

Permalink
Merge branch 'dev' into public-recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kossi-stack committed Jan 7, 2022
2 parents 3751bd8 + a5e67c6 commit 0318fbc
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 15 deletions.
43 changes: 41 additions & 2 deletions app/assets/stylesheets/application.css
Expand Up @@ -14,10 +14,10 @@
*= require_self
*/

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300&display=swap');
@import url("https://fonts.googleapis.com/css2?family=Inter:wght@300&display=swap");

body {
font-family: 'Inter', sans-serif;
font-family: Inter, sans-serif;
}

.add-btn {
Expand Down Expand Up @@ -73,3 +73,42 @@ li {
.log-input {
width: 140px;
}

.r-btn {
font-weight: 600;
}

.f-select {
width: 100%;
padding: 7px;
}

/* stylelint-disable-next-line selector-id-pattern */
select#recipe_food_food_id {
width: 100%;
padding: 5px;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #212529;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
appearance: none;
border-radius: 0.25rem;
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

/* stylelint-disable-next-line selector-id-pattern */
select#recipe_food_food_id:focus {
color: #212529;
background-color: #fff;
border-color: #86b7fe;
outline: 0;
box-shadow: 0 0 0 0.25rem rgb(13 110 253 / 25%);
}

.rf-btn {
margin: auto;
width: fit-content;
}
46 changes: 46 additions & 0 deletions app/controllers/recipe_foods_controller.rb
@@ -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
8 changes: 8 additions & 0 deletions app/controllers/users_controller.rb
Expand Up @@ -2,4 +2,12 @@ class UsersController < ApplicationController
def index
@users = User.all
end

def show
@recipe_foods = RecipeFood.joins(:food, :recipe).where(food: { user: current_user }, recipe: { user: current_user })
@items_count = @recipe_foods.select('food_id').distinct.count
@total_amount = @recipe_foods.sum('quantity * price')
@items = @recipe_foods.group('food.name, price, measurement_unit')
.select('food.name, SUM(quantity) as quantity, (sum(quantity) * price) as price, measurement_unit')
end
end
14 changes: 14 additions & 0 deletions app/models/recipe.rb
@@ -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
4 changes: 4 additions & 0 deletions app/models/recipe_food.rb
@@ -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
21 changes: 21 additions & 0 deletions app/views/recipe_foods/_form.html.erb
@@ -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 %>
30 changes: 30 additions & 0 deletions app/views/recipe_foods/_index.html.erb
@@ -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>
9 changes: 9 additions & 0 deletions app/views/recipe_foods/edit.html.erb
@@ -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>
2 changes: 0 additions & 2 deletions app/views/recipe_foods/index.html.erb

This file was deleted.

29 changes: 29 additions & 0 deletions app/views/recipe_foods/new.html.erb
@@ -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>
2 changes: 0 additions & 2 deletions app/views/recipe_foods/show.html.erb

This file was deleted.

15 changes: 8 additions & 7 deletions app/views/recipes/show.html.erb
Expand Up @@ -20,13 +20,14 @@
<h5 class="p-2"> Cooking time: <%= @recipe.preparation_time %> min </h5>
<h5 class="p-2"> Steps go here...</h5>
</div>
<div class="d-flex justify-content-around">
<div class="w-30">
<%= link_to "Generate shopping list", recipes_path, data: {disable_with: "Adding..."}, class: "btn btn-primary btn-floating mx-1"%>
</div>
<div>
<%= link_to "Add ingredient", recipes_path, data: {disable_with: "Adding..."}, class: "btn btn-primary btn-floating mx-1"%>
</div>
<div class="d-flex justify-content-around p-3">
<button id="open-modal" class="btn btn-primary btn-floating mx-1 r-btn">Generate shopping list</button>
<% if can? :update, @recipe %>
<%= link_to 'Add ingredient', new_recipe_recipe_food_path(@recipe), class: "btn btn-primary btn-floating mx-1 r-btn" %>
<% end %>
</div>
<div class="column is-three-quarters">
<%= render partial: "recipe_foods/index", locals: { recipe_foods: @recipe.recipe_foods } %>
</div>
</div>
</div>
17 changes: 15 additions & 2 deletions config/routes.rb
Expand Up @@ -2,8 +2,21 @@
devise_for :users
root to: 'foods#index'
resources :foods, only: [:index, :new, :create, :destroy]
resources :recipes, only: [:index, :show, :new, :create, :destroy]
resources :public_recipes, only: %i[index] resources :public_recipes, only: %i[index]
resources :recipes, only: [:index, :show, :new, :create, :destroy] do
resources :recipe_foods, only: [:new]
end
resources :recipe_foods, only: [:edit, :update, :destroy, :create]
resources :public_recipes, only: %i[index]

devise_scope :user do
authenticated :user do
root 'users#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
end

devise_scope :user do
authenticated :user do
Expand Down

0 comments on commit 0318fbc

Please sign in to comment.