user.rb 896 B

123456789101112131415161718192021222324
  1. class User < ApplicationRecord
  2. validates :name, presence: true
  3. VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  4. validates :email, presence: true, length: { maximum: 255 },
  5. format: { with: VALID_EMAIL_REGEX },
  6. uniqueness: { case_sensitive: false }
  7. before_save { email.downcase! }
  8. validates :login, presence: true, length: { maximum: 50 },
  9. format: { with: /\A[a-zA-Z0-9_]+\Z/ },
  10. uniqueness: { case_sensitive: false }
  11. has_secure_password
  12. validates :password, presence: true, length: { minimum: 6 }
  13. # Returns the hash digest of the given string.
  14. def User.digest(string)
  15. cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
  16. BCrypt::Engine.cost
  17. BCrypt::Password.create(string, cost: cost)
  18. end
  19. end