Skip to content

Commit

Permalink
User model validations
Browse files Browse the repository at this point in the history
  • Loading branch information
mhartl committed Aug 22, 2010
1 parent ff5ec33 commit 622a3c8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@

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
9 changes: 9 additions & 0 deletions db/migrate/20100822012431_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
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100821203213) do
ActiveRecord::Schema.define(:version => 20100822012431) do

create_table "users", :force => true do |t|
t.string "name"
Expand All @@ -19,4 +19,6 @@
t.datetime "updated_at"
end

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

end
54 changes: 53 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
require 'spec_helper'

describe User do
pending "add some examples to (or delete) #{__FILE__}"

before(:each) do
@attr = { :name => "Example User", :email => "[email protected]" }
end

it "should create a new instance 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[[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
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

0 comments on commit 622a3c8

Please sign in to comment.