From 6b442b6c3b4b9aac220b5bd8a33cafe8953136af Mon Sep 17 00:00:00 2001 From: Samuel Mwape <43408618+Mwapsam@users.noreply.github.com> Date: Tue, 17 May 2022 07:54:55 +0200 Subject: [PATCH 01/12] Created controller methods for recipes --- Gemfile | 4 ++-- Gemfile.lock | 9 +++++++- app/controllers/recipes_controller.rb | 33 ++++++++++++++++++++++++++- app/models/inventory_food.rb | 3 +++ app/models/recipe.rb | 4 ++++ app/models/recipe_food.rb | 3 +++ app/views/recipes/index.html.erb | 16 ++++++++++++- config/database.yml | 2 ++ db/seeds.rb | 19 +++++++++++++++ 9 files changed, 88 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 85f08e0..b64ed53 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '3.0.1' +ruby '3.0.4' # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem 'rails', '~> 7.0.2', '>= 7.0.2.3' @@ -46,7 +46,7 @@ gem 'cancancan', '~> 1.9' # gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] +gem 'tzinfo-data' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 58abf81..2cc8a25 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -110,6 +110,7 @@ GEM faker (2.21.0) i18n (>= 1.8.11, < 2) ffi (1.15.5) + ffi (1.15.5-x86-mingw32) globalid (1.0.0) activesupport (>= 5.0) i18n (1.10.0) @@ -157,6 +158,8 @@ GEM net-protocol timeout nio4r (2.5.8) + nokogiri (1.13.6-x86-mingw32) + racc (~> 1.4) nokogiri (1.13.6-x86_64-darwin) racc (~> 1.4) orm_adapter (0.5.0) @@ -164,6 +167,7 @@ GEM parser (3.1.2.0) ast (~> 2.4.1) pg (1.3.5) + pg (1.3.5-x86-mingw32) public_suffix (4.0.7) puma (5.6.4) nio4r (~> 2.0) @@ -259,6 +263,8 @@ GEM railties (>= 6.0.0) tzinfo (2.0.4) concurrent-ruby (~> 1.0) + tzinfo-data (1.2022.1) + tzinfo (>= 1.0.0) unicode-display_width (2.1.0) uniform_notifier (1.16.0) warden (1.2.9) @@ -280,6 +286,7 @@ GEM zeitwerk (2.5.4) PLATFORMS + x86-mingw32 x86_64-darwin-19 DEPENDENCIES @@ -310,7 +317,7 @@ DEPENDENCIES webdrivers RUBY VERSION - ruby 3.0.1p64 + ruby 3.0.4p208 BUNDLED WITH 2.3.12 diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 9a84af9..90c2f1a 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -1,3 +1,34 @@ class RecipesController < ApplicationController - def index; end + def index + @recipes = Recipe.all + end + + def show + @recipe = Recipe.find(params[:id]) + end + + def new + @recipe = Recipe.new + end + + def create + @recipe = Recipe.new(recipe_params) + if @recipe.save + redirect_to @recipe + else + render 'new' + end + end + + def destroy + @recipe = Recipe.find(params[:id]) + @recipe.destroy + redirect_to recipes_path + end + + private + + def recipe_params + params.require(:recipe).permit(:name, :description, :user_id) + end end diff --git a/app/models/inventory_food.rb b/app/models/inventory_food.rb index 60dda6f..ee1273d 100644 --- a/app/models/inventory_food.rb +++ b/app/models/inventory_food.rb @@ -1,4 +1,7 @@ class InventoryFood < ApplicationRecord belongs_to :food belongs_to :inventory + + validates :food_id, presence: true + validates :inventory_id, presence: true end diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 7fd8cc6..1d8ec3f 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -1,4 +1,8 @@ class Recipe < ApplicationRecord belongs_to :user has_many :recipe_foods, dependent: :destroy + + validates :name, presence: true + validates :description, presence: true + validates :user_id, presence: true end diff --git a/app/models/recipe_food.rb b/app/models/recipe_food.rb index de4c992..627482c 100644 --- a/app/models/recipe_food.rb +++ b/app/models/recipe_food.rb @@ -1,4 +1,7 @@ class RecipeFood < ApplicationRecord belongs_to :recipe belongs_to :food + + validates :recipe_id, presence: true + validates :food_id, presence: true end diff --git a/app/views/recipes/index.html.erb b/app/views/recipes/index.html.erb index e680043..425a00e 100644 --- a/app/views/recipes/index.html.erb +++ b/app/views/recipes/index.html.erb @@ -1,3 +1,17 @@ <%= button_to "Sign Out", destroy_user_session_path, method: 'delete', :"data-turbolinks" => false %> -<%= image_tag current_user.image.variant(resize_to_fill: [150, nil]) ? current_user.image : 'https://cdn.pixabay.com/photo/2016/04/01/10/11/avatar-1299805__340.png', class: "rounded-circle mr-2" if current_user %> \ No newline at end of file +<%= image_tag current_user.image.variant(resize_to_fill: [150, nil]) ? current_user.image : 'https://cdn.pixabay.com/photo/2016/04/01/10/11/avatar-1299805__340.png', class: "rounded-circle mr-2" if current_user %> + + +<% @recipes.each do |recipe| %> + <%= link_to recipe.name, recipe_path(recipe.id) %> + <%= recipe.description %> + <%= recipe.preparation_time %> + <%= recipe.cooking_time %> + <%= link_to "Delete", recipe_path(recipe.id), method: :delete, data: { confirm: "Are you sure?" } %> + <%= link_to "Show", recipe_path(recipe.id) %> + <%= link_to "Back", recipes_path %> +<% end %> + + + diff --git a/config/database.yml b/config/database.yml index b4f9972..d62e4a8 100644 --- a/config/database.yml +++ b/config/database.yml @@ -24,6 +24,8 @@ default: &default development: <<: *default database: recipe_app_development + username: postgres + password: root # The specified database role being used to connect to postgres. # To create additional roles in postgres see `$ createuser --help`. diff --git a/db/seeds.rb b/db/seeds.rb index bc25fce..880956a 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,22 @@ # # movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) # Character.create(name: "Luke", movie: movies.first) +Food.create(user_id:1, price:"$9", name:"Mashroom", measurement_unit:"1kg") +Food.create(user_id:1, price:"$5", name:"Tomato salads", measurement_unit:"2kg") +Food.create(user_id:1, price:"$3", name:"Cucumber", measurement_unit:"1kg") +Food.create(user_id:1, price:"$2", name:"Chicken soup", measurement_unit:"1kg") +Food.create(user_id:1, price:"$60", name:"Cake", measurement_unit:"8kg") +Food.create(user_id:1, price:"$30", name:"Fried rice", measurement_unit:"5kg") +Food.create(user_id:1, price:"$10", name:"Roasted meat", measurement_unit:"1kg") + +Recipe.create(name:"Nyama choma", preparation_time:"2 hours", cooking_time:"1 hour", description:"Good food", public:true, user_id:1) +Recipe.create(name:"Chicken Shawama", preparation_time:"3 hours", cooking_time:"1 hour", description:"Tastes yummy", public:true, user_id:1) +Recipe.create(name:"Fried Rice", preparation_time:"2 hours", cooking_time:"1 hour", description:"Good food", public:true, user_id:1) +Recipe.create(name:"French Rice", preparation_time:"1 hour", cooking_time:"2 hours", description:"Nice taste", public:true, user_id:1) +Recipe.create(name:"Eggs", preparation_time:"1 hour", cooking_time:"1 hour", description:"Good taste", public:true, user_id:1) +Recipe.create(name:"Chicken", preparation_time:"2 hours", cooking_time:"1 hour", description:"Good taste", public:true, user_id:1) + +RecipeFood.create(quantity:"10kg", food_id:1, recipe_id:1) + + + From 7b1e6787a3c0200f868ad6bc0fe64ac385d08da7 Mon Sep 17 00:00:00 2001 From: Samuel Mwape <43408618+Mwapsam@users.noreply.github.com> Date: Tue, 17 May 2022 08:57:45 +0200 Subject: [PATCH 02/12] Created the views for recipe --- app/views/recipes/index.html.erb | 3 --- app/views/recipes/show.html.erb | 10 ++++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 app/views/recipes/show.html.erb diff --git a/app/views/recipes/index.html.erb b/app/views/recipes/index.html.erb index 425a00e..90496d5 100644 --- a/app/views/recipes/index.html.erb +++ b/app/views/recipes/index.html.erb @@ -6,11 +6,8 @@ <% @recipes.each do |recipe| %> <%= link_to recipe.name, recipe_path(recipe.id) %> <%= recipe.description %> - <%= recipe.preparation_time %> - <%= recipe.cooking_time %> <%= link_to "Delete", recipe_path(recipe.id), method: :delete, data: { confirm: "Are you sure?" } %> <%= link_to "Show", recipe_path(recipe.id) %> - <%= link_to "Back", recipes_path %> <% end %> diff --git a/app/views/recipes/show.html.erb b/app/views/recipes/show.html.erb new file mode 100644 index 0000000..754f0f5 --- /dev/null +++ b/app/views/recipes/show.html.erb @@ -0,0 +1,10 @@ +
Preparation time: <%= @recipe.preparation_time %>
+Cooking time: <%= @recipe.cooking_time %>
++ <%= link_to "Show this recipe food", recipe_food %> +
+ <% end %> +Preparation time: <%= @recipe.preparation_time %>
-Cooking time: <%= @recipe.cooking_time %>
+Preparation time: <%= @recipe.preparation_time %>
+Cooking time: <%= @recipe.cooking_time %>
+Food | +Name | +Quantity | +Value | +Action | +
---|---|---|---|---|
+ <% if recipe_food.food.image.present? %> + <%= image_tag recipe_food.food.image, class: "float-start bio-photo" %> + <% else %> + <%= image_tag "https://nuwaay.com/wp-content/uploads/2022/01/default_256.png", class: "bio-photo" %> + <% end %> + | +<%= recipe_food.food.name %> | +<%= recipe_food.quantity %> | +$<%= recipe_food.food.price %> | ++ Delete + | +
shv nRRr_5C=<5
zVU=394a+F9cRO)$A8qpRxEO5qpt0wB2ssLNTM`4Hn3mN70wtN9dDBx(Dy2gpFHFi~
zZ|&fx!dCCpc1|YcJ@^!v2<%~g$GBsWa}hRvxaN=YcBpVG%K#r1?f}}iOFU=txP9NV
z4&l_WP+7cGI%@KxN+ZNqO1`p6Gr2W`t+8! xiV!FFD0JMg>lQaN?WL7t|nm>D+ghApt_HXz6>tuOUHxMPnEfj+)!v}f<
z)uYFB=&yU(xaX2ff}v3(AUFZCNK>~hjyJ~jWMG~Mxb7$WGBc4JaYoF<^c+JScg}X(
zG*-K$J)P4?tCjQx^D
m|8OCu{7jr^f2lzZ(UM{|Ma&WMaM={E
2;cm5CgGpy2fBMB70R
z5`e)NfI`THPv`CU)LdD=y5+v1_Dx3m8)OFsrLy8GdS?tPb8NcQ@o^1a$%725
zE}zr?0McO7jFh?+7c`=)g|@Yg^-Dkh08l^vGP~^h#{U5OPS^hc)PMc@kQPIgFIuQu
zU3