Skip to content

Commit 818e603

Browse files
committed
commit while troubleshooting migration
1 parent ce69bd8 commit 818e603

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

app/models/user.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class User < ActiveRecord::Base
2+
attr_accessible :name, :email
3+
4+
before_save { |user| user.email = user.email.downcase }
5+
6+
validates :name, presence: true, length: { maximum: 50 }
7+
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
8+
#validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
9+
# uniqueness: { case_sensitive: false }
10+
end
11+

db/development.sqlite3-journal

8.52 KB
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class AddIndexToUsersEmail < ActiveRecord::Migration
2+
def self.up
3+
add_index(table = :users, :column => :email, {:unique => true})
4+
end
5+
6+
def self.down
7+
remove_index :users, :column => :email, {:unique => true}
8+
end

db/schema.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# encoding: UTF-8
2+
# This file is auto-generated from the current state of the database. Instead
3+
# of editing this file, please use the migrations feature of Active Record to
4+
# incrementally modify your database, and then regenerate this schema definition.
5+
#
6+
# Note that this schema.rb definition is the authoritative source for your
7+
# database schema. If you need to create the application database on another
8+
# system, you should be using db:schema:load, not running all the migrations
9+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
10+
# you'll amass, the slower it'll run and the greater likelihood for issues).
11+
#
12+
# It's strongly recommended to check this file into your version control system.
13+
14+
ActiveRecord::Schema.define(:version => 20120614144603) do
15+
16+
create_table "users", :force => true do |t|
17+
t.string "name"
18+
t.string "email"
19+
t.datetime "created_at", :null => false
20+
t.datetime "updated_at", :null => false
21+
end
22+
23+
end

spec/models/user_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'spec_helper'
2+
3+
describe User do
4+
5+
before do
6+
@user = User.new(name: "Example User", email: "user@example.com")
7+
end
8+
9+
subject { @user }
10+
11+
it { should respond_to(:name) }
12+
it { should respond_to(:email) }
13+
14+
it { should be_valid }
15+
16+
describe "when name is not present" do
17+
before { @user.name = " " }
18+
it {should_not be_valid }
19+
end
20+
21+
describe " when email is not present" do
22+
before { @user.email = " " }
23+
it { should_not be_valid }
24+
end
25+
26+
27+
describe "when name is too long" do
28+
before { @user.name = "a" * 51 }
29+
it { should_not be_valid }
30+
end
31+
32+
33+
describe "when email format is invalid" do
34+
it "should be invalid" do
35+
addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
36+
addresses.each do |invalid_address|
37+
@user.email = invalid_address
38+
@user.should_not be_valid
39+
end
40+
end
41+
end
42+
43+
describe "when email format is valid" do
44+
it "should be valid" do
45+
addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
46+
addresses.each do |valid_address|
47+
@user.email = valid_address
48+
@user.should be_valid
49+
end
50+
end
51+
end
52+
53+
describe "when email address is already taken" do
54+
before do
55+
user_with_same_email = @user.dup
56+
user_with_same_email.email = @user.email.upcase
57+
user_with_same_email.save
58+
end
59+
60+
it { should_not be_valid }
61+
end
62+
end

0 commit comments

Comments
 (0)