Permalink
Browse files

User model validations

  • Loading branch information...
1 parent 44abcda commit 3d71893f8165200e7d0f20e2115da641c9a69277 @iscott committed Aug 28, 2011
@@ -1,4 +1,9 @@
class UsersController < ApplicationController
+
+ def show
+ @user = User.find(params[:id])
+ end
+
def new
@title = "Sign up"
end
@@ -11,6 +11,6 @@ def title
end
def logo
- image_tag ("logo.png", :alt => "Sample App", :class => "round")
+ image_tag("logo.png", :alt => "Sample App", :class => "round")
end
end
View
@@ -1,7 +1,3 @@
-class User < ActiveRecord::Base
- attr_accessible :name, :email
-end
-
# == Schema Information
#
# Table name: users
@@ -13,3 +9,18 @@ class User < ActiveRecord::Base
# updated_at :datetime
#
+class User < ActiveRecord::Base
+
+ attr_accessor :password
+
+ 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
+
@@ -11,6 +11,7 @@
<%= yield %>
</section>
<%= render 'layouts/footer' %>
+ <%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
@@ -0,0 +1,3 @@
+<h1>Users</h1>
+Name, Email</br>
+<%= @user.name%>, <%= @user.email %>
View
@@ -1,6 +1,6 @@
SampleApp::Application.routes.draw do
- get "users/new"
+ resources :users
root :to => "pages#home"
@@ -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
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110728092341) do
+ActiveRecord::Schema.define(:version => 20110729224641) do
create_table "users", :force => true do |t|
t.string "name"
@@ -19,4 +19,6 @@
t.datetime "updated_at"
end
+ add_index "users", ["email"], :name => "index_users_on_email", :unique => true
+
end
View
@@ -1,7 +1,77 @@
require 'spec_helper'
describe User do
- pending "add some examples to (or delete) #{__FILE__}"
+ before(:each) do
+ @attr = { :name => "Example User", :email => "user@example.com" }
+ end
+
+ it "should create a new instace given a valid attribute" 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@good.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@good,com", "user_at_foo.org", "first.last@foo."]
+ addresses.each do |address|
+ valid_email_user = User.new(@attr.merge(:email => address))
+ valid_email_user.should_not be_valid
+ end
+ end
+
+ it "should reject duplicate email addresses" do
+ 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
+
+ describe "passwords" do
+ before (:each) do
+ @user = User.new(@attr)
+ end
+
+ it "should have a password attribute" do
+ @user.should respond_to(:password)
+ end
+
+ it "should have a password confirmation" do
+ @user.should respond_to(:password_confirmation)
+ end
+ end
+
+ describe "password validations" do
+ it "should require a password"
+ User.new(@attr.merge(:password => ""))
+ end
end
# == Schema Information
@@ -13,5 +83,4 @@
# email :string(255)
# created_at :datetime
# updated_at :datetime
-#
-
+#

0 comments on commit 3d71893

Please sign in to comment.