Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final wave (4) #106

Open
wants to merge 4 commits into
base: rmt/master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions task-list/app/assets/javascripts/people.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions task-list/app/assets/stylesheets/people.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the People controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
5 changes: 5 additions & 0 deletions task-list/app/assets/stylesheets/tasks.scss
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,8 @@ opacity: 0.6;
.button_to {
display: inline;
}

.button-wrapper {
color: red;
font-size: 1.2em;
}
23 changes: 23 additions & 0 deletions task-list/app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class PeopleController < ApplicationController
def index
@people = Person.all
end

def show
id = params[:id]
@person = Person.find(id)
@tasks = @person.tasks
end

def task_by_person
id = params[:id]
@person_tasks = Person.find(id).tasks
@person = Person.find(id)

end

# def task_by_person
Copy link

Choose a reason for hiding this comment

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

It would be best not to commit commented-out code like this.

If we've written some code when working on a given change and decide not to use that code, it should be fine to delete it before committing.
If we want to keep hold of it in case it'll be helpful later, we can commit it to a different branch.
If we have already committed the code and later decide not to use it, we should delete it entirely (if we need it back later Git will still have the old version).

#
# end

end
5 changes: 3 additions & 2 deletions task-list/app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class TasksController < ApplicationController
# the instance variables make it possible for you to view information on the page
def index
@tasks = Task.all
# raise
@peeps = Person.all
end

def show
Expand All @@ -27,7 +29,6 @@ def destroy
def complete
task = Task.find(params[:id])
task.completed_date = Time.now
task.completed = true
task.save
redirect_to "/tasks"
end
Expand All @@ -52,7 +53,7 @@ def update

# changing this, along with my update method, made editing my task from the show page possible.
def task_params
params.require(:task).permit(:name, :description)
params.require(:task).permit(:name, :description, :person_id)
end


Expand Down
2 changes: 2 additions & 0 deletions task-list/app/helpers/people_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PeopleHelper
end
11 changes: 11 additions & 0 deletions task-list/app/models/person.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Person < ActiveRecord::Base
has_many :tasks

def self.incomplete(pers_id)
Copy link

Choose a reason for hiding this comment

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

I like the idea to use a class method here to extend the vocabulary associated with the Person model!

As we'll seen soon enough, you can actually move this method into the Task class and set tasks = Task.all instead of person.tasks (no longer any need for a pers_id parameter).

This will allow you to view all incomplete tasks with Task.incomplete -- however, it will also allow you to view the incomplete tasks for a single person with person.tasks.incomplete.

person = Person.find(pers_id)
tasks = person.tasks
incomplete = tasks.where(completed_date: nil)
return incomplete.length
end

end
1 change: 1 addition & 0 deletions task-list/app/models/task.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Task < ActiveRecord::Base
belongs_to :person
end
9 changes: 9 additions & 0 deletions task-list/app/views/people/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1> These people do tasks: </h1>

<ul>
<% @people.each do |person| %>
<li>
<%= link_to "#{person.name}", "/people/#{person.id}" %>, aka <%= person.alt_name %>
</li>
<% end %>
</ul>
16 changes: 16 additions & 0 deletions task-list/app/views/people/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1> All of <%= @person.name %>'s Uncompleted Tasks:</h1>

<ul>
<% @tasks.each do |task| %>
<% if task.completed_date == nil %>
Copy link

Choose a reason for hiding this comment

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

We have a check like this throughout the codebase, and we could "Metzatize" it (very, very slightly) by moving it into the Task model class:

class Task < ActiveRecord::Base
  def completed?
    return completed_date == nil
  end
end

That way our view logic doesn't have to "know" that a task is considered incomplete specifically because that field is nil. In this case its a very subtle difference, I think it's good practice for separating the concerns between Model and View in the MVC pattern. We'll start having more complex model logic soon and the distinction will probably become more clear over time.

<li>
<p><%= @person.alt_name %>'s chore: <%= task.name %></p>
</li>
<% end %>
<% end %>
<p>You have <%= Person.incomplete(@person.id) %> chores left to do</p>
</ul>

<%= link_to "Back to my page", "/people/#{@person.id}/tasks" %>

<%= link_to "Back to my peeps", "/people" %>
17 changes: 17 additions & 0 deletions task-list/app/views/people/task_by_person.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1> All of <%= @person.name %>'s Tasks:</h1>

<ul>
<% @person_tasks.each do |task| %>
<% if task.completed_date != nil %>
<li>
<p class="strike"><%= task.name %></p>
</li>
<% else %>
<li>
<p><%= task.name %></p>
</li>
<% end %>
<% end %>
</ul>

<%= link_to "Take me back", "/tasks" %>
3 changes: 3 additions & 0 deletions task-list/app/views/tasks/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<%= f.label :description %>
<%= f.text_field :description %>

<%= f.label :writer_id, "Whose Task?" %>
<%= f.collection_select(:writer_id, Person.all, :id, :alt_name, prompt: "Select the Tasker") %>

<%= f.submit %>
</div>
<% end %>
3 changes: 3 additions & 0 deletions task-list/app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
<a href="/tasks/<%= task.id %>" class="strike"><%= task.name %></a>
<p class="description"><%= task.description %></p>
</div>
<div class="button-wrapper">
<%= button_to "ain't gonna do it", "/tasks/#{task.id}", method: :delete, data: { :confirm => "Are you sure you want to delete this?" } %>
<%= button_to "already done did it", "/tasks/#{task.id}", method: :patch %>
<%= button_to "edit", "/tasks/#{task.id}/edit", method: :get %>
</div>
</li>
<% else %>
<li>
<div class="task-wrapper">
<p>This task belongs to <%= @peeps.name %></p>
<a href="/tasks/<%= task.id %>"><%= task.name %></a>
<p class="description"><%= task.description %></p>
</div>
Expand Down
3 changes: 3 additions & 0 deletions task-list/app/views/tasks/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<%= f.label :description %>
<%= f.text_field :description %>

<%= f.label :person_id, "Whose Task?" %>
<%= f.collection_select(:person_id, Person.all, :id, :alt_name, prompt: "Select the Tasker") %>

<%= f.submit %>
</div>
<% end %>
5 changes: 3 additions & 2 deletions task-list/app/views/tasks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
<p><%= @task.name%></p>
<h3>Description </h3>
<p><%= @task.description%></p>
<p>This task belongs to <% %></p>

<h3>Completed?</h3>
<% if @task.completed == false || @task.completed == nil %>
<% if @task.completed_date == nil %>
<p>nope</p>
<% else %>
<p>yes</p>
<% end %>

<!-- @task.completed == true is implied on the following line. -->
<% if @task.completed %>
<% if @task.completed_date != nil %>
<h3>On: </h3>
<% if @task.completed_date != nil %>
<p><%= @task.completed_date%></p>
Expand Down
7 changes: 4 additions & 3 deletions task-list/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
Rails.application.routes.draw do

# these are the routes for the edit link
get 'tasks/:id/edit' => 'tasks#edit'
patch 'tasks/:id/edit' => 'tasks#update'

# this is the route for the completed button.
patch 'tasks/:id' => 'tasks#complete'

Expand All @@ -14,10 +12,13 @@
get 'tasks/:id' => 'tasks#show'

post 'tasks/' => 'tasks#create'

# this is the route for the delete button.
delete 'tasks/:id' => 'tasks#destroy'

# When i go to this url, give me this page.
get '/people/' => 'people#index'
get '/people/:id' => 'people#show'
get '/people/:id/tasks' => 'people#task_by_person'



Expand Down
10 changes: 10 additions & 0 deletions task-list/db/migrate/20151116200506_create_people.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreatePeople < ActiveRecord::Migration
def change
create_table :people do |t|
t.string :name
t.string :alt_name

t.timestamps null: false
end
end
end
9 changes: 9 additions & 0 deletions task-list/db/migrate/20151116201012_modify_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ModifyTasks < ActiveRecord::Migration
def change
change_table :tasks do |t|
add_column :tasks, :writer_id, :integer
end

add_index :tasks, :writer_id
end
end
5 changes: 5 additions & 0 deletions task-list/db/migrate/20151116214614_delete_column.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DeleteColumn < ActiveRecord::Migration
def change
remove_column :tasks, :completed
end
end
5 changes: 5 additions & 0 deletions task-list/db/migrate/20151116233332_fix_column_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class FixColumnName < ActiveRecord::Migration
def change
rename_column :tasks, :writer_id, :person_id
end
end
13 changes: 11 additions & 2 deletions task-list/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151111001244) do
ActiveRecord::Schema.define(version: 20151116233332) do

create_table "people", force: :cascade do |t|
t.string "name"
t.string "alt_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.string "description"
t.boolean "completed"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "completed_date"
t.integer "person_id"
end

add_index "tasks", ["person_id"], name: "index_tasks_on_person_id"

end
23 changes: 17 additions & 6 deletions task-list/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@ def random_time
end

tasks = [
{ name: "Eat Lunch", description: "grilled cheese. Butter bread this time", completed: false, completed_date: random_time },
{ name: "Hike", description: "Discovery Park at noon", completed: true, completed_date: random_time},
{ name: "sleep", description: "at night", completed: true, completed_date: random_time },
{ name: "re-read notes", description: "rails, rails, rails", completed: false, completed_date: random_time },
{ name: "get invited to dinner at ricky's or emily's", description: "food", completed: false, completed_date: random_time },
{ name: "thanksgiving!", description: "Evan, Ohio, food", completed: false, completed_date: random_time }
{ name: "Eat Lunch", description: "grilled cheese. Butter bread this time", completed_date: random_time, person_id: 2 },
{ name: "Hike", description: "Discovery Park at noon", completed_date: random_time, person_id: 2},
{ name: "sleep", description: "at night", completed_date: random_time, person_id: 2 },
{ name: "re-read notes", description: "rails, rails, rails", completed_date: random_time, person_id: 2 },
{ name: "get invited to dinner at ricky's or emily's", description: "food", completed_date: random_time, person_id: 2 },
{ name: "thanksgiving!", description: "Evan, Ohio, food", completed_date: random_time, person_id: 2 }
]

tasks.each do |task|
Task.create task
end

people = [
{ name: "Jim", alt_name: "dad" },
{ name: "Sally", alt_name: "mom" },
{ name: "Elizabeth", alt_name: "sister" },
{ name: "Evan", alt_name: "hubs" }
]

people.each do |seed|
Person.create(seed)
end

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
Expand Down