Skip to content

Commit

Permalink
Merge pull request #11 from tsheporamantso/recipe-list
Browse files Browse the repository at this point in the history
Recipe list πŸ‘¨β€πŸ’»
  • Loading branch information
tsheporamantso authored Feb 6, 2024
2 parents b547db6 + 5512b36 commit 5c5cd4b
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 13 deletions.
26 changes: 26 additions & 0 deletions app/assets/stylesheets/recipe.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.container {
align-items: center;
text-align: center;
}

.recipe-container {
display: flex;
justify-content: space-around;
align-items: center;
border: 1px solid #333;
padding: 10px;
margin: 0 80px;
}

.recipe-info {
display: flex;
flex-direction: column;
justify-content: center;
align-items: flex-start;
gap: 20px;
}

p {
font-size: 20px;
font-weight: 700;
}
37 changes: 37 additions & 0 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class RecipesController < ApplicationController
def index
@user = User.find(params[:user_id])
@recipes = @user.recipes
end

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

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

def create
@recipe = current_user.recipes.build(recipe_params)
if @recipe.save
redirect_to user_recipes_path(current_user)
else
render :new, status: :unprocessable_entity
end
end

def destroy
@recipe = current_user.recipes.find(params[:id])
@recipe.destroy
redirect_to user_recipes_path(current_user), notice: 'Recipe was successfully deleted.'
end

private

def recipe_params
params.require(:recipe).permit(:name, :description, :preparation_time, :cooking_time, :public)
end
end
2 changes: 2 additions & 0 deletions app/helpers/recipes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module RecipesHelper
end
20 changes: 20 additions & 0 deletions app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<%= stylesheet_link_tag 'recipe' %>

<div class="container">
<h1>Recipes!</h1>
<% @recipes.each do |recipe| %>
<div class='recipe-container'>
<div class="recipe-info">
<%= link_to recipe.name, user_recipe_path(@user, recipe) %>
<%= button_to 'Remove', user_recipe_path(@user, recipe), data: {
turbo_method: :delete,
turbo_confirm: 'Are you sure?'
} %>
</div>
<div>
<p><%= recipe.description %></p>
</div>
</div>
<%= link_to 'New Recipe', new_user_recipe_path(@user) %>
<% end %>
</div>
36 changes: 36 additions & 0 deletions app/views/recipes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<%= stylesheet_link_tag 'registration' %>

<div class="container">
<h1>Create your recipe</h1>

<%= form_with(model: @recipe, url:user_recipes_path(@user), local: true, method: :post) do |form| %>
<div>
<%= form.label :name %><br>
<%= form.text_field :name, class: 'input'%>
</div>

<div>
<%= form.label :description %><br>
<%= form.text_area :description, class: 'input' %>
</div>

<div>
<%= form.label :preparation_time %><br>
<%= form.text_field :preparation_time, class: 'input' %>
</div>

<div>
<%= form.label :cooking_time %><br>
<%= form.text_field :cooking_time, class: 'input' %>
</div>

<div>
<%= form.label :public %><br>
<%= form.check_box :public %>
</div>

<div>
<%= form.submit class: 'btns' %>
</div>
<% end %>
</div>
17 changes: 17 additions & 0 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= stylesheet_link_tag 'recipe' %>

<div class="container">
<h1>Recipes</h1>
<div>
<h2><%= @recipe.name %></h2>
</div>
<div class='recipe-info'>
<p>preparation time:<%= @recipe.preparation_time %></p>
<p>cooking time:<%= @recipe.cooking_time %></p>
<p><%= @recipe.description %></p>
</div>
<div>
<%= link_to 'Generate Shopping List' %>
<%= link_to 'Add ingredients' %>
</div>
</div>
12 changes: 3 additions & 9 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
devise_for :users
root to: "users#index"

resources :users, only: [:index, :show]
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check

# Defines the root path route ("/")
# root "posts#index"
resources :users, only: [:index, :show] do
resources :recipes, only:[:index, :new, :show, :create, :destroy]
end
end
6 changes: 6 additions & 0 deletions db/migrate/20240205182737_remove_users_id_from_recipes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RemoveUsersIdFromRecipes < ActiveRecord::Migration[7.1]
def change
remove_column :recipes, :users_id
add_reference :recipes, :user, foreign_key: true
end
end
8 changes: 4 additions & 4 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions spec/helpers/recipes_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the RecipesHelper. For example:
#
# describe RecipesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe RecipesHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
10 changes: 10 additions & 0 deletions spec/requests/recipes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe 'Recipes', type: :request do
describe 'GET /index' do
it 'returns http success' do
get '/recipes/index'
expect(response).to have_http_status(:success)
end
end
end
5 changes: 5 additions & 0 deletions spec/views/recipes/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe 'recipes/index.html.erb', type: :view do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 5c5cd4b

Please sign in to comment.