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
Empty file.
96 changes: 96 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

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

Definitely not a big deal but your indentation is a little off here.

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

Choose a reason for hiding this comment

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

indentation here as well.

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
4 changes: 4 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
<title>TaskList</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>

<body>
<%= yield %>

<div class="container-fluid">
<h1>Task List</h1>
<%= yield %>
</div>

</body>
</html>
1 change: 1 addition & 0 deletions app/views/tasks/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= link_to "Tasks", tasks_path %>
Empty file.
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, class: 'create-task' do |f| %>
<p>Please provide the following information to edit your task in our database:</p>

<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_area :description %>
</div>

<%= f.submit "Edit task", class: "edit-button" %>
<% end %>
25 changes: 25 additions & 0 deletions app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= link_to "Add Task", new_task_path %>

<h4>Your tasks</h4>
<% if @tasks.length > 0 %>
<ol>
<% @tasks.each do |task| %>
<li>
<header><strong><%= link_to(task.name, task_path(task.id) ) %></strong></header>
<%= task.description %>

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

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

</li>
<% end %>
</ol>
<% else %>
<h3>We are out of task, can you believe it?!</h3>
<% end %>
15 changes: 15 additions & 0 deletions app/views/tasks/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h2>Add new Task</h2>
<%= form_with model: @task, class: 'create-task' do |f| %>
<p>Please provide the following information to save your task to a database:</p>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>

<div>
<%= f.label :description %>
<%= f.text_area :description %>
</div>

<%= f.submit "Save Task", class: "save-button" %>
<% end %>
10 changes: 10 additions & 0 deletions app/views/tasks/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h2><%= @task.name %></h2>
<p><%= @task.description %></p>
<p><%= link_to "Edit Task", edit_task_path(@task) %></p>
<% if @task.completed_at.nil?%>
<p>Not completed</p>
<% else %>
<p> Completed on <%= @task.completed_at %></p>
<% end %>

<%= link_to "All Tasks", tasks_path %>
1 change: 1 addition & 0 deletions app/views/tasks/update.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= link_to "Task", task_path(@task) %>
18 changes: 18 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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





11 changes: 11 additions & 0 deletions db/migrate/20200512050603_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.string :description
t.string :completed_at

t.timestamps
end
end
end
26 changes: 26 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -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
Loading