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/application_controller.rb b/Training/to_do_list/app/controllers/application_controller.rb index 09705d1..ae02c95 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_by(id: session[:user_id]) + else + @current_user = nil + end + end end 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 new file mode 100644 index 0000000..b73e865 --- /dev/null +++ b/Training/to_do_list/app/controllers/projects_controller.rb @@ -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 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/project.rb b/Training/to_do_list/app/models/project.rb new file mode 100644 index 0000000..afdb881 --- /dev/null +++ b/Training/to_do_list/app/models/project.rb @@ -0,0 +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/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/home/index.html.erb b/Training/to_do_list/app/views/home/index.html.erb index 2085730..771dd2b 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,10 @@ -

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 %>

+ +<%= 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:

+ + +
+ <% end %> + +
+ <%= form.label :name %> + <%= form.text_field :name %> +
+ +
+ <%= form.submit %> +
+<% end %> 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 %> 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

+ + + + + + + + + + + <% @projects.each do |project| %> + + + + + + + <% end %> + +
name
<%= project.name %><%= link_to 'Show', project %><%= link_to 'Edit', edit_project_path(project) %><%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New project', new_project_path %> 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..d0386ca --- /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', projects_path %> 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..45d60df --- /dev/null +++ b/Training/to_do_list/app/views/sessions/new.html.erb @@ -0,0 +1,15 @@ +

<%= 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 a7c7cfe..82b3d2c 100644 --- a/Training/to_do_list/config/routes.rb +++ b/Training/to_do_list/config/routes.rb @@ -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 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/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 71891b6..e08eff9 100644 --- a/Training/to_do_list/db/schema.rb +++ b/Training/to_do_list/db/schema.rb @@ -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" 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 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/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/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 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