Permalink
Browse files

add user, todo, and project models

  • Loading branch information...
1 parent e5a945a commit 70781b8f2731cdf6f033f10d08922ca58339e59a @mattr- committed Feb 22, 2012
View
@@ -0,0 +1,7 @@
+class Project < ActiveRecord::Base
+ belongs_to :user
+
+ has_many :todos
+
+ validates_presence_of :user
+end
View
@@ -0,0 +1,4 @@
+class Todo < ActiveRecord::Base
+ belongs_to :user
+ validates_presence_of :user
+end
View
@@ -0,0 +1,15 @@
+class User < ActiveRecord::Base
+ has_many :todos
+ has_many :projects
+
+ # Include default devise modules. Others available are:
+ # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
+ devise :database_authenticatable, :registerable,
+ :recoverable, :rememberable, :trackable, :validatable
+
+ # Setup accessible (or protected) attributes for your model
+ attr_accessible :email, :password, :password_confirmation, :remember_me
+
+
+
+end
View
@@ -1,4 +1,10 @@
Tracks3::Application.routes.draw do
+
+ root :to => "users#index"
+ devise_for :users
+
+ resources :users
+
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -0,0 +1,25 @@
+class DeviseCreateUsers < ActiveRecord::Migration
+ def change
+ create_table(:users) do |t|
+ t.database_authenticatable :null => false
+ t.recoverable
+ t.rememberable
+ t.trackable
+
+ # t.encryptable
+ # t.confirmable
+ # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
+ # t.token_authenticatable
+
+
+ t.timestamps
+ end
+
+ add_index :users, :email, :unique => true
+ add_index :users, :reset_password_token, :unique => true
+ # add_index :users, :confirmation_token, :unique => true
+ # add_index :users, :unlock_token, :unique => true
+ # add_index :users, :authentication_token, :unique => true
+ end
+
+end
@@ -0,0 +1,9 @@
+class CreateTodos < ActiveRecord::Migration
+ def change
+ create_table :todos do |t|
+ t.string :description
+ t.references :user
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,8 @@
+class CreateProjects < ActiveRecord::Migration
+ def change
+ create_table :projects do |t|
+ t.references :user
+ t.timestamps
+ end
+ end
+end
View
@@ -0,0 +1,33 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20120127051502) do
+
+ create_table "users", :force => true do |t|
+ t.string "email", :default => "", :null => false
+ t.string "encrypted_password", :limit => 128, :default => "", :null => false
+ t.string "reset_password_token"
+ t.datetime "reset_password_sent_at"
+ t.datetime "remember_created_at"
+ t.integer "sign_in_count", :default => 0
+ t.datetime "current_sign_in_at"
+ t.datetime "last_sign_in_at"
+ t.string "current_sign_in_ip"
+ t.string "last_sign_in_ip"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "users", ["email"], :name => "index_users_on_email", :unique => true
+ add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
+
+end
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+describe Project do
+ context "without a User object" do
+ it "cannot be created" do
+ expect { Project.create!}.to raise_error
+ end
+ end
+
+ context "with a user" do
+ it "can be created" do
+ user = User.create!(:email => "foo@example.com", :password => "foooooo")
+ project = Project.create!(:user => user)
+ project.reload.user.should == user
+ end
+ end
+
+ it "can have todos" do
+ user = User.create!(:email => "foo@example.com", :password => "foooooo")
+ project = Project.create!(:user => user)
+ todo = Todo.create!(:user => user)
+ project.todos << todo
+ project.reload.todos.should == [todo]
+ end
+end
View
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe Todo do
+ context "creation" do
+ it "cannot happen without user" do
+ expect { Todo.create! }.to raise_error
+ end
+
+ it "succeeds with a user" do
+ user = User.create!(:email => "foo@example.com", :password => "foooooo")
+ t = Todo.create!(:user => user)
+ t.reload.user.should == user
+ end
+ end
+end
View
@@ -0,0 +1,32 @@
+# This file is copied to spec/ when you run 'rails generate rspec:install'
+ENV["RAILS_ENV"] ||= 'test'
+require File.expand_path("../../config/environment", __FILE__)
+require 'rspec/rails'
+require 'rspec/autorun'
+
+# Requires supporting ruby files with custom matchers and macros, etc,
+# in spec/support/ and its subdirectories.
+Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
+
+RSpec.configure do |config|
+ # ## Mock Framework
+ #
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
+ #
+ # config.mock_with :mocha
+ # config.mock_with :flexmock
+ # config.mock_with :rr
+
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
+
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
+ # examples within a transaction, remove the following line or assign false
+ # instead of true.
+ config.use_transactional_fixtures = true
+
+ # If true, the base class of anonymous controllers will be inferred
+ # automatically. This will be the default behavior in future versions of
+ # rspec-rails.
+ config.infer_base_class_for_anonymous_controllers = false
+end

0 comments on commit 70781b8

Please sign in to comment.