Skip to content

Commit

Permalink
Allow a todo to be marked complete
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Jul 16, 2015
1 parent c51b51c commit c1f38f2
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 5 deletions.
12 changes: 12 additions & 0 deletions app/controllers/completions_controller.rb
@@ -0,0 +1,12 @@
class CompletionsController < ApplicationController
def create
todo.mark_complete# update(completed_at: Time.current)
redirect_to root_path
end

private

def todo
Todo.find(params[:todo_id])
end
end
7 changes: 7 additions & 0 deletions app/models/todo.rb
@@ -1,2 +1,9 @@
class Todo < ActiveRecord::Base
def complete?
completed_at?
end

def mark_complete
update(completed_at: Time.current)
end
end
5 changes: 4 additions & 1 deletion app/views/todos/index.html.erb
Expand Up @@ -2,6 +2,9 @@

<ul class="todos">
<% @todos.each do |todo| %>
<li><%= todo.title %></li>
<li class="<%= todo.complete? ? "completed" : "" %>">
<%= todo.title %>
<%= button_to "Mark complete", todo_completion_path(todo) %>
</li>
<% end %>
</ul>
4 changes: 3 additions & 1 deletion config/routes.rb
@@ -1,6 +1,8 @@
Rails.application.routes.draw do
root to: "todos#index"
resources :todos, only: [:new, :create]
resources :todos, only: [:new, :create] do
resource :completion, only: [:create]
end

resource :session, only: [:new, :create]
end
5 changes: 5 additions & 0 deletions db/migrate/20150716195707_add_completed_at_to_todos.rb
@@ -0,0 +1,5 @@
class AddCompletedAtToTodos < ActiveRecord::Migration
def change
add_column :todos, :completed_at, :timestamp
end
end
7 changes: 4 additions & 3 deletions db/schema.rb
Expand Up @@ -11,13 +11,14 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150716165146) do
ActiveRecord::Schema.define(version: 20150716195707) do

create_table "todos", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "owner_email"
t.datetime "completed_at"
end

end
21 changes: 21 additions & 0 deletions spec/features/user_completes_a_todo_spec.rb
@@ -0,0 +1,21 @@
require "rails_helper"

feature "User completes a todo" do
scenario "successfully" do
sign_in
create_todo_titled title: "Buy eggs"
create_todo_titled title: "Buy milk"

within "li:contains('Buy eggs')" do
click_on "Mark complete"
end

expect(page).to have_css "ul.todos li.completed", text: "Buy eggs"
end

def create_todo_titled(title:)
click_on "Create todo"
fill_in "Title", with: title
click_on "Submit"
end
end
27 changes: 27 additions & 0 deletions spec/models/todo_spec.rb
@@ -0,0 +1,27 @@
require "rails_helper"

RSpec.describe Todo do
describe "#complete?" do
it "returns true when completed_at is set" do
todo = Todo.new(completed_at: Time.current)

expect(todo).to be_complete
end

it "returns false when completed_at is nil" do
todo = Todo.new(completed_at: nil)

expect(todo).not_to be_complete
end
end

describe "#mark_complete" do
it "updates the record with completed_at" do
todo = Todo.create

todo.mark_complete

expect(todo.reload).to be_complete
end
end
end

0 comments on commit c1f38f2

Please sign in to comment.