From 7a8311596de269bd43cda92996dc56ac014e5d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannah=20Teki=C3=A9?= Date: Tue, 12 May 2020 11:52:32 -0700 Subject: [PATCH 1/4] did till wave two for the 3rd time, test is not working, Jared is helping --- app/controllers/tasks_controller.rb | 76 +++++++++++++++++++++++ app/models/task.rb | 4 ++ app/views/tasks/create.html.erb | 1 + app/views/tasks/destroy.html.erb | 0 app/views/tasks/edit.html.erb | 0 app/views/tasks/index.html.erb | 17 +++++ app/views/tasks/new.html.erb | 12 ++++ app/views/tasks/show.html.erb | 4 ++ app/views/tasks/update.html.erb | 0 config/routes.rb | 18 ++++++ db/migrate/20200512050603_create_tasks.rb | 11 ++++ db/schema.rb | 18 ++++++ 12 files changed, 161 insertions(+) create mode 100644 app/controllers/tasks_controller.rb create mode 100644 app/models/task.rb create mode 100644 app/views/tasks/create.html.erb create mode 100644 app/views/tasks/destroy.html.erb create mode 100644 app/views/tasks/edit.html.erb create mode 100644 app/views/tasks/index.html.erb create mode 100644 app/views/tasks/new.html.erb create mode 100644 app/views/tasks/show.html.erb create mode 100644 app/views/tasks/update.html.erb create mode 100644 db/migrate/20200512050603_create_tasks.rb create mode 100644 db/schema.rb diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb new file mode 100644 index 000000000..48840c159 --- /dev/null +++ b/app/controllers/tasks_controller.rb @@ -0,0 +1,76 @@ +# 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_to tasks_path + # head :not_found # you can also put 404 + return + end + end + + def update + @task = Task.find_by(id: params[:id]) + if @task.nil? + head :not_found + return + elsif @task.update(task_params) + redirect_to tasks_path # 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? + head :not_found + return + end + end + + def destroy + 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/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..e69de29bb diff --git a/app/views/tasks/index.html.erb b/app/views/tasks/index.html.erb new file mode 100644 index 000000000..ce9a16a89 --- /dev/null +++ b/app/views/tasks/index.html.erb @@ -0,0 +1,17 @@ + +<%= 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 %> + <%=task.completed_at %> + +
  2. + <% end %> +
+<% else %> +

You are in luck, no tasks for today!

+<% end %> diff --git a/app/views/tasks/new.html.erb b/app/views/tasks/new.html.erb new file mode 100644 index 000000000..8274abaf4 --- /dev/null +++ b/app/views/tasks/new.html.erb @@ -0,0 +1,12 @@ +

Add new

+<%= 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_field :description %> + + <%= f.submit "Save Task", class: "task-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..e10c9a20d --- /dev/null +++ b/app/views/tasks/show.html.erb @@ -0,0 +1,4 @@ +

<%= @task.name %>

+

<%= @task.description %>

+

<%= @task.completed_at %>

+<%= 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..e69de29bb diff --git a/config/routes.rb b/config/routes.rb index c06383a17..3b7779c0b 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: 'task#update' + delete '/tasks/:id', to: 'task#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..b10373ba6 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,18 @@ +# 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: 0) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + +end From 30a3719ed06d28495f9a8fba158a5281773ba085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannah=20Teki=C3=A9?= Date: Tue, 12 May 2020 17:45:07 -0700 Subject: [PATCH 2/4] completed wave 3 w/out completing the test --- app/controllers/tasks_controller.rb | 4 ++-- app/views/tasks/edit.html.erb | 16 ++++++++++++++++ app/views/tasks/new.html.erb | 17 ++++++++++------- app/views/tasks/show.html.erb | 1 + app/views/tasks/update.html.erb | 1 + config/routes.rb | 6 +++--- db/schema.rb | 10 +++++++++- test/controllers/tasks_controller_test.rb | 8 ++++---- 8 files changed, 46 insertions(+), 17 deletions(-) diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 48840c159..6def76cd3 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -17,7 +17,7 @@ def show @task = Task.find_by(id: params[:id]) if @task.nil? - redirect_to_to tasks_path + redirect_to tasks_path # head :not_found # you can also put 404 return end @@ -29,7 +29,7 @@ def update head :not_found return elsif @task.update(task_params) - redirect_to tasks_path # go to the index so we can see the task in the list + redirect_to tasks_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 diff --git a/app/views/tasks/edit.html.erb b/app/views/tasks/edit.html.erb index e69de29bb..a9809b53b 100644 --- a/app/views/tasks/edit.html.erb +++ 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/new.html.erb b/app/views/tasks/new.html.erb index 8274abaf4..4b9678da9 100644 --- a/app/views/tasks/new.html.erb +++ b/app/views/tasks/new.html.erb @@ -1,12 +1,15 @@ -

Add new

+

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 :name %> - <%= f.text_field :name %> + + <%= f.label :description %> + <%= f.text_area :description %> + - <%= f.label :description %> - <%= f.text_field :description %> - - <%= f.submit "Save Task", class: "task-button" %> + <%= 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 index e10c9a20d..25ae7c6c8 100644 --- a/app/views/tasks/show.html.erb +++ b/app/views/tasks/show.html.erb @@ -1,4 +1,5 @@

<%= @task.name %>

<%= @task.description %>

<%= @task.completed_at %>

+

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

<%= 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 index e69de29bb..d242c758e 100644 --- a/app/views/tasks/update.html.erb +++ 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 3b7779c0b..f7b2e4c30 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,10 +9,10 @@ # 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: 'task#update' - delete '/tasks/:id', to: 'task#destroy' + 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' + patch '/tasks/:id/mark_complete', to: 'tasks#mark_complete', as: 'task_completed' end diff --git a/db/schema.rb b/db/schema.rb index b10373ba6..2e177038b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +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..f4922d2d1 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 = { From 192e62c4866a3f2535ceca54a4e46e329cd424b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannah=20Teki=C3=A9?= Date: Tue, 12 May 2020 18:15:04 -0700 Subject: [PATCH 3/4] completed the test for wave 3 and it passed --- test/controllers/tasks_controller_test.rb | 29 ++++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/test/controllers/tasks_controller_test.rb b/test/controllers/tasks_controller_test.rb index f4922d2d1..0438f4f0c 100644 --- a/test/controllers/tasks_controller_test.rb +++ b/test/controllers/tasks_controller_test.rb @@ -88,26 +88,43 @@ # 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 + + # Arrange + get edit_task_path(task.id) + # Act + # 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 :not_found + 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. + let (:task_hash){ + {task:{ + name: "update task", + description: "update task description", + }} + } it "can update an existing task" do - # Your code here + end it "will redirect to the root page if given an invalid id" do - # Your code here + patch task_path(-1), params: task_hash + + must_respond_with 404 end end From c21fb5512d3ee27a5a05d882f03f75ef2bb607cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannah=20Teki=C3=A9?= Date: Thu, 14 May 2020 13:35:32 -0700 Subject: [PATCH 4/4] finished all waves and all test passed --- app/assets/stylesheets/tasks.css | 0 app/controllers/tasks_controller.rb | 32 ++++++++--- app/views/layouts/application.html.erb | 9 +++- app/views/tasks/index.html.erb | 18 +++++-- app/views/tasks/new.html.erb | 2 +- app/views/tasks/show.html.erb | 9 +++- config/routes.rb | 2 +- test/controllers/tasks_controller_test.rb | 66 ++++++++++++++++++----- 8 files changed, 108 insertions(+), 30 deletions(-) create mode 100644 app/assets/stylesheets/tasks.css 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 index 6def76cd3..ded0c87b4 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -7,7 +7,6 @@ # {name: "Kyle", description: "High Five Somebody You Don't Know", completed_at: "a min ago"}, # ] - class TasksController < ApplicationController def index @tasks = Task.all @@ -17,7 +16,7 @@ def show @task = Task.find_by(id: params[:id]) if @task.nil? - redirect_to tasks_path + redirect_to root_path # head :not_found # you can also put 404 return end @@ -26,10 +25,10 @@ def show def update @task = Task.find_by(id: params[:id]) if @task.nil? - head :not_found + redirect_to root_path return elsif @task.update(task_params) - redirect_to tasks_path(@task.id) # go to the index so we can see the task in the list + 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 @@ -38,15 +37,36 @@ def update end def edit - @task = Task.find_by(id: params[:id]) + @task = Task.find_by(id: params[:id]) if @task.nil? - head :not_found + 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 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/index.html.erb b/app/views/tasks/index.html.erb index ce9a16a89..f1109c20a 100644 --- a/app/views/tasks/index.html.erb +++ b/app/views/tasks/index.html.erb @@ -1,5 +1,5 @@ - <%= link_to "Add Task", new_task_path %> +

Your tasks

<% if @tasks.length > 0 %>
    @@ -7,11 +7,19 @@
  1. <%= link_to(task.name, task_path(task.id) ) %>
    <%= task.description %> - <%=task.completed_at %> - + +

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

You are in luck, no tasks for today!

-<% end %> +

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 index 4b9678da9..51ba50b20 100644 --- a/app/views/tasks/new.html.erb +++ b/app/views/tasks/new.html.erb @@ -6,7 +6,7 @@ <%= f.text_field :name %> - +
<%= f.label :description %> <%= f.text_area :description %>
diff --git a/app/views/tasks/show.html.erb b/app/views/tasks/show.html.erb index 25ae7c6c8..de27d3824 100644 --- a/app/views/tasks/show.html.erb +++ b/app/views/tasks/show.html.erb @@ -1,5 +1,10 @@

<%= @task.name %>

-

<%= @task.description %>

-

<%= @task.completed_at %>

+

<%= @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/config/routes.rb b/config/routes.rb index f7b2e4c30..ac627b082 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,7 +12,7 @@ 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: 'task_completed' + patch '/tasks/:id/mark_complete', to: 'tasks#mark_complete', as: 'mark_completed' end diff --git a/test/controllers/tasks_controller_test.rb b/test/controllers/tasks_controller_test.rb index 0438f4f0c..ac521cd33 100644 --- a/test/controllers/tasks_controller_test.rb +++ b/test/controllers/tasks_controller_test.rb @@ -83,16 +83,28 @@ 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 - + # Act + Task.create(name: "Task 94", description: "new task test description") + task = Task.first # Arrange get edit_task_path(task.id) - # Act - # Assert + # Assert must_respond_with :success end @@ -102,7 +114,7 @@ get edit_task_path(-1) # Assert - must_respond_with :not_found + must_respond_with :redirect end end @@ -111,31 +123,57 @@ 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. - let (:task_hash){ - {task:{ - name: "update task", - description: "update task description", - }} - } it "can update an existing task" do - + 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 + task_hash = { + task: { + name: "new task name", + description: "new task name description" + }, + } patch task_path(-1), params: task_hash - - must_respond_with 404 + 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