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
Ashwin Das committed Sep 25, 2011
1 parent 03749dd commit 0b9d5fd
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 103 deletions.
7 changes: 4 additions & 3 deletions .idea/sample_app.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

241 changes: 150 additions & 91 deletions .idea/workspace.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ GEM
activemodel (= 3.0.9)
activesupport (= 3.0.9)
activesupport (3.0.9)
annotate (2.4.0)
arel (2.0.10)
builder (2.1.2)
diff-lcs (1.1.3)
Expand Down Expand Up @@ -91,6 +92,7 @@ PLATFORMS
ruby

DEPENDENCIES
annotate (= 2.4.0)
rails (= 3.0.9)
rspec-rails (= 2.6.1)
sqlite3 (= 1.3.3)
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class UsersController < ApplicationController

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

def new
@title = "Sign up"
end
Expand Down
25 changes: 25 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# == 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

1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<%= yield %>
</section>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
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 %>
16 changes: 16 additions & 0 deletions bin/annotate
Original file line number Diff line number Diff line change
@@ -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')
9 changes: 1 addition & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SampleApp::Application.routes.draw do

get "users/new"
resources :users

match '/signup', :to => 'users#new'
match '/contact', :to => 'pages#contact'
Expand All @@ -9,13 +9,6 @@

root :to => 'pages#home'

get "pages/home"

get "pages/contact"

get "pages/about"

get "pages/help"

# The priority is based upon order of creation:
# first created -> highest priority.
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20110925035149_create_users.rb
Original file line number Diff line number Diff line change
@@ -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/20110925120839_add_email_uniqueness_index.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 0) do
ActiveRecord::Schema.define(:version => 20110925035149) do

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

end
70 changes: 70 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# == 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 => "[email protected]" }
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[[email protected] [email protected] [email protected]]
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(@attr)
user_with_duplicate_email.should_not be_valid
end

it "should reject email addresses identical up to case" do
upcased_email = @attr[:email].upcase
User.create!(@attr.merge(:email => upcased_email))
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 @@ -27,4 +27,18 @@
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 # fill in
click_link "Contact"
response.should # fill in
click_link "Home"
response.should # fill in
click_link "Sign up now!"
response.should # fill in
end

end
7 changes: 7 additions & 0 deletions webrat.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Logfile created on 2011-09-24 23:13:40 -0400 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"=>"/"}

0 comments on commit 0b9d5fd

Please sign in to comment.