From 83e5afc5e1c3395df171b998f188e14a15d5b729 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 12:38:33 +0200 Subject: [PATCH 01/12] Add foods resourceful route --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index 0558788..cca6b2d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ root to: "users#index" resources :users, only: [:index, :show] do + resources :foods, only: [:index, :new, :create, :destroy] resources :recipes, only:[:index, :new, :show, :create, :destroy] end end From 28ea7bbb10afee97cc1fce3f183c808dbcc5cd3c Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 12:42:35 +0200 Subject: [PATCH 02/12] Generate foods controller --- app/controllers/foods_controller.rb | 4 ++++ app/helpers/foods_helper.rb | 2 ++ app/views/foods/index.html.erb | 2 ++ spec/helpers/foods_helper_spec.rb | 15 +++++++++++++++ spec/requests/foods_spec.rb | 11 +++++++++++ spec/views/foods/index.html.erb_spec.rb | 5 +++++ 6 files changed, 39 insertions(+) create mode 100644 app/controllers/foods_controller.rb create mode 100644 app/helpers/foods_helper.rb create mode 100644 app/views/foods/index.html.erb create mode 100644 spec/helpers/foods_helper_spec.rb create mode 100644 spec/requests/foods_spec.rb create mode 100644 spec/views/foods/index.html.erb_spec.rb diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb new file mode 100644 index 0000000..b29aa47 --- /dev/null +++ b/app/controllers/foods_controller.rb @@ -0,0 +1,4 @@ +class FoodsController < ApplicationController + def index + end +end diff --git a/app/helpers/foods_helper.rb b/app/helpers/foods_helper.rb new file mode 100644 index 0000000..4bad757 --- /dev/null +++ b/app/helpers/foods_helper.rb @@ -0,0 +1,2 @@ +module FoodsHelper +end diff --git a/app/views/foods/index.html.erb b/app/views/foods/index.html.erb new file mode 100644 index 0000000..f84ff7e --- /dev/null +++ b/app/views/foods/index.html.erb @@ -0,0 +1,2 @@ +

Foods#index

+

Find me in app/views/foods/index.html.erb

diff --git a/spec/helpers/foods_helper_spec.rb b/spec/helpers/foods_helper_spec.rb new file mode 100644 index 0000000..065129f --- /dev/null +++ b/spec/helpers/foods_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the FoodsHelper. For example: +# +# describe FoodsHelper 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 FoodsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/foods_spec.rb b/spec/requests/foods_spec.rb new file mode 100644 index 0000000..d463a36 --- /dev/null +++ b/spec/requests/foods_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe "Foods", type: :request do + describe "GET /index" do + it "returns http success" do + get "/foods/index" + expect(response).to have_http_status(:success) + end + end + +end diff --git a/spec/views/foods/index.html.erb_spec.rb b/spec/views/foods/index.html.erb_spec.rb new file mode 100644 index 0000000..42c01b7 --- /dev/null +++ b/spec/views/foods/index.html.erb_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe "foods/index.html.erb", type: :view do + pending "add some examples to (or delete) #{__FILE__}" +end From 9551912fd78058637f8c0ca0d547262cc121b32f Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 12:47:46 +0200 Subject: [PATCH 03/12] Correct naming convertion error on user_id fogerign key --- db/migrate/20240206104401_remove_users_id_from_foods.rb | 6 ++++++ db/schema.rb | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20240206104401_remove_users_id_from_foods.rb diff --git a/db/migrate/20240206104401_remove_users_id_from_foods.rb b/db/migrate/20240206104401_remove_users_id_from_foods.rb new file mode 100644 index 0000000..c24fdb6 --- /dev/null +++ b/db/migrate/20240206104401_remove_users_id_from_foods.rb @@ -0,0 +1,6 @@ +class RemoveUsersIdFromFoods < ActiveRecord::Migration[7.1] + def change + remove_column :foods, :users_id + add_reference :foods, :user, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 2acf0b8..c3c2bea 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_05_182737) do +ActiveRecord::Schema[7.1].define(version: 2024_02_06_104401) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -19,10 +19,10 @@ t.string "measurement_unit" t.integer "price" t.integer "quantity" - t.bigint "users_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["users_id"], name: "index_foods_on_users_id" + t.bigint "user_id" + t.index ["user_id"], name: "index_foods_on_user_id" end create_table "recipe_foods", force: :cascade do |t| @@ -60,7 +60,7 @@ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end - add_foreign_key "foods", "users", column: "users_id" + add_foreign_key "foods", "users" add_foreign_key "recipe_foods", "foods", column: "foods_id" add_foreign_key "recipe_foods", "recipes", column: "recipes_id" add_foreign_key "recipes", "users" From b02d2be4fcc46fbbad1b4b3289cac8c781f3b277 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 12:53:20 +0200 Subject: [PATCH 04/12] Correct naming convention error on recipe food table --- ...ve_recipes_id_and_foods_id_from_recipe_foods.rb | 8 ++++++++ db/schema.rb | 14 +++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20240206104934_remove_recipes_id_and_foods_id_from_recipe_foods.rb diff --git a/db/migrate/20240206104934_remove_recipes_id_and_foods_id_from_recipe_foods.rb b/db/migrate/20240206104934_remove_recipes_id_and_foods_id_from_recipe_foods.rb new file mode 100644 index 0000000..cd794d6 --- /dev/null +++ b/db/migrate/20240206104934_remove_recipes_id_and_foods_id_from_recipe_foods.rb @@ -0,0 +1,8 @@ +class RemoveRecipesIdAndFoodsIdFromRecipeFoods < ActiveRecord::Migration[7.1] + def change + remove_column :recipe_foods, :recipes_id + remove_column :recipe_foods, :foods_id + add_reference :recipe_foods, :recipe, foreign_key: true + add_reference :recipe_foods, :food, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index c3c2bea..88fb774 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_06_104401) do +ActiveRecord::Schema[7.1].define(version: 2024_02_06_104934) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -27,12 +27,12 @@ create_table "recipe_foods", force: :cascade do |t| t.integer "quantity" - t.bigint "recipes_id" - t.bigint "foods_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["foods_id"], name: "index_recipe_foods_on_foods_id" - t.index ["recipes_id"], name: "index_recipe_foods_on_recipes_id" + t.bigint "recipe_id" + t.bigint "food_id" + t.index ["food_id"], name: "index_recipe_foods_on_food_id" + t.index ["recipe_id"], name: "index_recipe_foods_on_recipe_id" end create_table "recipes", force: :cascade do |t| @@ -61,7 +61,7 @@ end add_foreign_key "foods", "users" - add_foreign_key "recipe_foods", "foods", column: "foods_id" - add_foreign_key "recipe_foods", "recipes", column: "recipes_id" + add_foreign_key "recipe_foods", "foods" + add_foreign_key "recipe_foods", "recipes" add_foreign_key "recipes", "users" end From 3e99c56987f203f1879d77956def7068c99dc95d Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 12:57:49 +0200 Subject: [PATCH 05/12] Define index action on foods controller --- app/controllers/foods_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb index b29aa47..1bb669e 100644 --- a/app/controllers/foods_controller.rb +++ b/app/controllers/foods_controller.rb @@ -1,4 +1,6 @@ class FoodsController < ApplicationController def index + @user = User.find(params[:user_id]) + @food = @user.foods end end From 428bcbcb4ec03bf50546e103c363654d22965c2e Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 13:17:23 +0200 Subject: [PATCH 06/12] Add index view for foods --- app/controllers/foods_controller.rb | 2 +- app/views/foods/index.html.erb | 31 +++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb index 1bb669e..038b872 100644 --- a/app/controllers/foods_controller.rb +++ b/app/controllers/foods_controller.rb @@ -1,6 +1,6 @@ class FoodsController < ApplicationController def index @user = User.find(params[:user_id]) - @food = @user.foods + @foods = @user.foods end end diff --git a/app/views/foods/index.html.erb b/app/views/foods/index.html.erb index f84ff7e..4382f10 100644 --- a/app/views/foods/index.html.erb +++ b/app/views/foods/index.html.erb @@ -1,2 +1,29 @@ -

Foods#index

-

Find me in app/views/foods/index.html.erb

+

Foods!

+ +<%= link_to 'Add food', new_user_food_path(@user) %> + + + + + + + + + + + + + <% @foods.each do |food| %> + + + + + + + + <% end %> + +
NameMeasurement UnitPriceQuantityAction
<%= food.name %><%= food.measurement_unit %><%= food.price %><%= food.quantity %><%= button_to 'Remove', user_food_path(@user, food), data: { + turbo_method: :delete, + turbo_confirm: 'Are you sure?' + } %>
From 880452395afe07b6e88c84cb04055655d8b3c7b8 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 13:30:45 +0200 Subject: [PATCH 07/12] Add new and create action on foods controller --- app/controllers/foods_controller.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb index 038b872..8cb4b8b 100644 --- a/app/controllers/foods_controller.rb +++ b/app/controllers/foods_controller.rb @@ -3,4 +3,24 @@ def index @user = User.find(params[:user_id]) @foods = @user.foods end + + def new + @user = current_user + @food = @user.foods.build + end + + def create + @food = current_user.foods.build(food_parama) + if @food.save + redirect_to user_foods_path(current_user) + else + render :new, status: :unprocessable_entity + end + end + + private + + def food_params + params.require(:food).permit(:name, :measurement_unit, :price, :quantity) + end end From 0dd37f62b4cfb1234a5303eeec93933616b94001 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 13:33:11 +0200 Subject: [PATCH 08/12] Add destroy action --- app/controllers/foods_controller.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb index 8cb4b8b..ff0ebce 100644 --- a/app/controllers/foods_controller.rb +++ b/app/controllers/foods_controller.rb @@ -18,6 +18,12 @@ def create end end + def destroy + @food = current_user.foods.find(params[:id]) + @food.destroy + redirect_to user_foods_path(current_user), notice: 'Food successfully deleted' + end + private def food_params From 9d70b2632da1bd4ae52e91a194c9453af976d9b5 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 13:42:29 +0200 Subject: [PATCH 09/12] Correct typing error --- app/controllers/foods_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb index ff0ebce..c290a26 100644 --- a/app/controllers/foods_controller.rb +++ b/app/controllers/foods_controller.rb @@ -10,7 +10,7 @@ def new end def create - @food = current_user.foods.build(food_parama) + @food = current_user.foods.build(food_params) if @food.save redirect_to user_foods_path(current_user) else From 6936c0a611daee5ffb52808fc3ad2ba9c39fd877 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 13:55:31 +0200 Subject: [PATCH 10/12] Correct delete button method --- app/views/foods/index.html.erb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/views/foods/index.html.erb b/app/views/foods/index.html.erb index 4382f10..3b8428a 100644 --- a/app/views/foods/index.html.erb +++ b/app/views/foods/index.html.erb @@ -19,10 +19,7 @@ <%= food.measurement_unit %> <%= food.price %> <%= food.quantity %> - <%= button_to 'Remove', user_food_path(@user, food), data: { - turbo_method: :delete, - turbo_confirm: 'Are you sure?' - } %> + <%= button_to 'Remove', user_food_path(@user, food), method: :delete %> <% end %> From 718d0408e70018529438a4b0425e4c49d1063663 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 13:56:41 +0200 Subject: [PATCH 11/12] Add form builder to add new food --- app/views/foods/new.html.erb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 app/views/foods/new.html.erb diff --git a/app/views/foods/new.html.erb b/app/views/foods/new.html.erb new file mode 100644 index 0000000..d317005 --- /dev/null +++ b/app/views/foods/new.html.erb @@ -0,0 +1,27 @@ +

Add food

+ +<%= form_with model: @food, url: user_foods_path(@user), local: true, method: :post do |form| %> +
+ <%= form.label :name %>
+ <%= form.text_field :name %> +
+ +
+ <%= form.label :measurement_unit %>
+ <%= form.select :measurement_unit, ['mg', 'g', 'kg', 'oz', 'lb', 'units'] %> +
+ +
+ <%= form.label :price %>
+ <%= form.text_field :price %> +
+ +
+ <%= form.label :quantity %>
+ <%= form.text_field :quantity %> +
+ +
+ <%= form.submit %> +
+<% end %> \ No newline at end of file From d5c22f9522346e3efdfb544a00bff1bfe1aa7842 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Tue, 6 Feb 2024 14:00:22 +0200 Subject: [PATCH 12/12] Correct rubocop offenses --- app/controllers/foods_controller.rb | 2 +- spec/requests/foods_spec.rb | 9 ++++----- spec/views/foods/index.html.erb_spec.rb | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/controllers/foods_controller.rb b/app/controllers/foods_controller.rb index c290a26..2d30b4a 100644 --- a/app/controllers/foods_controller.rb +++ b/app/controllers/foods_controller.rb @@ -1,7 +1,7 @@ class FoodsController < ApplicationController def index @user = User.find(params[:user_id]) - @foods = @user.foods + @foods = @user.foods end def new diff --git a/spec/requests/foods_spec.rb b/spec/requests/foods_spec.rb index d463a36..8c54c31 100644 --- a/spec/requests/foods_spec.rb +++ b/spec/requests/foods_spec.rb @@ -1,11 +1,10 @@ require 'rails_helper' -RSpec.describe "Foods", type: :request do - describe "GET /index" do - it "returns http success" do - get "/foods/index" +RSpec.describe 'Foods', type: :request do + describe 'GET /index' do + it 'returns http success' do + get '/foods/index' expect(response).to have_http_status(:success) end end - end diff --git a/spec/views/foods/index.html.erb_spec.rb b/spec/views/foods/index.html.erb_spec.rb index 42c01b7..332473c 100644 --- a/spec/views/foods/index.html.erb_spec.rb +++ b/spec/views/foods/index.html.erb_spec.rb @@ -1,5 +1,5 @@ require 'rails_helper' -RSpec.describe "foods/index.html.erb", type: :view do +RSpec.describe 'foods/index.html.erb', type: :view do pending "add some examples to (or delete) #{__FILE__}" end