diff --git a/app/assets/stylesheets/tasks.css b/app/assets/stylesheets/tasks.css new file mode 100644 index 000000000..e69de29bb diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb new file mode 100644 index 000000000..ded0c87b4 --- /dev/null +++ b/app/controllers/tasks_controller.rb @@ -0,0 +1,96 @@ +# Tasks = [ +# {name: "Honey", description: "go swimming", completed_at: "Sunday evening"}, +# {name: "Calder", description: "do laundry", completed_at: "Monday night"}, +# {name: "Zoe", description: "plant flowers", completed_at: "Monday morning"}, +# {name: "Claire", description: "go to pure barre", completed_at: "yesterday miday"}, +# {name: "Darin", description: "fold cloth", completed_at: "2 min ago"}, +# {name: "Kyle", description: "High Five Somebody You Don't Know", completed_at: "a min ago"}, +# ] + +class TasksController < ApplicationController + def index + @tasks = Task.all + end + + def show + @task = Task.find_by(id: params[:id]) + + if @task.nil? + redirect_to root_path + # head :not_found # you can also put 404 + return + end + end + + def update + @task = Task.find_by(id: params[:id]) + if @task.nil? + redirect_to root_path + return + elsif @task.update(task_params) + redirect_to task_path(@task.id) # go to the index so we can see the task in the list + return + else # save failed :( + render :edit # show the new task form view again + return + end + end + + def edit + @task = Task.find_by(id: params[:id]) + + if @task.nil? + redirect_to root_path + return + end + end + + def destroy + @task = Task.find_by(id: params[:id]) + + if @task.nil? + redirect_to root_path + return + else + @task.destroy + redirect_to root_path + end + end + + def mark_complete + @task = Task.find_by(id: params[:id]) + + if @task.completed_at.nil? + @task.completed_at = Date.today + else + @task.completed_at = nil + end + @task.save + redirect_to root_path + end + + def new + @task = Task.new + + end + + def create + @task = Task.new(task_params) #instantiate a new task + if @task.save # save return true if the DB insert succeeds + # redirect_to task_path # to to the index so we can see the task in the list + redirect_to task_path(@task.id) + return + else # save filled :( + render :new # show the new task form view again + return + end + end + + private + + def task_params + return params.require(:task).permit(:name, :description, :completed_at) + end + + +end diff --git a/app/models/task.rb b/app/models/task.rb new file mode 100644 index 000000000..3c68de7fe --- /dev/null +++ b/app/models/task.rb @@ -0,0 +1,4 @@ +class Task < ApplicationRecord + # t9 = Task.create(name: "Marta", description: "take shower", completed_at: "this morning"), + # t10 = Task.create(name: "Liya", description: "cut nail", completed_at: "5 min ago") +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 7a8dbd9c4..a44b83b86 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -4,12 +4,19 @@ TaskList <%= csrf_meta_tags %> <%= csp_meta_tag %> + + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> - <%= yield %> + +
+

Task List

+ <%= yield %> +
+ diff --git a/app/views/tasks/create.html.erb b/app/views/tasks/create.html.erb new file mode 100644 index 000000000..d3397f87a --- /dev/null +++ b/app/views/tasks/create.html.erb @@ -0,0 +1 @@ +<%= link_to "Tasks", tasks_path %> \ No newline at end of file diff --git a/app/views/tasks/destroy.html.erb b/app/views/tasks/destroy.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/tasks/edit.html.erb b/app/views/tasks/edit.html.erb new file mode 100644 index 000000000..a9809b53b --- /dev/null +++ b/app/views/tasks/edit.html.erb @@ -0,0 +1,16 @@ +

Edit Task

+ +<%= form_with model: @task, class: 'create-task' do |f| %> +

Please provide the following information to edit your task in our database:

+ +
+ <%= f.label :name %> + <%= f.text_field :name %> +
+
+ <%= f.label :description %> + <%= f.text_area :description %> +
+ + <%= f.submit "Edit task", class: "edit-button" %> +<% end %> \ No newline at end of file diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb new file mode 100644 index 000000000..f1109c20a --- /dev/null +++ b/app/views/tasks/index.html.erb @@ -0,0 +1,25 @@ +<%= link_to "Add Task", new_task_path %> + +

Your tasks

+<% if @tasks.length > 0 %> +
    + <% @tasks.each do |task| %> +
  1. +
    <%= link_to(task.name, task_path(task.id) ) %>
    + <%= task.description %> + +

    <%= link_to "Delete Task", task_path(task), method: :delete, class:"btn btn-danger", data: {confirm: "Are you sure you want to delete this task?"} + %>

    + + <% if task.completed_at.nil? %> + <%= link_to "Mark Complete", mark_completed_path(task.id), method: :patch, class:"btn btn-primary" %> + <% else %> +

    Completed On <%= task.completed_at %>

    + <% end %> + +
  2. + <% end %> +
+<% else %> +

We are out of task, can you believe it?!

+<% end %> \ No newline at end of file diff --git a/app/views/tasks/new.html.erb b/app/views/tasks/new.html.erb new file mode 100644 index 000000000..51ba50b20 --- /dev/null +++ b/app/views/tasks/new.html.erb @@ -0,0 +1,15 @@ +

Add new Task

+<%= form_with model: @task, class: 'create-task' do |f| %> +

Please provide the following information to save your task to a database:

+
+ <%= f.label :name %> + <%= f.text_field :name %> +
+ +
+ <%= f.label :description %> + <%= f.text_area :description %> +
+ + <%= f.submit "Save Task", class: "save-button" %> +<% end %> \ No newline at end of file diff --git a/app/views/tasks/show.html.erb b/app/views/tasks/show.html.erb new file mode 100644 index 000000000..de27d3824 --- /dev/null +++ b/app/views/tasks/show.html.erb @@ -0,0 +1,10 @@ +

<%= @task.name %>

+

<%= @task.description %>

+

<%= link_to "Edit Task", edit_task_path(@task) %>

+<% if @task.completed_at.nil?%> +

Not completed

+<% else %> +

Completed on <%= @task.completed_at %>

+<% end %> + +<%= link_to "All Tasks", tasks_path %> \ No newline at end of file diff --git a/app/views/tasks/update.html.erb b/app/views/tasks/update.html.erb new file mode 100644 index 000000000..d242c758e --- /dev/null +++ b/app/views/tasks/update.html.erb @@ -0,0 +1 @@ +<%= link_to "Task", task_path(@task) %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c06383a17..ac627b082 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,21 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html + root to: "tasks#index" + get '/tasks', to:'tasks#index', as: 'tasks' + get '/tasks/new', to: 'tasks#new', as: 'new_task' + post '/tasks', to: 'tasks#create' + + + # routes that deal with a specific task + get 'tasks/:id', to: 'tasks#show', as: 'task' + get '/tasks/:id/edit', to: 'tasks#edit', as: 'edit_task' + patch '/tasks/:id', to: 'tasks#update' + delete '/tasks/:id', to: 'tasks#destroy' + # This is a custom route, and in this case, the mark_completed! + patch '/tasks/:id/mark_complete', to: 'tasks#mark_complete', as: 'mark_completed' end + + + + + \ No newline at end of file diff --git a/db/migrate/20200512050603_create_tasks.rb b/db/migrate/20200512050603_create_tasks.rb new file mode 100644 index 000000000..2b277b0e9 --- /dev/null +++ b/db/migrate/20200512050603_create_tasks.rb @@ -0,0 +1,11 @@ +class CreateTasks < ActiveRecord::Migration[6.0] + def change + create_table :tasks do |t| + t.string :name + t.string :description + t.string :completed_at + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..2e177038b --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,26 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2020_05_12_050603) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "tasks", force: :cascade do |t| + t.string "name" + t.string "description" + t.string "completed_at" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + +end diff --git a/test/controllers/tasks_controller_test.rb b/test/controllers/tasks_controller_test.rb index 8746f3597..ac521cd33 100644 --- a/test/controllers/tasks_controller_test.rb +++ b/test/controllers/tasks_controller_test.rb @@ -28,7 +28,7 @@ # Unskip these tests for Wave 2 describe "show" do it "can get a valid task" do - skip + # Act get task_path(task.id) @@ -37,7 +37,7 @@ end it "will redirect for an invalid task" do - skip + # Act get task_path(-1) @@ -48,7 +48,7 @@ describe "new" do it "can get the new task page" do - skip + # Act get new_task_path @@ -60,7 +60,7 @@ describe "create" do it "can create a new task" do - skip + # Arrange task_hash = { @@ -83,42 +83,97 @@ must_respond_with :redirect must_redirect_to task_path(new_task.id) end + + it "adds a new task to database " do + task_hash = { + task: { + name: "new task", + description: "new task description", + completed_at: nil, + }, + } + expect{post tasks_path, params: task_hash}.must_differ "Task.count", 1 + end end # Unskip and complete these tests for Wave 3 describe "edit" do it "can get the edit page for an existing task" do - skip - # Your code here + # Act + Task.create(name: "Task 94", description: "new task test description") + task = Task.first + # Arrange + get edit_task_path(task.id) + # Assert + must_respond_with :success end it "will respond with redirect when attempting to edit a nonexistant task" do - skip + # Your code here + get edit_task_path(-1) + + # Assert + must_respond_with :redirect + end end # Uncomment and complete these tests for Wave 3 describe "update" do # Note: If there was a way to fail to save the changes to a task, that would be a great - # thing to test. + # thing to test. it "can update an existing task" do - # Your code here + new_task = Task.create(name: "Task 97", description: "new task test description") + task_hash = { + task: { + name: "fresh task", + description: "descrition of fresh task" + }, + } + expect { + patch task_path(new_task.id), params: task_hash + }.must_differ "Task.count", 0 + + must_redirect_to task_path + expect(Task.last.name).must_equal task_hash[:task][:name] + expect(Task.last.description).must_equal task_hash[:task][:description] end it "will redirect to the root page if given an invalid id" do - # Your code here + task_hash = { + task: { + name: "new task name", + description: "new task name description" + }, + } + patch task_path(-1), params: task_hash + must_redirect_to root_path end end # Complete these tests for Wave 4 describe "destroy" do # Your tests go here - + it "can delete existing task" do + new_task = Task.create(name: "Task 97", description: "task description") + + expect { + delete task_path(new_task.id) + }.must_change "Task.count", -1 + absent = Task.find_by(id: new_task.id) + expect(absent).must_be_nil + end + + it "will redirect to the root page if given an invalide id" do + delete task_path(-1) + must_redirect_to root_path + end end # Complete for Wave 4 describe "toggle_complete" do # Your tests go here + it "show date completion upon Completion " end end