asciinema.org/app/models/user.rb

73 lines
1.5 KiB
Ruby
Raw Normal View History

class User < ActiveRecord::Base
2012-04-06 12:06:40 +00:00
has_many :user_tokens, :dependent => :destroy
has_many :asciicasts, :dependent => :destroy
has_many :comments, :dependent => :destroy
2012-04-06 12:06:40 +00:00
has_many :likes, :dependent => :destroy
2012-03-04 22:07:45 +00:00
has_many :user_tokens, :dependent => :destroy
has_many :asciicasts, :dependent => :destroy
2012-04-06 20:58:44 +00:00
has_many :comments, :dependent => :destroy
2012-04-09 12:42:22 +00:00
attr_accessible :nickname, :email, :name
validates :nickname, :email, presence: true, uniqueness: true
2013-10-22 17:16:18 +00:00
before_create :generate_auth_token
def self.for_credentials(credentials)
where(provider: credentials.provider, uid: credentials.uid).first
end
def self.for_email(email)
where(email: email).first
end
2013-10-22 17:16:18 +00:00
def self.generate_auth_token
SecureRandom.urlsafe_base64
end
def nickname=(value)
value ? super(value.strip) : super
end
def email=(value)
value ? super(value.strip) : super
end
2012-03-04 22:07:45 +00:00
def to_param
nickname
end
2012-03-04 19:29:19 +00:00
def add_user_token(token)
user_tokens.where(:token => token).first_or_create
2012-03-04 19:29:19 +00:00
end
2013-11-18 18:06:56 +00:00
def asciicast_count
asciicasts.count
end
2014-01-18 10:44:13 +00:00
def asciicasts_excluding(asciicast, limit)
asciicasts.where('id <> ?', asciicast.id).order('RANDOM()').limit(limit)
end
def editable_by?(user)
user && user.id == id
end
def paged_asciicasts(page, per_page)
asciicasts.
includes(:user).
order("created_at DESC").
paginate(page, per_page)
end
2013-10-22 17:16:18 +00:00
private
def generate_auth_token
begin
self[:auth_token] = self.class.generate_auth_token
end while User.exists?(auth_token: self[:auth_token])
end
end