2012-02-25 16:30:42 +00:00
|
|
|
class User < ActiveRecord::Base
|
|
|
|
|
2012-04-06 12:06:40 +00:00
|
|
|
has_many :user_tokens, :dependent => :destroy
|
|
|
|
has_many :asciicasts, :dependent => :destroy
|
2012-04-09 15:04:58 +00:00
|
|
|
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-03-04 17:14:58 +00:00
|
|
|
|
2012-04-09 12:42:22 +00:00
|
|
|
attr_accessible :nickname, :email, :name
|
|
|
|
|
2013-10-22 15:52:04 +00:00
|
|
|
validates :nickname, :email, presence: true, uniqueness: true
|
|
|
|
|
2013-10-22 17:16:18 +00:00
|
|
|
before_create :generate_auth_token
|
|
|
|
|
2013-10-20 16:51:35 +00:00
|
|
|
def self.for_credentials(credentials)
|
2013-10-19 18:59:39 +00:00
|
|
|
where(provider: credentials.provider, uid: credentials.uid).first
|
2013-10-20 16:51:35 +00:00
|
|
|
end
|
2013-10-19 18:59:39 +00:00
|
|
|
|
2013-10-20 16:51:35 +00:00
|
|
|
def self.for_email(email)
|
|
|
|
where(email: email).first
|
|
|
|
end
|
2013-10-19 18:59:39 +00:00
|
|
|
|
2013-10-22 17:16:18 +00:00
|
|
|
def self.generate_auth_token
|
|
|
|
SecureRandom.urlsafe_base64
|
|
|
|
end
|
|
|
|
|
2013-10-22 15:56:14 +00:00
|
|
|
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)
|
2013-08-08 13:07:48 +00:00
|
|
|
user_tokens.where(:token => token).first_or_create
|
2012-03-04 19:29:19 +00:00
|
|
|
end
|
2013-08-19 15:08:13 +00:00
|
|
|
|
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
|
|
|
|
|
2014-02-01 00:16:28 +00:00
|
|
|
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
|
|
|
|
|
2012-02-25 16:30:42 +00:00
|
|
|
end
|