Skip to content
Browse files

luck o the irish

  • Loading branch information...
1 parent 6688b86 commit 6d677effee155919ba75026e01818ac0b3f3e8c7 @drapergeek committed
View
5 app/controllers/application_controller.rb
@@ -2,12 +2,13 @@ class ApplicationController < ActionController::Base
protect_from_forgery
def current_user
- session[:current_user]
+ User.new(session[:current_user])
end
helper_method :current_user
def signed_in?
- current_user.present?
+ current_user.signed_in?
end
helper_method :signed_in?
+
end
View
4 app/controllers/todos_controller.rb
@@ -1,6 +1,6 @@
class TodosController < ApplicationController
def index
- @todos = Todo.all
+ @todos = current_user.todos
end
def new
@@ -8,7 +8,7 @@ def new
end
def create
- @todo = Todo.new(params[:todo])
+ @todo = Todo.new(params[:todo].merge(owner: current_user.email_address))
if @todo.save
redirect_to todos_path
else
View
21 app/models/user.rb
@@ -0,0 +1,21 @@
+class User
+
+ attr_reader :email_address
+
+ def initialize(email)
+ @email_address = email
+ end
+
+ def signed_in?
+ email_address.present?
+ end
+
+ def to_s
+ email_address.to_s
+ end
+
+ def todos
+ Todo.where(owner: email_address)
+ end
+
+end
View
5 db/migrate/20120326202505_add_owner_to_todos.rb
@@ -0,0 +1,5 @@
+class AddOwnerToTodos < ActiveRecord::Migration
+ def change
+ add_column :todos, :owner, :string
+ end
+end
View
3 db/schema.rb
@@ -11,12 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120326183542) do
+ActiveRecord::Schema.define(:version => 20120326202505) do
create_table "todos", :force => true do |t|
t.string "title"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.string "owner"
end
end
View
16 features/step_definitions/todo_steps.rb
@@ -23,3 +23,19 @@
page.should have_css("li", text: /Title/)
end
end
+
+Then /^I should see the following todos:$/ do |table|
+ todos = table.raw.flatten
+ todos.each do |todo|
+ step %{"#{todo}" should be in my list of things to do}
+ end
+end
+
+Then /^I should not see the following todos:$/ do |table|
+ todos = table.raw.flatten
+ todos.each do |todo|
+ within "ul.todos" do
+ page.should_not have_css("li", text: todo)
+ end
+ end
+end
View
3 features/support/env.rb
@@ -16,6 +16,9 @@
end
Cucumber::Rails::Database.javascript_strategy = :truncation
+
+ require 'factory_girl/step_definitions'
+
end
Spork.each_run { }
View
29 features/view_my_todos.feature
@@ -0,0 +1,29 @@
+Feature: View todos I own
+ In order to determine what I need to accomplish
+ As a user
+ I should be able to see all my todos
+
+ Scenario: View my todos
+ Given the following todos exist:
+ | title | owner|
+ | Buy milk | me@example.com |
+ | Buy eggs | me@example.com |
+ | Buy cheese | me@example.com |
+ Given I have signed in as "me@example.com"
+ Then I should see the following todos:
+ | Buy milk |
+ | Buy eggs |
+ | Buy cheese |
+
+ Scenario: View only my todos
+ Given the following todos exist:
+ | title | owner|
+ | Buy milk | me@example.com |
+ | Buy eggs | me@example.com |
+ | Buy bread | not_me@example.com |
+ | Buy cheese | stupid@example.com |
+ Given I have signed in as "me@xample.com"
+ Then I should not see the following todos:
+ | Buy bread |
+ | Buy cheese |
+
View
6 spec/factories.rb
@@ -0,0 +1,6 @@
+FactoryGirl.define do
+ factory :todo do
+ title "Great new title"
+ owner "user@example.com"
+ end
+end
View
52 spec/models/user_spec.rb
@@ -0,0 +1,52 @@
+require 'spec_helper'
+
+describe User, "with an email address" do
+
+ subject { User.new("john@example.com")}
+
+ it "knows its email address" do
+ subject.email_address.should == "john@example.com"
+ end
+
+ it "is logged in to the system" do
+ subject.should be_signed_in
+ end
+
+ it "returns email for to_s" do
+ subject.to_s.should == "john@example.com"
+ end
+
+end
+
+describe User, "without an email address" do
+ subject { User.new(nil)}
+
+ it "has no email address" do
+ subject.email_address.should be_nil
+ end
+
+ it "is logged in to the system" do
+ subject.should_not be_signed_in
+ end
+
+ it "returns an empty string" do
+ subject.to_s.should == ""
+ end
+
+end
+
+describe User, "with todos" do
+
+ subject { User.new("john@example.com")}
+
+ before do
+ FactoryGirl.create(:todo,title: "Mine", owner: "john@example.com")
+ FactoryGirl.create(:todo,title: "Not mine", owner: "not_me@example.com")
+ end
+
+ it "finds my todos" do
+ subject.todos.length.should == 1
+ subject.todos.first.title.should == "Mine"
+ end
+
+end
View
2 spec/spec_helper.rb
@@ -1,5 +1,3 @@
-# spec/spec_helper.rb
-
require 'rubygems'
require 'spork'

0 comments on commit 6d677ef

Please sign in to comment.
Something went wrong with that request. Please try again.