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
dep committed Dec 28, 2011
1 parent d4d2df3 commit feff3bf
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ def new
@title = "Sign up"
end

def show
@user = User.find(params[:id])
end
end
22 changes: 22 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# == 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: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
</head>
<body>
<%= render "pages/header" %>
<%= yield %>
<%= render "pages/footer" %>
<%= debug(params) if Rails.env.development? %>
</body>
</html>
4 changes: 4 additions & 0 deletions app/views/pages/_footer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<br/>
<%= link_to "Contact", contact_path %>
<%= link_to "Help", help_path %>
<%= link_to "About", about_path %>
3 changes: 3 additions & 0 deletions app/views/pages/_header.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= link_to "Home", root_path %>
<%= link_to "Sign up", signup_path %>
<br/>
1 change: 1 addition & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= @user.name %>, <%= @user.email %>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SampleApp::Application.routes.draw do
get "users/new"
# Adding a Users resource.
resources :users

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

Expand All @@ -8,6 +9,7 @@
match "/help", :to => 'pages#help'

root :to => 'pages#home'

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20111228172028_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email

t.timestamps
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20111228195817_add_email_uniqueness_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddEmailUniquenessIndex < ActiveRecord::Migration
def up
add_index :users, :email, :unique => true
end

def down
remove_index :users, :email
end
end
25 changes: 25 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -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 => 20111228195817) 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
60 changes: 60 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# == 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 require an email address" do
no_email_user = User.new(@attr.merge(:email => ""))
no_email_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 email addresses identical up to case" do
User.create!(@attr.merge(:email => @attr[:email].upcase))
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end
end
14 changes: 14 additions & 0 deletions spec/requests/layout_links_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,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"
response.should have_selector('title', :content => "Sign up")
end
end
22 changes: 22 additions & 0 deletions webrat.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Logfile created on 2011-12-26 16:21:15 -0500 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 / 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 / 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 / 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 feff3bf

Please sign in to comment.