Soc/app/models/user.rb

58 lines
1.1 KiB
Ruby
Raw Normal View History

2023-08-08 19:25:35 +00:00
# frozen_string_literal: true
2023-08-19 14:38:38 +00:00
require "securerandom"
2023-08-08 19:25:35 +00:00
class User < ApplicationRecord
has_secure_password
has_many :microposts
2023-10-12 15:24:33 +00:00
has_many :blog_posts
2023-08-08 19:25:35 +00:00
validates :username,
:first_name,
:last_names,
:email,
presence: true
validates :email,
:username,
uniqueness: { case_sensitive: false }
validates :email,
format: { with: /\A.*@.*\..*\z/ } # Only very basic regex
validates :password,
confirmation: true,
length: { in: 12..300 },
presence: true,
if: :validate_password?
validates :password_confirmation,
presence: true,
if: :validate_password?
2023-08-18 16:45:38 +00:00
2023-08-19 14:38:38 +00:00
after_create :require_confirmation
2023-08-18 16:45:38 +00:00
def to_param
username
end
def full_name
"#{first_name} #{last_names}"
end
2023-08-19 14:38:38 +00:00
def requires_confirmation?
email_confirmation_string.present?
end
private
2023-08-19 14:38:38 +00:00
def require_confirmation
update(email_confirmation_string: SecureRandom.uuid)
UserMailer.with(user: self).confirm_email.deliver_later
end
def validate_password?
new_record? || password_digest_changed?
end
2023-08-08 19:25:35 +00:00
end