Select Git revision
Forked from
hub / hub
Source project has a limited visibility.
user.rb 1.11 KiB
class User < ApplicationRecord
has_many :assignments
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
validates :email, uniqueness: { case_sensitive: false, message: "already in use" }
after_initialize :set_avatar_color
def errors
super.tap { |errors| errors.delete(:password, :blank) if password.nil? }
end
def has_password?
!password.nil?
end
def text_color
r, g, b = avatar_color.delete_prefix('#').chars.each_slice(2).map { |hex| hex.join.to_i(16) }
# Calculate relative luminance (WCAG 2.0 formula)
luminance = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255
# Choose text color based on luminance threshold
luminance > 0.5 ? "#000000" : "#ffffff"
end
def initials
name.split(/\s+/).map(&:first).join('')
end
def set_avatar_color
return unless self.avatar_color.nil?
r = rand(256)
g = rand(256)
b = rand(256)
# r = [r, 128].max
# g = [g, 128].max
# b = [b, 128].max
self.avatar_color = "##{r.to_s(16).rjust(2, '0')}#{g.to_s(16).rjust(2, '0')}#{b.to_s(16).rjust(2, '0')}"
end
end