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
11 changes: 11 additions & 0 deletions app/assets/stylesheets/tasks.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Place all the styles related to the Tasks controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
.task-list {
color: blueviolet;
align-content: center;
}

.completed {
text-decoration: line-through;
}
80 changes: 80 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class TasksController < ApplicationController

def index
@tasks = Task.all
end

def show
@task = Task.find_by(id: params[:id])
if @task.nil?
head :not_found
Copy link

@jmaddox19 jmaddox19 May 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin marked the project as not handling non-existent tasks because the thing most people did here was a redirect but this is fine for now as well.
For MediaRanker, we will want to do a redirect in addition to flash messages to keep the user on a functioning page while also giving clear messaging to the user that what they were looking for is not there.

return
end
end

def new
@task = Task.new
end

def create
@task = Task.new(name: params[:task][:name], description: params[:task][:description])
if @task.save
redirect_to task_path(@task)
return
else
render :new
return
end
end

def edit
@task = Task.find_by(id: params[:id])
if @task.nil?
head :not_found
return
end
end

def update
@task = Task.find_by(id: params[:id])
if @task.nil?
head :not_found
return
elsif @task.update(
name: params[:task][:name],
description: params[:task][:description],
completed_at: params[:task][:completed_at]
)
redirect_to task_path(@task)
return
else
render :edit
return
end
end

def destroy
@task = Task.find_by(id: params[:id])
if @task.nil?
head :not_found
return
else
@task.destroy
redirect_to tasks_path
return
end
end

def mark_complete

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't figured out why but this is not succeeding for me in updating the task to complete.

@task = Task.find_by(id: params[:id])
if @task.nil?
head :not_found
return
else
@task.update(completed: true, completed_at: Time.now.to_s)
redirect_to tasks_path
return
end
end

end
2 changes: 2 additions & 0 deletions app/helpers/tasks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TasksHelper
end
3 changes: 3 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Task < ApplicationRecord
validates :name, presence: true
end
16 changes: 16 additions & 0 deletions app/views/tasks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>edit task</h2>
<%= form_with model: @task do |f| %>

<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :description %>
<%= f.text_field :description %>

<%if @task.completed%>
<%= f.label :completed_at%>
<%= f.datetime_field :completed_at%>
<%end%>

<%= f.submit "update task" %>
Comment on lines +2 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good place to use a form partial.

<%end%>
18 changes: 18 additions & 0 deletions app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h2>current tasks</h2>
<main>
<ul class="task-list">
<% @tasks.each do |task| %>
<li>
<%if task.completed %>
<%= link_to task.name, task_path(task), class: "completed"%>
<%else%>
<%= link_to task.name, task_path(task) %>
<%= link_to "(mark complete)", mark_complete_path(task), method: :patch%>
<%end%>
</li>
<%end%>
</ul>
<%= link_to "add new task", new_task_path, class: "button" %>
</main>

<%#= button_to "Edit #{book.title}", edit_book_path(book.id) %>
10 changes: 10 additions & 0 deletions app/views/tasks/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h2>add new task</h2>
<%= form_with model: @task do |f|%>
<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :decription %>
<%= f.text_field :description %>

<%= f.submit "save task" %>
<%end%>
13 changes: 13 additions & 0 deletions app/views/tasks/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2><%=@task.name%></h2>
<main>
<p><%= @task.description%></p>
<%if @task.completed%>
<p>completed at: <%= @task.completed_at %> </p>
<%end%>
<%= link_to "edit", edit_task_path(@task)%>
<span>/</span>
<%= link_to "delete", delete_task_path(@task), method: :delete%>
<br>
<br>
<%= link_to "back to all tasks", tasks_path %>
</main>
11 changes: 11 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
# collection of tasks

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, it'll be important to have a root path to make your application more user-friendly.
Right now, I need to know to navigate to the /tasks path to be able to use the site.

get '/tasks', to: 'tasks#index', as: 'tasks'
get '/tasks/new', to: 'tasks#new', as: 'new_task'
post '/tasks', to: 'tasks#create'

# individual tasks
get '/tasks/:id', to: 'tasks#show', as: 'task'
get '/tasks/:id/edit', to: 'tasks#edit', as: 'edit_task'
patch '/tasks/:id', to: 'tasks#update'
patch '/tasks.:id', to: 'tasks#mark_complete', as: 'mark_complete'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This totally still works because the code uses the alias but I think you intended for this to be a / instead of a .

delete '/tasks/:id', to: 'tasks#destroy', as: 'delete_task'
end
11 changes: 11 additions & 0 deletions db/migrate/20200516032507_create_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateTasks < ActiveRecord::Migration[6.0]
def change
create_table :tasks do |t|
t.string :name
t.text :description
t.string :completed_at

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20200516232451_add_created_boolean_to_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCreatedBooleanToTasks < ActiveRecord::Migration[6.0]
def change
add_column :tasks, :completed, :boolean, default: false
end
end
27 changes: 27 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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_16_232451) 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.text "description"
t.string "completed_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.boolean "completed", default: false
end

end
5 changes: 1 addition & 4 deletions test/controllers/tasks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
end

it "can get the root path" do
skip
# Act
get root_path

Expand All @@ -28,7 +29,6 @@
# Unskip these tests for Wave 2
describe "show" do
it "can get a valid task" do
skip
# Act
get task_path(task.id)

Expand All @@ -37,7 +37,6 @@
end

it "will redirect for an invalid task" do
skip
# Act
get task_path(-1)

Expand All @@ -48,7 +47,6 @@

describe "new" do
it "can get the new task page" do
skip

# Act
get new_task_path
Expand All @@ -60,7 +58,6 @@

describe "create" do
it "can create a new task" do
skip

# Arrange
task_hash = {
Expand Down
7 changes: 7 additions & 0 deletions test/models/task_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

describe Task do
# it "does a thing" do
# value(1+1).must_equal 2
# end
end