-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a617314
commit 465350d
Showing
9 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class User < ActiveRecord::Base | ||
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i | ||
before_save { self.email = self.email.downcase } | ||
validates :name, presence: true, length: { maximum: 50 } | ||
validates :email, presence: true, length: { maximum: 255 }, | ||
format: { with: VALID_EMAIL_REGEX }, | ||
uniqueness: { case_sensitive: false } | ||
has_secure_password | ||
validates :password, length: { minimum: 6 } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateUsers < ActiveRecord::Migration | ||
def change | ||
create_table :users do |t| | ||
t.string :name | ||
t.string :email | ||
|
||
t.timestamps null: false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddIndexToUsersEmail < ActiveRecord::Migration | ||
def change | ||
add_index :users, :email, unique: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddPasswordDigestToUsers < ActiveRecord::Migration | ||
def change | ||
add_column :users, :password_digest, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'test_helper' | ||
|
||
class UserTest < ActiveSupport::TestCase | ||
|
||
def setup | ||
@user = User.new(name: "Example User", email: "[email protected]", | ||
password: "foobar", password_confirmation: "foobar") | ||
end | ||
|
||
test "should be valid" do | ||
assert @user.valid? | ||
end | ||
|
||
test "name should be present" do | ||
@user.name = " " | ||
assert_not @user.valid? | ||
end | ||
|
||
test "email should be present" do | ||
@user.email = " " | ||
assert_not @user.valid? | ||
end | ||
|
||
test "name should not be too long" do | ||
@user.name = "a"*51 | ||
assert_not @user.valid? | ||
end | ||
|
||
test "email should not be too long" do | ||
@user.email = "a"*255 + "@example.com" | ||
assert_not @user.valid? | ||
end | ||
|
||
test "email validation should accept valid email adresses" do | ||
valid_addresses = %w[[email protected] [email protected] [email protected] [email protected] [email protected]] | ||
valid_addresses.each do |valid_address| | ||
@user.email = valid_address | ||
assert @user.valid?, "#{valid_address.inspect} should be valid" | ||
end | ||
end | ||
|
||
test "email addresses should be unique" do | ||
duplicate_user = @user.dup | ||
duplicate_user.email = @user.email.upcase | ||
@user.save | ||
assert_not duplicate_user.valid? | ||
end | ||
|
||
test "password should have minimum length" do | ||
@user.password = @user.password_confirmation = "a"*5 | ||
assert_not @user.valid? | ||
end | ||
|
||
end |