Commit 2b492193 by Đường Sỹ Hoàng

Make a basic model (Including secure passwords)

parent 1514d952
source "https://rubygems.org" source "https://rubygems.org"
gem "rails", "5.1.6" gem "rails", "5.1.6"
gem 'bcrypt', "3.1.12"
gem "bootstrap-sass", "3.3.7" gem "bootstrap-sass", "3.3.7"
gem "puma", "3.9.1" gem "puma", "3.9.1"
gem "sass-rails", "5.0.6" gem "sass-rails", "5.0.6"
......
...@@ -42,6 +42,7 @@ GEM ...@@ -42,6 +42,7 @@ GEM
arel (8.0.0) arel (8.0.0)
autoprefixer-rails (9.7.1) autoprefixer-rails (9.7.1)
execjs execjs
bcrypt (3.1.12)
bindex (0.8.1) bindex (0.8.1)
bootstrap-sass (3.3.7) bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1) autoprefixer-rails (>= 5.2.1)
...@@ -202,6 +203,7 @@ PLATFORMS ...@@ -202,6 +203,7 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
bcrypt (= 3.1.12)
bootstrap-sass (= 3.3.7) bootstrap-sass (= 3.3.7)
byebug (= 9.0.6) byebug (= 9.0.6)
coffee-rails (= 4.2.2) coffee-rails (= 4.2.2)
......
class User < ApplicationRecord class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 } validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false } uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end end
class AddIndexToUsersEmail < ActiveRecord::Migration[5.0]
def change
add_index :users, :email, unique: true
end
end
class AddPasswordDigestToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :password_digest, :string
end
end
...@@ -10,13 +10,15 @@ ...@@ -10,13 +10,15 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20191108013337) do ActiveRecord::Schema.define(version: 20191108085254) do
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "name" t.string "name"
t.string "email" t.string "email"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "password_digest"
t.index ["email"], name: "index_users_on_email", unique: true
end end
end end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # empty
one:
name: MyString
email: MyString
two:
name: MyString
email: MyString
...@@ -2,7 +2,8 @@ require "test_helper" ...@@ -2,7 +2,8 @@ require "test_helper"
class UserTest < ActiveSupport::TestCase class UserTest < ActiveSupport::TestCase
def setup def setup
@user = User.new(name: "Example User", email: "user@example.com") @user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end end
test "should be valid" do test "should be valid" do
...@@ -50,4 +51,21 @@ class UserTest < ActiveSupport::TestCase ...@@ -50,4 +51,21 @@ class UserTest < ActiveSupport::TestCase
@user.save @user.save
assert_not duplicate_user.valid? assert_not duplicate_user.valid?
end end
test "email addresses should be saved as lower-case" do
mixed_case_email = "Foo@ExAMPle.CoM"
@user.email = mixed_case_email
@user.save
assert_equal mixed_case_email.downcase, @user.reload.email
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 end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment