Skip to content
Browse files

Finished first cut of the User model

  • Loading branch information...
1 parent ed3d282 commit ac309b16e57d2ba16637da230d9dce813e12b0e5 @almakdad committed Apr 8, 2011
View
1 Gemfile
@@ -9,6 +9,7 @@ gem 'mysql2'
group :development do
gem 'rspec-rails', '2.5.0'
+ gem 'annotate-models', '1.0.4'
end
group :test do
View
2 Gemfile.lock
@@ -28,6 +28,7 @@ GEM
activemodel (= 3.0.6.rc2)
activesupport (= 3.0.6.rc2)
activesupport (3.0.6.rc2)
+ annotate-models (1.0.4)
arel (2.0.9)
builder (2.1.2)
diff-lcs (1.1.2)
@@ -89,6 +90,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ annotate-models (= 1.0.4)
mysql2
rails (= 3.0.6.rc2)
rspec (= 2.5.0)
View
6 app/controllers/users_controller.rb
@@ -1,7 +1,11 @@
class UsersController < ApplicationController
+ def show
+ @user = User.find(params[:id])
+ end
+
def signup
@title= "Sign up"
- end
+ end
end
View
23 app/models/user.rb
@@ -0,0 +1,23 @@
+# == Schema Information
+# Schema version: 20110407201836
+#
+# Table name: users
+#
+# id :integer(4) 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
View
3 app/views/layouts/application.html.erb
@@ -11,7 +11,8 @@
<section class="round">
<%= yield %>
</section>
- <%= render 'layouts/footer' %>
+ <%= render 'layouts/footer' %>
+ <%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
View
1 app/views/users/show.html.erb
@@ -0,0 +1 @@
+<%= @user.name %>, <%= @user.email %>
View
2 config/routes.rb
@@ -1,5 +1,7 @@
SampleApp::Application.routes.draw do
+ resources :users
+
match '/', :to => 'pages#home'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
View
14 db/migrate/20110407201836_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
View
9 db/migrate/20110407222636_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
View
22 db/schema.rb
@@ -0,0 +1,22 @@
+# 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 => 20110407201836) do
+
+ create_table "users", :force => true do |t|
+ t.string "name"
+ t.string "email"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
View
54 spec/models/user_spec.rb
@@ -0,0 +1,54 @@
+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(@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
View
24 webrat.log
@@ -5,3 +5,27 @@ 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 ac309b1

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