Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Training/to_do_list/app/assets/javascripts/sessions.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions Training/to_do_list/app/assets/stylesheets/sessions.scss
Original file line number Diff line number Diff line change
@@ -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/
11 changes: 11 additions & 0 deletions Training/to_do_list/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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_by(id: session[:user_id])
else
@current_user = nil
end
end
end
1 change: 1 addition & 0 deletions Training/to_do_list/app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class HomeController < ApplicationController
def index
@projects = Project.all
end
end
61 changes: 61 additions & 0 deletions Training/to_do_list/app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]

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

def edit
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

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
params.require(:project).permit(:name)
end

def set_project
@project = Project.find(params[:id])
end
end
21 changes: 21 additions & 0 deletions Training/to_do_list/app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions Training/to_do_list/app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SessionsHelper
end
5 changes: 5 additions & 0 deletions Training/to_do_list/app/models/project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Project < ApplicationRecord
validates :name, presence: true, uniqueness: true

has_many :tasks
end
5 changes: 5 additions & 0 deletions Training/to_do_list/app/models/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Task < ApplicationRecord
validates :name, presence: true, uniqueness: true

belongs_to :project
end
2 changes: 2 additions & 0 deletions Training/to_do_list/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class User < ApplicationRecord
has_secure_password

validates :email, presence: true, uniqueness: true
end
12 changes: 10 additions & 2 deletions Training/to_do_list/app/views/home/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<% 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 %>
<p id="notice"><%= notice %></p>

<%= render template: 'projects/index', projects: @projects %>
22 changes: 22 additions & 0 deletions Training/to_do_list/app/views/projects/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%= form_with(model: project, local: true) do |form| %>
<% if project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(project.errors.count, "error") %> prohibited this project from being saved:</h2>

<ul>
<% project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions Training/to_do_list/app/views/projects/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing project</h1>

<%= render 'form', project: @project %>

<%= link_to 'Show', @project %> |
<%= link_to 'Back', projects_path %>
27 changes: 27 additions & 0 deletions Training/to_do_list/app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<p id="notice"><%= notice %></p>

<h1>projects</h1>

<table>
<thead>
<tr>
<th>name</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @projects.each do |project| %>
<tr>
<td><%= project.name %></td>
<td><%= link_to 'Show', project %></td>
<td><%= link_to 'Edit', edit_project_path(project) %></td>
<td><%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New project', new_project_path %>
5 changes: 5 additions & 0 deletions Training/to_do_list/app/views/projects/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New project</h1>

<%= render 'form', project: @project %>

<%= link_to 'Back', projects_path %>
9 changes: 9 additions & 0 deletions Training/to_do_list/app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p id="notice"><%= notice %></p>

<p>
<strong>name:</strong>
<%= @project.name %>
</p>

<%= link_to 'Edit', edit_project_path(@project) %> |
<%= link_to 'Back', projects_path %>
2 changes: 2 additions & 0 deletions Training/to_do_list/app/views/sessions/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Sessions#create</h1>
<p>Find me in app/views/sessions/create.html.erb</p>
2 changes: 2 additions & 0 deletions Training/to_do_list/app/views/sessions/destroy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Sessions#destroy</h1>
<p>Find me in app/views/sessions/destroy.html.erb</p>
15 changes: 15 additions & 0 deletions Training/to_do_list/app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<p id="alert"><%= alert %></p>
<h1>Login</h1>
<%= form_tag sessions_path do |form| %>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<div class="actions">
<%= submit_tag "Login" %>
</div>
<% end %>
10 changes: 8 additions & 2 deletions Training/to_do_list/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Rails.application.routes.draw do
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]
resources :projects

get 'signup', to: 'users#new', as: 'signup'
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
end
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions Training/to_do_list/db/migrate/20200507203141_create_tasks.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddProjectToTasks < ActiveRecord::Migration[5.2]
def change
add_reference :tasks, :project, foreign_key: true
end
end
17 changes: 16 additions & 1 deletion Training/to_do_list/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@
#
# 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_07_204348) 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 "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"
Expand Down
19 changes: 19 additions & 0 deletions Training/to_do_list/test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions Training/to_do_list/test/fixtures/projects.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: MyString

two:
name: MyString
9 changes: 9 additions & 0 deletions Training/to_do_list/test/fixtures/tasks.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions Training/to_do_list/test/models/project_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ProjectTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions Training/to_do_list/test/models/task_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TaskTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end