mirror of
https://github.com/ryankazokas/turbovault-app.git
synced 2026-04-16 23:22:53 +00:00
Moving to github
This commit is contained in:
42
app/models/user.rb
Normal file
42
app/models/user.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
class User < ApplicationRecord
|
||||
has_secure_password
|
||||
|
||||
# Associations
|
||||
has_many :games, dependent: :destroy
|
||||
has_many :collections, dependent: :destroy
|
||||
has_many :items, dependent: :destroy
|
||||
has_many :api_tokens, dependent: :destroy
|
||||
has_many :igdb_match_suggestions, through: :games
|
||||
|
||||
# Validations
|
||||
validates :email, presence: true, uniqueness: { case_sensitive: false }, format: { with: URI::MailTo::EMAIL_REGEXP }
|
||||
validates :username, presence: true, uniqueness: { case_sensitive: false },
|
||||
length: { minimum: 3, maximum: 30 },
|
||||
format: { with: /\A[a-zA-Z0-9_]+\z/, message: "only allows letters, numbers, and underscores" }
|
||||
validates :password, length: { minimum: 8 }, if: -> { password.present? }
|
||||
validates :theme, presence: true, inclusion: { in: %w[light dark midnight retro ocean] }
|
||||
|
||||
# Callbacks
|
||||
before_save :downcase_email
|
||||
|
||||
# Instance methods
|
||||
def generate_password_reset_token
|
||||
self.password_reset_token = SecureRandom.urlsafe_base64
|
||||
self.password_reset_sent_at = Time.current
|
||||
save!
|
||||
end
|
||||
|
||||
def password_reset_expired?
|
||||
password_reset_sent_at.nil? || password_reset_sent_at < 2.hours.ago
|
||||
end
|
||||
|
||||
def theme_class
|
||||
"theme-#{theme}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def downcase_email
|
||||
self.email = email.downcase if email.present?
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user