Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c51b51c
commit c1f38f2
Showing
8 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
class Todo < ActiveRecord::Base | ||
def complete? | ||
completed_at? | ||
end | ||
|
||
def mark_complete | ||
update(completed_at: Time.current) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddCompletedAtToTodos < ActiveRecord::Migration | ||
def change | ||
add_column :todos, :completed_at, :timestamp | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |