From cb499a68ac6c24113bc8ad556a32bc4adca0a21a Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Fri, 25 Sep 2020 10:54:08 -0700 Subject: [PATCH] Add validations and hooks to User model --- app/models/user.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 379658a..1ed856a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,2 +1,36 @@ class User < ApplicationRecord + before_save { self.email = email.downcase } + + ##### ActiveRecord Validations + validates :username, + presence: true, + length: { minimum: 3, maximum: 26 } + + validates :email, + case_sensitive: false, + presence: true, + uniqueness: true, + email: true + + validates :password, + length: { minimum: 8 }, + presence: true, + on: :create + + validates :password, + length: { minimum: 8 }, + on: :update, + if: :password_digest_changed? + + validates :password_confirmation, + presence: true, + on: :create + + validates :password_confirmation, + presence: true, + on: :update, + if: :password_digest_changed? + +##### ActiveModel Security + has_secure_password end