-
-
Save motdotla/5183011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
attr_accessible :name, :email, :password, :password_confirmation, :lvl, :xp, :hp, :ap, :max_xp, :max_hp, :max_ap, :type | |
has_secure_password | |
before_save { email.downcase! } | |
before_save :create_remember_token | |
validates :name, presence: true, length: { maximum: 50 } | |
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i | |
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, | |
uniqueness: { case_sensitive: false } | |
# validates :password, presence: true, length: { minimum: 6 } | |
# validates :password_confirmation, presence: true | |
after_initialize :set_any_nil_values_to_defaults | |
private | |
def set_any_nil_values_to_defaults | |
self.xp = 0 unless xp | |
self.max_xp = 50 | |
self.hp = 4 | |
self.max_hp = 4 | |
self.ap = 4 | |
self.max_ap = 4 | |
self.lvl = 1 | |
end | |
def create_remember_token | |
self.remember_token = SecureRandom.urlsafe_base64 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
User.new
User.new(xp: 100)
User.create(xp: 100)
User.find(id)