Tuesday, May 23, 2017

User model in my own style

I am continuing to make my Ruby on Rails website in my own style with borrowing some CSS theme from templatemo. I want to separate the single Name field into First Name and Last Name fields respectively.

* I went back to myapp2. There is a Github repository on:
https://github.com/jimmy2046/myapp2

* I created a branch modeling-users
$ git checkout -b modeling-users

* I generated a new controller Users
$ rails generate controller Users new

* I generated a new model User. I wanted to separated the name field into First Name and Last Name.
$ rails generate model User first_name:string last_name:string email:string

* The database migration file was:
db/migrate/20170516222225_create_users.rb
 class CreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      t.string :first_name
      t.string :last_name
      t.string :email

      t.timestamps
    end
  end
end 

* Then, I ran database migration command.
$ rails db:migrate
== 20170516222225 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0018s
== 20170516222225 CreateUsers: migrated (0.0019s) =============================

* And then, I wrote a test case for the db model.
myapp2/test/models/user_test.rb
require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = User.new(first_name: "Example", last_name: "User", email: "user@example.com")
  end

  test "should be valid" do
    assert @user.valid?
  end   
   
end

* After that, I ran the db model test.
$ rails test:models
Finished in 0.110520s, 9.0481 runs/s, 9.0481 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

* Next, I elaborated more about the User model and the test cases for the model test.

* In order to add the unique constraint to the email field, I created an index for the table Users and field email.
$ rails generate migration add_index_to_users_email
Running via Spring preloader in process 31393
      invoke  active_record
      create    db/migrate/20170524031347_add_index_to_users_email.rb

* I didn't forget to fill in the content of the db migration file.
myapp2/db/migrate/20170524031347_add_index_to_users_email.rb
class AddIndexToUsersEmail < ActiveRecord::Migration[5.0]
  def change
    add_index :users, :email, unique: true     
  end
end

* I saved the file 20170524031347_add_index_to_users_email.rb and ran the db migration shell command.
$ rails db:migrate
== 20170524031347 AddIndexToUsersEmail: migrating =============================
-- add_index(:users, :email, {:unique=>true})
   -> 0.0018s
== 20170524031347 AddIndexToUsersEmail: migrated (0.0019s) ====================

* To solve the violation of unique constraint of the db, I temporarily remarked (commented)  the lines in the user fixtures.
test/fixtures/users.yml
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# one:
#  first_name: MyString
#  last_name: MyString
#  email: MyString

# two:
#  first_name: MyString
#  last_name: MyString
#  email: MyString

* I did the db migration for password digest.
$ rails generate migration add_password_digest_to_users password_digest:string

* Then, i ran db migrate.
$ rails db:migrate

* I added bcrypt in the Gemfile.
gem 'bcrypt',         '3.1.11'

* And then I ran bundle install.
$ bundle install

* This was the final version of the User model file.
myapp2/app/models/user.rb
class User < ApplicationRecord
  before_save { self.email = email.downcase }
  validates :first_name, presence: true, length: { maximum: 50 }
  validates :last_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

* And this was the final version of the test case file for User model test.
myapp2/test/models/user_test.rb
require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = User.new(first_name: "Example", last_name: "User", email:                     "user@example.com",
                    password: "foobar", password_confirmation: "foobar")
  end

  test "should be valid" do
    assert @user.valid?
  end   

  test "first name should be present" do
    @user.first_name = "     "
    assert_not @user.valid?
  end

  test "last name should be present" do
    @user.last_name = "     "
    assert_not @user.valid?
  end   
   
  test "email should be present" do
    @user.email = "     "
    assert_not @user.valid?
  end

  test "first name should not be too long" do
    @user.first_name = "a" * 51
    assert_not @user.valid?
  end

  test "last name should not be too long" do
    @user.last_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]
    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 "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

* I ran a test, added all untracked files, and committed the project: myapp2.
$ rails test
$ git add -A
$ git commit -m "Make a basic User model (including secure passwords)"

* I merged the modeling users branch to master branch. And then, I pushed it onto Github.
$ git checkout master
$ git merge modeling-users
$ git push

* The URL address for current project myapp2 is https://github.com/jimmy2046/myapp2

1 comment:

  1. After reading this blog i very strong in this topics and this blog really helpful to all Ruby on Rails Online Training


    ReplyDelete

How to kill an abandoned process in Linux/Unix

I remembered it, then I forgot, then I remembered it, and then I forgot again. In case of a Linux/Unit process hang, I have to figure out ...