Skip to content

Instantly share code, notes, and snippets.

@tpendragon
Forked from ewilliam/gist:5171037
Created March 15, 2013 16:14
Show Gist options
  • Save tpendragon/5171054 to your computer and use it in GitHub Desktop.
Save tpendragon/5171054 to your computer and use it in GitHub Desktop.
class Carnivore < Player
before_save :set_lvl
def initialize(attributes = {})
super
self.xp = 0
self.max_xp = 50
self.hp = 5
self.max_hp = 5
self.ap = 3
self.max_ap = 3
end
def set_lvl
self.lvl = 4
end
end
class Herbivore < Player
end
class Omnivore < Player
end
class Player < User
end
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
# For Single Table Inheritance
def self.inherited(child)
child.instance_eval do
def model_name
User.model_name
end
end
super
end
def self.select_options
descendants.map{ |c| c.to_s }.sort
end
private
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