Skip to content

Commit

Permalink
Finished first cut of the User model
Browse files Browse the repository at this point in the history
  • Loading branch information
Jplez committed Mar 11, 2012
1 parent ded3cf8 commit 8e76296
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -5,6 +5,7 @@ gem 'sqlite3', '1.3.3'

group :development do
gem 'rspec-rails', '2.6.1'
gem 'annotate', '2.4.0'
end

group :test do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -29,6 +29,7 @@ GEM
activemodel (= 3.0.11)
activesupport (= 3.0.11)
activesupport (3.0.11)
annotate (2.4.0)
arel (2.0.10)
autotest (4.4.6)
ZenTest (>= 4.4.1)
Expand Down Expand Up @@ -104,6 +105,7 @@ PLATFORMS
ruby

DEPENDENCIES
annotate (= 2.4.0)
autotest (= 4.4.6)
autotest-fsevent (= 0.2.4)
autotest-growl (= 0.2.16)
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/users_controller.rb
@@ -1,5 +1,9 @@
class UsersController < ApplicationController

def show
@user = User.find(params[:id])
end

def new
@title = "Sign up"
end
Expand Down
23 changes: 23 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,23 @@
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime
# updated_at :datetime
#

class User < ActiveRecord::Base
attr_accessible :name, :email

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

validates :name, :presence => true,
:length => { :maximum => 50}

validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
end
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.erb
Expand Up @@ -11,7 +11,8 @@
<section class="round">
<%= yield %>
</section>
<%= render 'layouts/footer' %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions app/views/users/show.html.erb
@@ -0,0 +1 @@
<%= @user.name %>, <%= @user.email %>
16 changes: 16 additions & 0 deletions bin/annotate
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'annotate' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('annotate', 'annotate')
2 changes: 1 addition & 1 deletion config/routes.rb
@@ -1,5 +1,5 @@
SampleApp::Application.routes.draw do
get "users/new"
resources :users

match '/signup', :to => 'users#new'

Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20120308090808_create_users.rb
@@ -0,0 +1,14 @@
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :email

t.timestamps
end
end

def self.down
drop_table :users
end
end
9 changes: 9 additions & 0 deletions db/migrate/20120310091333_add_email_uniqueness_index.rb
@@ -0,0 +1,9 @@
class AddEmailUniquenessIndex < ActiveRecord::Migration
def self.up
add_index :users, :email, :unique => true
end

def self.down
remove_index :users, :email
end
end
25 changes: 25 additions & 0 deletions db/schema.rb
@@ -0,0 +1,25 @@
# encoding: UTF-8
# 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 => 20120310091333) do

create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "users", ["email"], :name => "index_users_on_email", :unique => true

end
15 changes: 15 additions & 0 deletions spec/helpers/users_helper_spec.rb
@@ -0,0 +1,15 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the UsersHelper. For example:
#
# describe UsersHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe UsersHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
57 changes: 57 additions & 0 deletions spec/models/user_spec.rb
@@ -0,0 +1,57 @@
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime
# updated_at :datetime
#

require 'spec_helper'

describe User do

before(:each) do
@attr = { :name => "Example User", :email => "user@example.com"}
end

it "should create a new instance given valid attributes" do
User.create!(@attr)
end

it "should require a name" do
no_name_user = User.new(@attr.merge(:name => ""))
no_name_user.should_not be_valid
end

it "should reject names that are too long" do
long_name = "a" * 51
long_name_user = User.new(@attr.merge(:name => long_name))
long_name_user.should_not be_valid
end

it "should accept valid email addresses" do
addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
addresses.each do |address|
valid_email_user = User.new(@attr.merge(:email => address))
valid_email_user.should be_valid
end
end

it "should reject invalid email addresses" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
addresses.each do |address|
invalid_email_user = User.new(@attr.merge(:email => address))
invalid_email_user.should_not be_valid
end
end

it "should reject duplicate email addresses" do
# Put a user with given email address into the database.
User.create!(@attr)
user_with_duplicate_email = User.new(@atter)
user_with_duplicate_email.should_not be_valid
end
end
14 changes: 14 additions & 0 deletions spec/requests/layout_links_spec.rb
Expand Up @@ -26,4 +26,18 @@
get '/signup'
response.should have_selector('title', :content => "Sign up")
end

it "should have the right links on the layout" do
visit root_path
click_link "About"
response.should have_selector('title', :content => "About")
click_link "Help"
response.should have_selector('title', :content => "Help")
click_link "Contact"
response.should have_selector('title', :content => "Contact")
click_link "Home"
response.should have_selector('title', :content => "Home")
click_link "Sign up now!"
response.should have_selector('title', :content => "Sign up")
end
end
5 changes: 5 additions & 0 deletions spec/views/users/new.html.erb_spec.rb
@@ -0,0 +1,5 @@
require 'spec_helper'

describe "users/new.html.erb" do
pending "add some examples to (or delete) #{__FILE__}"
end
49 changes: 49 additions & 0 deletions webrat.log
@@ -0,0 +1,49 @@
# Logfile created on 2012-03-08 13:19:08 +0900 by logger.rb/25413
REQUESTING PAGE: GET / with {} and HTTP headers {}
REQUESTING PAGE: GET /about with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET / with {} and HTTP headers {}
REQUESTING PAGE: GET /about with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET / with {} and HTTP headers {}
REQUESTING PAGE: GET /about with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET / with {} and HTTP headers {}
REQUESTING PAGE: GET /about with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET / with {} and HTTP headers {}
REQUESTING PAGE: GET /about with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET / with {} and HTTP headers {}
REQUESTING PAGE: GET /about with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET / with {} and HTTP headers {}
REQUESTING PAGE: GET /about with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET / with {} and HTTP headers {}
REQUESTING PAGE: GET /about with {} and HTTP headers {"HTTP_REFERER"=>"/"}
REQUESTING PAGE: GET /help with {} and HTTP headers {"HTTP_REFERER"=>"/about"}
REQUESTING PAGE: GET /contact with {} and HTTP headers {"HTTP_REFERER"=>"/help"}
REQUESTING PAGE: GET / with {} and HTTP headers {"HTTP_REFERER"=>"/contact"}
REQUESTING PAGE: GET /signup with {} and HTTP headers {"HTTP_REFERER"=>"/"}

0 comments on commit 8e76296

Please sign in to comment.