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

Kld/master - finished wave 4 #101

Open
wants to merge 13 commits into
base: kld/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 tasks/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 tasks/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/
6 changes: 5 additions & 1 deletion tasks/app/assets/stylesheets/tasks.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ a {
color: #FBFBFB;
}

a:hover {
color: #800000;
}

p {
color: #FBFBFB;
font-family: 'Rock Salt', cursive;
Expand Down Expand Up @@ -70,7 +74,7 @@ fieldset {
color: #FBFBFB;
}

#add_new {
.add_new {
margin-top: 20px;
font-size: 1.5em;
text-align: center;
Expand Down
51 changes: 51 additions & 0 deletions tasks/app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class PeopleController < ApplicationController
before_filter :last_page

def index
@people = Person.all
end

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

def showtasks
id = params[:id]
@person = Person.find(id)
@tasks = Person.find(id).tasks
Copy link

Choose a reason for hiding this comment

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

For this we could reuse the @person variable as it should be the same as calling Person.find(id) again.

end

def new
@person = Person.new
end

def create
Person.create(person_params[:person])
redirect_to "/people"
end

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

def update
id = params[:id]
@person = Person.find(id)
@person.update(
name: person_params[:person][:name]
)
redirect_to "/people"
end

private

def person_params
params.permit(person:[:id, :name])
Copy link

Choose a reason for hiding this comment

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

I'm not sure if this should permit :id, as we generally don't want the user of our app to be able to update an existing Person object with a new ID, or to create a new Person with a specific ID.

end

def last_page
session[:last_page] = request.env['HTTP_REFERER']
end
end
10 changes: 8 additions & 2 deletions tasks/app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class TasksController < ApplicationController
before_filter :last_page

def index
@tasks = Task.all
Expand Down Expand Up @@ -28,7 +29,8 @@ def update
@task = Task.find(id)
@task.update(
name: task_params[:task][:name],
description: task_params[:task][:description]
description: task_params[:task][:description],
person_id: task_params[:task][:person_id]
)
redirect_to "/"
end
Expand All @@ -49,7 +51,11 @@ def destroy
private

def task_params
params.permit(task:[:name, :description, :completed])
params.permit(task:[:name, :description, :completed, :person_id])
end

def last_page
session[:last_page] = request.env['HTTP_REFERER']
end

end
2 changes: 2 additions & 0 deletions tasks/app/helpers/people_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PeopleHelper
end
3 changes: 3 additions & 0 deletions tasks/app/models/person.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Person < ActiveRecord::Base
has_many :tasks
end
1 change: 1 addition & 0 deletions tasks/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
5 changes: 5 additions & 0 deletions tasks/app/views/people/_backbutton.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p class="center_task">
<% if session[:last_page] %>
Copy link

Choose a reason for hiding this comment

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

This code might be simpler if you used a small amount of javascript:

<%= button_tag "Go Back", onclick: "History.back();" %>

At that point you would not need to store any information in the session hash.

<%= link_to "Go Back", session[:last_page] %>
<% end %>
</p>
8 changes: 8 additions & 0 deletions tasks/app/views/people/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%= form_for @person, :html => {:class => "form-inline"} do |f| %>
<fieldset>
<legend><%= legend_text %></legend>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit value: "#{button_text}", class: "new_button"%>
</fieldset>
<% end %>
2 changes: 2 additions & 0 deletions tasks/app/views/people/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= render partial: 'form', locals: {method_type: :patch, button_text: "Update Person", legend_text: "Edit Person"} %>
<%= render partial: 'backbutton' %>
22 changes: 22 additions & 0 deletions tasks/app/views/people/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<h1>People</h1>
<div class="center">
<% @people.each do |person| %>
<span>
<a href="/people/<%= person.id %>" >
<%= person.name %> - <%= person.tasks.where({:completed => nil}).length %>
Copy link

Choose a reason for hiding this comment

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

I would suggest moving a small bit of this code into the Person model class:

def tasks_uncompleted
  return self.tasks.where({:completed => nil})
end

Then you can use person.tasks_uncompleted.count on this line and the one below it. (count is preferred to length for ActiveRecord model classes.)

<% unless person.tasks.where({:completed => nil}).length == 1 %>
uncompleted tasks
<% else %>
uncompleted task
<% end %>
</a>
</span>
<br />
<% end %>
</div>
<p class="add_new">
<a href="/people/new">Add a new person</a>
</p>
<p class="add_new">
<a href="/tasks/">See all tasks</a>
</p>
2 changes: 2 additions & 0 deletions tasks/app/views/people/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= render partial: 'form', locals: {button_text: "Add New Person", legend_text: "Add New Person"} %>
<%= render partial: 'backbutton' %>
10 changes: 10 additions & 0 deletions tasks/app/views/people/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1><%= @person.name %></h1>
<div class="center">
<span>
<a href="/people/<%= @person.id %>/tasks">See task list </a>
<br />
<br />
<a href="/people/<%= @person.id %>/edit">Edit person </a>
</span>
</div>
<%= render partial: 'backbutton' %>
28 changes: 28 additions & 0 deletions tasks/app/views/people/showtasks.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1><%= @person.name %></h1>
<div class="center">
<span>
To Do:
<br />
<% @tasks.each do |task| %>
Copy link

Choose a reason for hiding this comment

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

The code within this loop through @tasks is very similar to the code in the loop later on in this template that also goes through @tasks. How might we be able to effectively DRY this out?

<% if task.completed.nil? %>
<a href="/tasks/<%= task.id %>"><%= task.name %></a>
<br />
<% end %>
<% end %>
</span>
<br />
<br />
<br />
<span>
Completed:
<br />
<% @tasks.each do |task| %>
<% if !task.completed.nil? %>
<a href="/tasks/<%= task.id %>" class="strikethrough"><%= task.name %></a>
<br />
<% end %>
<% end %>
</span>
<br />
</div>
<%= render partial: 'backbutton' %>
5 changes: 5 additions & 0 deletions tasks/app/views/tasks/_backbutton.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p class="center_task">
<% if session[:last_page] %>
<%= link_to "Go Back", session[:last_page] %>
<% end %>
</p>
2 changes: 2 additions & 0 deletions tasks/app/views/tasks/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<%= f.text_field :description %>
<%= f.label :completed %>
<%= f.date_field :completed %>
<%= f.label :person_id, "Task owner:" %>
<%= f.collection_select(:person_id, Person.all, :id, :name, prompt: "Select the owner:") %>
<%= f.submit value: "#{button_text}", class: "new_button"%>
</fieldset>
<% end %>
1 change: 1 addition & 0 deletions tasks/app/views/tasks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<%= render partial: 'form', locals: {method_type: :patch, button_text: "Update Task", legend_text: "Edit Task"} %>
<%= render partial: 'backbutton' %>
7 changes: 5 additions & 2 deletions tasks/app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
<% else %>
<%= button_to " ", "/tasks/#{task.id}/complete", method: :patch, class: 'complete-button' %>
<% end %>
<a href="/tasks/<%= task.id %>"<% if !task.completed.nil? %>class="strikethrough"<% end %>><%= task.name %></a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="/tasks/<%= task.id %>"<% if !task.completed.nil? %>class="strikethrough"<% end %>><%= task.name %> - <%= task.person.name %></a>&nbsp;&nbsp;&nbsp;&nbsp;
<%= button_to "X", "/tasks/#{task.id}", method: :delete, data: { confirm: "Are you sure you want to delete this task?" } , class: 'x-button' %>
</span>
<br />
<% end %>
</div>
<p id="add_new">
<p class="add_new">
<a href="/tasks/new">Add a new task</a>
</p>
<p class="add_new">
<a href="/people/">See tasks per person</a>
</p>
1 change: 1 addition & 0 deletions tasks/app/views/tasks/new.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<%= render partial: 'form', locals: {button_text: "Create Task", legend_text: "Add Task"} %>
<%= render partial: 'backbutton' %>
4 changes: 4 additions & 0 deletions tasks/app/views/tasks/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
Not completed
<% end %>
</p>
<p class = "center_task">
Assigned to: <%= @task.person.name %>
</p>
<p class="center_task">
<a href="/tasks/<%= @task.id %>/edit" >Edit task</a>
</p>
<%= render partial: 'backbutton' %>
10 changes: 10 additions & 0 deletions tasks/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
patch 'tasks/:id' => 'tasks#update', :as => :task
patch 'tasks/:id/complete' => 'tasks#complete'

get 'people/' => 'people#index'
get 'people/new' => 'people#new'
post 'people/' => 'people#create'
get 'people/:id/edit' => 'people#edit'
get 'people/:id/tasks' => 'people#showtasks'
get 'people/:id' => 'people#show'
patch 'people/:id' => 'people#update', :as => :person



# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

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

t.timestamps null: false
end
end
end
11 changes: 11 additions & 0 deletions tasks/db/migrate/20151116194355_add_person_index_to_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddPersonIndexToTasks < ActiveRecord::Migration
def change
change_table :tasks do |t|
add_column :tasks, :person_id, :integer
end

add_index :tasks, :person_id
end


end
11 changes: 10 additions & 1 deletion tasks/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151110221105) do
ActiveRecord::Schema.define(version: 20151116194355) do

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

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

end
32 changes: 22 additions & 10 deletions tasks/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,29 @@ def random_time
Time.at(rand * Time.now.to_i)
end

seed_people = [
{name: "Katherine"},
{name: "Jared"},
{name: "Maxwell"},
{name: "Mom"}
]

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


tasks = [
{ name: "The First Task", description: "", completed: random_time },
{ name: "Go to Brunch", description: "" },
{ name: "Go to Lunch", description: "", completed: random_time },
{ name: "Go to Second Lunch", description: "" },
{ name: "Play Video Games", description: "", completed: random_time },
{ name: "High Five Somebody You Don't Know", description: "", completed: random_time },
{ name: "Plant Flowers", description: "", completed: random_time },
{ name: "Call Mom", description: "" },
{ name: "She worries, you know.", description: "" },
{ name: "Nap.", description: "", completed: random_time }
{ name: "The First Task", description: "", completed: random_time, person_id: 1 },
{ name: "Go to Brunch", description: "", person_id: 1 },
{ name: "Go to Lunch", description: "", completed: random_time, person_id: 1 },
{ name: "Go to Second Lunch", description: "", person_id: 2 },
{ name: "Play Video Games", description: "", completed: random_time, person_id: 3 },
{ name: "High Five Somebody You Don't Know", description: "", completed: random_time, person_id: 1 },
{ name: "Plant Flowers", description: "", completed: random_time, person_id: 4 },
{ name: "Call Mom", description: "", person_id: 3 },
{ name: "She worries, you know.", description: "", person_id: 2 },
{ name: "Nap.", description: "", completed: random_time, person_id: 4 }
]

tasks.each do |task|
Expand Down