From c43c5cdb3470c35f2c0be0c7ec45108e21da9440 Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Wed, 6 May 2020 16:20:30 +0300
Subject: [PATCH 01/11] Add session functionality
---
.../app/assets/javascripts/sessions.coffee | 3 +++
.../app/assets/stylesheets/sessions.scss | 3 +++
.../app/controllers/sessions_controller.rb | 21 +++++++++++++++++++
.../to_do_list/app/helpers/sessions_helper.rb | 2 ++
Training/to_do_list/app/models/user.rb | 2 ++
.../app/views/sessions/create.html.erb | 2 ++
.../app/views/sessions/destroy.html.erb | 2 ++
.../app/views/sessions/new.html.erb | 2 ++
Training/to_do_list/config/routes.rb | 3 +++
.../controllers/sessions_controller_test.rb | 19 +++++++++++++++++
10 files changed, 59 insertions(+)
create mode 100644 Training/to_do_list/app/assets/javascripts/sessions.coffee
create mode 100644 Training/to_do_list/app/assets/stylesheets/sessions.scss
create mode 100644 Training/to_do_list/app/controllers/sessions_controller.rb
create mode 100644 Training/to_do_list/app/helpers/sessions_helper.rb
create mode 100644 Training/to_do_list/app/views/sessions/create.html.erb
create mode 100644 Training/to_do_list/app/views/sessions/destroy.html.erb
create mode 100644 Training/to_do_list/app/views/sessions/new.html.erb
create mode 100644 Training/to_do_list/test/controllers/sessions_controller_test.rb
diff --git a/Training/to_do_list/app/assets/javascripts/sessions.coffee b/Training/to_do_list/app/assets/javascripts/sessions.coffee
new file mode 100644
index 0000000..24f83d1
--- /dev/null
+++ b/Training/to_do_list/app/assets/javascripts/sessions.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/
diff --git a/Training/to_do_list/app/assets/stylesheets/sessions.scss b/Training/to_do_list/app/assets/stylesheets/sessions.scss
new file mode 100644
index 0000000..7bef9cf
--- /dev/null
+++ b/Training/to_do_list/app/assets/stylesheets/sessions.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the sessions controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/Training/to_do_list/app/controllers/sessions_controller.rb b/Training/to_do_list/app/controllers/sessions_controller.rb
new file mode 100644
index 0000000..72a27e9
--- /dev/null
+++ b/Training/to_do_list/app/controllers/sessions_controller.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+class SessionsController < ApplicationController
+ def new; end
+
+ def create
+ user = User.find_by_email(params[:email])
+ if user&.authenticate(params[:password])
+ session[:user_id] = user.id
+ redirect_to root_url, notice: 'Logged in!'
+ else
+ flash.now[:alert] = 'Email or password is invalid'
+ render 'new'
+ end
+ end
+
+ def destroy
+ session[:user_id] = nil
+ redirect_to root_url, notice: 'Logged out!'
+ end
+end
diff --git a/Training/to_do_list/app/helpers/sessions_helper.rb b/Training/to_do_list/app/helpers/sessions_helper.rb
new file mode 100644
index 0000000..309f8b2
--- /dev/null
+++ b/Training/to_do_list/app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
diff --git a/Training/to_do_list/app/models/user.rb b/Training/to_do_list/app/models/user.rb
index d67da20..527cea1 100644
--- a/Training/to_do_list/app/models/user.rb
+++ b/Training/to_do_list/app/models/user.rb
@@ -1,3 +1,5 @@
class User < ApplicationRecord
has_secure_password
+
+ validates :email, presence: true, uniqueness: true
end
diff --git a/Training/to_do_list/app/views/sessions/create.html.erb b/Training/to_do_list/app/views/sessions/create.html.erb
new file mode 100644
index 0000000..c251174
--- /dev/null
+++ b/Training/to_do_list/app/views/sessions/create.html.erb
@@ -0,0 +1,2 @@
+Sessions#create
+Find me in app/views/sessions/create.html.erb
diff --git a/Training/to_do_list/app/views/sessions/destroy.html.erb b/Training/to_do_list/app/views/sessions/destroy.html.erb
new file mode 100644
index 0000000..d75237d
--- /dev/null
+++ b/Training/to_do_list/app/views/sessions/destroy.html.erb
@@ -0,0 +1,2 @@
+Sessions#destroy
+Find me in app/views/sessions/destroy.html.erb
diff --git a/Training/to_do_list/app/views/sessions/new.html.erb b/Training/to_do_list/app/views/sessions/new.html.erb
new file mode 100644
index 0000000..b39a3bc
--- /dev/null
+++ b/Training/to_do_list/app/views/sessions/new.html.erb
@@ -0,0 +1,2 @@
+Sessions#new
+Find me in app/views/sessions/new.html.erb
diff --git a/Training/to_do_list/config/routes.rb b/Training/to_do_list/config/routes.rb
index a7c7cfe..a401a4f 100644
--- a/Training/to_do_list/config/routes.rb
+++ b/Training/to_do_list/config/routes.rb
@@ -1,4 +1,7 @@
Rails.application.routes.draw do
+ get 'sessions/new'
+ get 'sessions/create'
+ get 'sessions/destroy'
resources :users
root 'home#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
diff --git a/Training/to_do_list/test/controllers/sessions_controller_test.rb b/Training/to_do_list/test/controllers/sessions_controller_test.rb
new file mode 100644
index 0000000..c6251b0
--- /dev/null
+++ b/Training/to_do_list/test/controllers/sessions_controller_test.rb
@@ -0,0 +1,19 @@
+require 'test_helper'
+
+class SessionsControllerTest < ActionDispatch::IntegrationTest
+ test "should get new" do
+ get sessions_new_url
+ assert_response :success
+ end
+
+ test "should get create" do
+ get sessions_create_url
+ assert_response :success
+ end
+
+ test "should get destroy" do
+ get sessions_destroy_url
+ assert_response :success
+ end
+
+end
From 8e98df457a9141de0ef63e75c7bd4653c112201a Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Wed, 6 May 2020 16:29:54 +0300
Subject: [PATCH 02/11] Add logic in new session template and refactor routes
---
.../to_do_list/app/views/sessions/new.html.erb | 17 +++++++++++++++--
Training/to_do_list/config/routes.rb | 11 ++++++-----
2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/Training/to_do_list/app/views/sessions/new.html.erb b/Training/to_do_list/app/views/sessions/new.html.erb
index b39a3bc..45d60df 100644
--- a/Training/to_do_list/app/views/sessions/new.html.erb
+++ b/Training/to_do_list/app/views/sessions/new.html.erb
@@ -1,2 +1,15 @@
-Sessions#new
-Find me in app/views/sessions/new.html.erb
+<%= alert %>
+Login
+<%= form_tag sessions_path do |form| %>
+
+ <%= label_tag :email %>
+ <%= text_field_tag :email %>
+
+
+ <%= label_tag :password %>
+ <%= password_field_tag :password %>
+
+
+ <%= submit_tag "Login" %>
+
+<% end %>
diff --git a/Training/to_do_list/config/routes.rb b/Training/to_do_list/config/routes.rb
index a401a4f..0494c4c 100644
--- a/Training/to_do_list/config/routes.rb
+++ b/Training/to_do_list/config/routes.rb
@@ -1,8 +1,9 @@
Rails.application.routes.draw do
- get 'sessions/new'
- get 'sessions/create'
- get 'sessions/destroy'
- resources :users
root 'home#index'
- # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
+
+ resources :users
+ resources :sessions, only: [:new, :create, :destroy]
+ get 'signup', to: 'users#new', as: 'signup'
+ get 'login', to: 'sessions#new', as: 'login'
+ get 'logout', to: 'sessions#destroy', as: 'logout'
end
From 16bf5227a06c72358030e81d67d1a5398865004e Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Wed, 6 May 2020 16:31:12 +0300
Subject: [PATCH 03/11] Add #current_user in ApplicationController
---
.../app/controllers/application_controller.rb | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/Training/to_do_list/app/controllers/application_controller.rb b/Training/to_do_list/app/controllers/application_controller.rb
index 09705d1..d795f9f 100644
--- a/Training/to_do_list/app/controllers/application_controller.rb
+++ b/Training/to_do_list/app/controllers/application_controller.rb
@@ -1,2 +1,13 @@
+# frozen_string_literal: true
+
class ApplicationController < ActionController::Base
+ helper_method :current_user
+
+ def current_user
+ if session[:user_id]
+ @current_user ||= User.find(session[:user_id])
+ else
+ @current_user = nil
+ end
+ end
end
From a4743e3eeed3eb7df12b04df1d72adc96cc0b04d Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Wed, 6 May 2020 18:59:31 +0300
Subject: [PATCH 04/11] Add logic to diplay if current_user is existing
---
Training/to_do_list/app/views/home/index.html.erb | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/Training/to_do_list/app/views/home/index.html.erb b/Training/to_do_list/app/views/home/index.html.erb
index 2085730..adeb8e9 100644
--- a/Training/to_do_list/app/views/home/index.html.erb
+++ b/Training/to_do_list/app/views/home/index.html.erb
@@ -1,2 +1,8 @@
-Home#index
-Find me in app/views/home/index.html.erb
+<% if current_user %>
+ Logged in as <%= current_user.email %>.
+ <%= link_to "Log Out", logout_path %>
+<% else %>
+ <%= link_to "Sign Up", signup_path %> or
+ <%= link_to "Log In", login_path %>
+<% end %>
+<%= notice %>
From dbe93e76721b46848c5e4b26ae7500b2416f61e7 Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Thu, 7 May 2020 12:51:22 +0300
Subject: [PATCH 05/11] Add Project model
---
Training/to_do_list/app/models/project.rb | 2 ++
.../db/migrate/20200506163019_create_projects.rb | 9 +++++++++
Training/to_do_list/db/schema.rb | 8 +++++++-
Training/to_do_list/test/fixtures/projects.yml | 7 +++++++
Training/to_do_list/test/models/project_test.rb | 7 +++++++
5 files changed, 32 insertions(+), 1 deletion(-)
create mode 100644 Training/to_do_list/app/models/project.rb
create mode 100644 Training/to_do_list/db/migrate/20200506163019_create_projects.rb
create mode 100644 Training/to_do_list/test/fixtures/projects.yml
create mode 100644 Training/to_do_list/test/models/project_test.rb
diff --git a/Training/to_do_list/app/models/project.rb b/Training/to_do_list/app/models/project.rb
new file mode 100644
index 0000000..9c7fe3f
--- /dev/null
+++ b/Training/to_do_list/app/models/project.rb
@@ -0,0 +1,2 @@
+class Project < ApplicationRecord
+end
diff --git a/Training/to_do_list/db/migrate/20200506163019_create_projects.rb b/Training/to_do_list/db/migrate/20200506163019_create_projects.rb
new file mode 100644
index 0000000..2fbee86
--- /dev/null
+++ b/Training/to_do_list/db/migrate/20200506163019_create_projects.rb
@@ -0,0 +1,9 @@
+class CreateProjects < ActiveRecord::Migration[5.2]
+ def change
+ create_table :projects do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
diff --git a/Training/to_do_list/db/schema.rb b/Training/to_do_list/db/schema.rb
index 71891b6..4eafabf 100644
--- a/Training/to_do_list/db/schema.rb
+++ b/Training/to_do_list/db/schema.rb
@@ -10,7 +10,13 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 2020_05_06_125331) do
+ActiveRecord::Schema.define(version: 2020_05_06_163019) do
+
+ create_table "projects", force: :cascade do |t|
+ t.string "name"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
create_table "users", force: :cascade do |t|
t.string "email"
diff --git a/Training/to_do_list/test/fixtures/projects.yml b/Training/to_do_list/test/fixtures/projects.yml
new file mode 100644
index 0000000..56066c6
--- /dev/null
+++ b/Training/to_do_list/test/fixtures/projects.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ name: MyString
+
+two:
+ name: MyString
diff --git a/Training/to_do_list/test/models/project_test.rb b/Training/to_do_list/test/models/project_test.rb
new file mode 100644
index 0000000..0821e1f
--- /dev/null
+++ b/Training/to_do_list/test/models/project_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ProjectTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
From 334c6d2d3d56cff34600c3a74ece1a69c63bd2a3 Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Thu, 7 May 2020 13:02:14 +0300
Subject: [PATCH 06/11] Add projects controller and index view
---
.../app/controllers/projects_controller.rb | 29 +++++++++++++++++++
Training/to_do_list/app/models/project.rb | 1 +
.../app/views/projects/index.html.erb | 27 +++++++++++++++++
Training/to_do_list/config/routes.rb | 2 ++
4 files changed, 59 insertions(+)
create mode 100644 Training/to_do_list/app/controllers/projects_controller.rb
create mode 100644 Training/to_do_list/app/views/projects/index.html.erb
diff --git a/Training/to_do_list/app/controllers/projects_controller.rb b/Training/to_do_list/app/controllers/projects_controller.rb
new file mode 100644
index 0000000..66edd6d
--- /dev/null
+++ b/Training/to_do_list/app/controllers/projects_controller.rb
@@ -0,0 +1,29 @@
+class ProjectsController < ApplicationController
+ def index
+ @projects = Project.all
+ end
+
+ def new
+ @project = Project.new
+ end
+
+ def create
+ @project = Project.new(project_params)
+
+ respond_to do |format|
+ if @project.save
+ format.html { redirect_to @project, notice: 'Project was successfully created.' }
+ format.json { render :show, status: :created, location: @project }
+ else
+ format.html { render :new }
+ format.json { render json: @project.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ private
+
+ def project_params
+ params.require(:project).permit(:name)
+ end
+end
diff --git a/Training/to_do_list/app/models/project.rb b/Training/to_do_list/app/models/project.rb
index 9c7fe3f..b95bd4b 100644
--- a/Training/to_do_list/app/models/project.rb
+++ b/Training/to_do_list/app/models/project.rb
@@ -1,2 +1,3 @@
class Project < ApplicationRecord
+ validates :name, presence: true, uniqueness: true
end
diff --git a/Training/to_do_list/app/views/projects/index.html.erb b/Training/to_do_list/app/views/projects/index.html.erb
new file mode 100644
index 0000000..363e6bf
--- /dev/null
+++ b/Training/to_do_list/app/views/projects/index.html.erb
@@ -0,0 +1,27 @@
+<%= notice %>
+
+projects
+
+
+
+
+ | name |
+ |
+
+
+
+
+ <% @projects.each do |project| %>
+
+ | <%= project.name %> |
+ <%= link_to 'Show', project %> |
+ <%= link_to 'Edit', edit_project_path(project) %> |
+ <%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %> |
+
+ <% end %>
+
+
+
+
+
+<%= link_to 'New project', new_project_path %>
diff --git a/Training/to_do_list/config/routes.rb b/Training/to_do_list/config/routes.rb
index 0494c4c..82b3d2c 100644
--- a/Training/to_do_list/config/routes.rb
+++ b/Training/to_do_list/config/routes.rb
@@ -3,6 +3,8 @@
resources :users
resources :sessions, only: [:new, :create, :destroy]
+ resources :projects
+
get 'signup', to: 'users#new', as: 'signup'
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
From 2e40d367ced06a882fa55fcc9aa86595d56f75c8 Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Thu, 7 May 2020 23:19:09 +0300
Subject: [PATCH 07/11] Add missing templates at projects folder. Add #destroy
at ProjectController. Render projects list at homepage
---
.../app/controllers/home_controller.rb | 1 +
.../app/controllers/projects_controller.rb | 17 ++++++++++++++
.../to_do_list/app/views/home/index.html.erb | 2 ++
.../app/views/projects/_form.html.erb | 22 +++++++++++++++++++
.../app/views/projects/new.html.erb | 5 +++++
.../app/views/projects/show.html.erb | 9 ++++++++
6 files changed, 56 insertions(+)
create mode 100644 Training/to_do_list/app/views/projects/_form.html.erb
create mode 100644 Training/to_do_list/app/views/projects/new.html.erb
create mode 100644 Training/to_do_list/app/views/projects/show.html.erb
diff --git a/Training/to_do_list/app/controllers/home_controller.rb b/Training/to_do_list/app/controllers/home_controller.rb
index 95f2992..be5529b 100644
--- a/Training/to_do_list/app/controllers/home_controller.rb
+++ b/Training/to_do_list/app/controllers/home_controller.rb
@@ -1,4 +1,5 @@
class HomeController < ApplicationController
def index
+ @projects = Project.all
end
end
diff --git a/Training/to_do_list/app/controllers/projects_controller.rb b/Training/to_do_list/app/controllers/projects_controller.rb
index 66edd6d..7160cb9 100644
--- a/Training/to_do_list/app/controllers/projects_controller.rb
+++ b/Training/to_do_list/app/controllers/projects_controller.rb
@@ -1,4 +1,6 @@
class ProjectsController < ApplicationController
+ before_action :set_project, only: [:show, :edit, :update, :destroy]
+
def index
@projects = Project.all
end
@@ -21,9 +23,24 @@ def create
end
end
+ def show
+ end
+
+ def destroy
+ @project.destroy
+ respond_to do |format|
+ format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
+ format.json { head :no_content }
+ end
+ end
+
private
def project_params
params.require(:project).permit(:name)
end
+
+ def set_project
+ @project = Project.find(params[:id])
+ end
end
diff --git a/Training/to_do_list/app/views/home/index.html.erb b/Training/to_do_list/app/views/home/index.html.erb
index adeb8e9..771dd2b 100644
--- a/Training/to_do_list/app/views/home/index.html.erb
+++ b/Training/to_do_list/app/views/home/index.html.erb
@@ -6,3 +6,5 @@
<%= link_to "Log In", login_path %>
<% end %>
<%= notice %>
+
+<%= render template: 'projects/index', projects: @projects %>
diff --git a/Training/to_do_list/app/views/projects/_form.html.erb b/Training/to_do_list/app/views/projects/_form.html.erb
new file mode 100644
index 0000000..f53c6c1
--- /dev/null
+++ b/Training/to_do_list/app/views/projects/_form.html.erb
@@ -0,0 +1,22 @@
+<%= form_with(model: project, local: true) do |form| %>
+ <% if project.errors.any? %>
+
+
<%= pluralize(project.errors.count, "error") %> prohibited this project from being saved:
+
+
+ <% project.errors.full_messages.each do |message| %>
+ - <%= message %>
+ <% end %>
+
+
+ <% end %>
+
+
+ <%= form.label :name %>
+ <%= form.text_field :name %>
+
+
+
+ <%= form.submit %>
+
+<% end %>
diff --git a/Training/to_do_list/app/views/projects/new.html.erb b/Training/to_do_list/app/views/projects/new.html.erb
new file mode 100644
index 0000000..92bf4f0
--- /dev/null
+++ b/Training/to_do_list/app/views/projects/new.html.erb
@@ -0,0 +1,5 @@
+New project
+
+<%= render 'form', project: @project %>
+
+<%= link_to 'Back', projects_path %>
diff --git a/Training/to_do_list/app/views/projects/show.html.erb b/Training/to_do_list/app/views/projects/show.html.erb
new file mode 100644
index 0000000..d645d7c
--- /dev/null
+++ b/Training/to_do_list/app/views/projects/show.html.erb
@@ -0,0 +1,9 @@
+<%= notice %>
+
+
+ name:
+ <%= @project.name %>
+
+
+<%= link_to 'Edit', edit_project_path(@project) %> |
+<%= link_to 'Back', users_path %>
From 8fdede79203d9d03994c2274b01753de60108211 Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Thu, 7 May 2020 23:23:09 +0300
Subject: [PATCH 08/11] Fix wrong back redirection at show project template
---
Training/to_do_list/app/views/projects/show.html.erb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Training/to_do_list/app/views/projects/show.html.erb b/Training/to_do_list/app/views/projects/show.html.erb
index d645d7c..d0386ca 100644
--- a/Training/to_do_list/app/views/projects/show.html.erb
+++ b/Training/to_do_list/app/views/projects/show.html.erb
@@ -6,4 +6,4 @@
<%= link_to 'Edit', edit_project_path(@project) %> |
-<%= link_to 'Back', users_path %>
+<%= link_to 'Back', projects_path %>
From 42bab28786150593f2d0a64d9f2e5f1444ba7455 Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Thu, 7 May 2020 23:23:36 +0300
Subject: [PATCH 09/11] Add #update and #edit for a project object
---
.../app/controllers/projects_controller.rb | 17 ++++++++++++++++-
.../to_do_list/app/views/projects/edit.html.erb | 6 ++++++
2 files changed, 22 insertions(+), 1 deletion(-)
create mode 100644 Training/to_do_list/app/views/projects/edit.html.erb
diff --git a/Training/to_do_list/app/controllers/projects_controller.rb b/Training/to_do_list/app/controllers/projects_controller.rb
index 7160cb9..b73e865 100644
--- a/Training/to_do_list/app/controllers/projects_controller.rb
+++ b/Training/to_do_list/app/controllers/projects_controller.rb
@@ -23,7 +23,7 @@ def create
end
end
- def show
+ def edit
end
def destroy
@@ -34,6 +34,21 @@ def destroy
end
end
+ def show
+ end
+
+ def update
+ respond_to do |format|
+ if @project.update(project_params)
+ format.html { redirect_to @project, notice: 'Project was successfully updated.' }
+ format.json { render :show, status: :ok, location: @project }
+ else
+ format.html { render :edit }
+ format.json { render json: @project.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
private
def project_params
diff --git a/Training/to_do_list/app/views/projects/edit.html.erb b/Training/to_do_list/app/views/projects/edit.html.erb
new file mode 100644
index 0000000..d05a6ba
--- /dev/null
+++ b/Training/to_do_list/app/views/projects/edit.html.erb
@@ -0,0 +1,6 @@
+Editing project
+
+<%= render 'form', project: @project %>
+
+<%= link_to 'Show', @project %> |
+<%= link_to 'Back', projects_path %>
From 0b22b8ab8cc9cb3ec10b62b62bd9a39b6e9c2a71 Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Thu, 7 May 2020 23:27:20 +0300
Subject: [PATCH 10/11] Use find_by insted of find to handle case when session
exists, but user is destoyed
---
Training/to_do_list/app/controllers/application_controller.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Training/to_do_list/app/controllers/application_controller.rb b/Training/to_do_list/app/controllers/application_controller.rb
index d795f9f..ae02c95 100644
--- a/Training/to_do_list/app/controllers/application_controller.rb
+++ b/Training/to_do_list/app/controllers/application_controller.rb
@@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base
def current_user
if session[:user_id]
- @current_user ||= User.find(session[:user_id])
+ @current_user ||= User.find_by(id: session[:user_id])
else
@current_user = nil
end
From 6be1c45c70a2f19f57b5ca802a8cc40d19418c78 Mon Sep 17 00:00:00 2001
From: slavunderkind
Date: Thu, 7 May 2020 23:45:27 +0300
Subject: [PATCH 11/11] Add task model. Add association between task and
project
---
Training/to_do_list/app/models/project.rb | 2 ++
Training/to_do_list/app/models/task.rb | 5 +++++
.../db/migrate/20200507203141_create_tasks.rb | 10 ++++++++++
.../db/migrate/20200507204348_add_project_to_tasks.rb | 5 +++++
Training/to_do_list/db/schema.rb | 11 ++++++++++-
Training/to_do_list/test/fixtures/tasks.yml | 9 +++++++++
Training/to_do_list/test/models/task_test.rb | 7 +++++++
7 files changed, 48 insertions(+), 1 deletion(-)
create mode 100644 Training/to_do_list/app/models/task.rb
create mode 100644 Training/to_do_list/db/migrate/20200507203141_create_tasks.rb
create mode 100644 Training/to_do_list/db/migrate/20200507204348_add_project_to_tasks.rb
create mode 100644 Training/to_do_list/test/fixtures/tasks.yml
create mode 100644 Training/to_do_list/test/models/task_test.rb
diff --git a/Training/to_do_list/app/models/project.rb b/Training/to_do_list/app/models/project.rb
index b95bd4b..afdb881 100644
--- a/Training/to_do_list/app/models/project.rb
+++ b/Training/to_do_list/app/models/project.rb
@@ -1,3 +1,5 @@
class Project < ApplicationRecord
validates :name, presence: true, uniqueness: true
+
+ has_many :tasks
end
diff --git a/Training/to_do_list/app/models/task.rb b/Training/to_do_list/app/models/task.rb
new file mode 100644
index 0000000..1b863b0
--- /dev/null
+++ b/Training/to_do_list/app/models/task.rb
@@ -0,0 +1,5 @@
+class Task < ApplicationRecord
+ validates :name, presence: true, uniqueness: true
+
+ belongs_to :project
+end
diff --git a/Training/to_do_list/db/migrate/20200507203141_create_tasks.rb b/Training/to_do_list/db/migrate/20200507203141_create_tasks.rb
new file mode 100644
index 0000000..fc813f4
--- /dev/null
+++ b/Training/to_do_list/db/migrate/20200507203141_create_tasks.rb
@@ -0,0 +1,10 @@
+class CreateTasks < ActiveRecord::Migration[5.2]
+ def change
+ create_table :tasks do |t|
+ t.string :name
+ t.integer :priority_rank, default: 0
+
+ t.timestamps
+ end
+ end
+end
diff --git a/Training/to_do_list/db/migrate/20200507204348_add_project_to_tasks.rb b/Training/to_do_list/db/migrate/20200507204348_add_project_to_tasks.rb
new file mode 100644
index 0000000..f3444c8
--- /dev/null
+++ b/Training/to_do_list/db/migrate/20200507204348_add_project_to_tasks.rb
@@ -0,0 +1,5 @@
+class AddProjectToTasks < ActiveRecord::Migration[5.2]
+ def change
+ add_reference :tasks, :project, foreign_key: true
+ end
+end
diff --git a/Training/to_do_list/db/schema.rb b/Training/to_do_list/db/schema.rb
index 4eafabf..e08eff9 100644
--- a/Training/to_do_list/db/schema.rb
+++ b/Training/to_do_list/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 2020_05_06_163019) do
+ActiveRecord::Schema.define(version: 2020_05_07_204348) do
create_table "projects", force: :cascade do |t|
t.string "name"
@@ -18,6 +18,15 @@
t.datetime "updated_at", null: false
end
+ create_table "tasks", force: :cascade do |t|
+ t.string "name"
+ t.integer "priority_rank", default: 0
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.integer "project_id"
+ t.index ["project_id"], name: "index_tasks_on_project_id"
+ end
+
create_table "users", force: :cascade do |t|
t.string "email"
t.string "password_digest"
diff --git a/Training/to_do_list/test/fixtures/tasks.yml b/Training/to_do_list/test/fixtures/tasks.yml
new file mode 100644
index 0000000..ee4123c
--- /dev/null
+++ b/Training/to_do_list/test/fixtures/tasks.yml
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ name: MyString
+ priority_rank: 1
+
+two:
+ name: MyString
+ priority_rank: 1
diff --git a/Training/to_do_list/test/models/task_test.rb b/Training/to_do_list/test/models/task_test.rb
new file mode 100644
index 0000000..3ca2159
--- /dev/null
+++ b/Training/to_do_list/test/models/task_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TaskTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end