* I created a new brach 'modeling-users'.
$ git checkout -b modeling-users
* I created a new model (database table) 'user' (singular).
$ rails generate model User name:string email:string
** Note plural convention for controller names, singular for model names.
a Users controller, but a User model
The table name is plural (users) even though the model name is singular (User)
* I ran the database migration command.
$ rails db:migrate
* I wrote some test cases for testing the User model.
test/models/user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: "Example User", email: "user@example.com",
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" * 244 + "@example.com"
assert_not @user.valid?
end
test "email validation should accept valid addresses" do
valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
first.last@foo.jp alice+bob@baz.cn]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end
test "email validation should reject invalid addresses" do
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example.
foo@bar_baz.com foo@bar+baz.com foo@bar_baz..com]
invalid_addresses.each do |invalid_address|
@user.email = invalid_address
assert_not @user.valid?, "#{invalid_address.inspect} should be invalid"
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 be present (nonblank)" do
@user.password = @user.password_confirmation = " " * 6
assert_not @user.valid?
end
test "password should have a minimum length" do
@user.password = @user.password_confirmation = "a" * 5
assert_not @user.valid?
end
end
* The User model file
app/models/user.rb
class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end
* I ran a model test.
$ rails test:models
* I added an index to table Users field Email.
$ rails generate migration add_index_to_users_email
* I filled the email unique requirement to the db migration file.
db/migrate/[timestamp]_add_index_to_users_email.rb
class AddIndexToUsersEmail < ActiveRecord::Migration[5.0]
def change
add_index :users, :email, unique: true
end
end
* Don't forget to run the db migrate command.
$ rails db:migrate
* I deleted the database fixture temporarily.
test/fixtures/users.yml
# empty
* I added a password digest for model User.
$ rails generate migration add_password_digest_to_users password_digest:string
* Then I ran the db migrate command.
$ rails db:migrate
* I addded the bcrypt gem to my Gemfile.
gem 'bcrypt', '3.1.11'
* Then I ran bundle install.
$ bundle install
* At the end of the chapter, I commited using Git and pushed it onto Github.
$ rails test $ git add -A $ git commit -m "Make a basic User model (including secure passwords)"$ git checkout master $ git merge modeling-users $ git push
No comments:
Post a Comment