Commit 601b88b9 by Tan Phat Nguyen

Make a basic user model

parent 1180548b
source 'https://rubygems.org'
gem 'rails', '4.2.0.beta4'
gem 'bcrypt', '3.1.7'
gem 'bootstrap-sass', '3.2.0.0'
#gem 'bootstrap-sass', github: 'thomas-mcdonald/bootstrap-sass'
gem 'sass-rails', '5.0.0.beta1'
......@@ -24,6 +25,7 @@ group :test do
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
gem 'shoulda'
end
group :production do
......
......@@ -38,6 +38,7 @@ GEM
tzinfo (~> 1.1)
ansi (1.4.3)
arel (6.0.0.beta2)
bcrypt (3.1.7)
binding_of_caller (0.7.3.pre1)
debug_inspector (>= 0.0.1)
bootstrap-sass (3.2.0.0)
......@@ -164,6 +165,12 @@ GEM
sdoc (0.4.0)
json (~> 1.8)
rdoc (~> 4.0, < 5.0)
shoulda (3.5.0)
shoulda-context (~> 1.0, >= 1.0.1)
shoulda-matchers (>= 1.4.1, < 3.0)
shoulda-context (1.2.1)
shoulda-matchers (2.7.0)
activesupport (>= 3.0.0)
slop (3.6.0)
spring (1.1.3)
sprockets (2.12.3)
......@@ -198,6 +205,7 @@ PLATFORMS
ruby
DEPENDENCIES
bcrypt (= 3.1.7)
bootstrap-sass (= 3.2.0.0)
byebug (= 3.4.0)
coffee-rails (= 4.0.1)
......@@ -213,6 +221,7 @@ DEPENDENCIES
rails_12factor (= 0.0.2)
sass-rails (= 5.0.0.beta1)
sdoc (= 0.4.0)
shoulda
spring (= 1.1.3)
sqlite3 (= 1.3.9)
turbolinks (= 2.3.0)
......
class User < ActiveRecord::Base
before_save {self.email = email.downcase}
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: {maximum: 255}, format: {with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
validates :password, length: {minimum: 6}
has_secure_password
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps null: false
end
end
end
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
add_index :users, :email, unique: true
end
end
class AddPasswordDigestToUsers < ActiveRecord::Migration
def change
add_column :users, :password_digest, :string
end
end
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20141104095950) do
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
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
should validate_presence_of(:name)
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" * 256
assert_not @user.valid?
end
test "email validation should accept valid addresses" do
valid_addresses = %w[user@example,com user_at_foo.org user.name@example. foo@bar_baz.com foo@bar+baz.com]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert_not @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 a minimum length" do
@user.password = @user.password_confirmation = "a" * 5
assert_not @user.valid?
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