mirror of
https://github.com/wahyd4/libr-2.git
synced 2026-08-27 22:25:59 +10:00
51 lines
1.0 KiB
Ruby
51 lines
1.0 KiB
Ruby
class User < ActiveRecord::Base
|
|
# Include default devise modules. Others available are:
|
|
# :confirmable, :lockable, :timeoutable and :omniauthable
|
|
devise :database_authenticatable, :registerable,
|
|
:recoverable, :rememberable, :trackable, :validatable
|
|
|
|
before_create :set_name, :generate_auth_key
|
|
|
|
has_many :comments
|
|
has_many :likes
|
|
has_many :favorites
|
|
has_many :share_instances
|
|
has_many :auth_tokens
|
|
|
|
has_many :follow_relations
|
|
|
|
has_many :stars, through: :follow_relations, source: :star, primary_key: :user_id ,foreign_key: :user_id
|
|
|
|
def followers
|
|
user_ids = FollowRelation.where(star_id: self.id).pluck(:user_id)
|
|
User.where(id: user_ids)
|
|
end
|
|
|
|
private
|
|
|
|
def set_name
|
|
email_name = email.split('@')[0]
|
|
self.name = email_name
|
|
end
|
|
|
|
def ensure_authentication_token
|
|
if auth_keys.empty?
|
|
generate_auth_key
|
|
end
|
|
end
|
|
|
|
def generate_auth_key
|
|
AuthToken.generate_token_for self
|
|
end
|
|
|
|
def active_auth_token
|
|
if auth_tokens.empty?
|
|
generate_auth_key
|
|
end
|
|
auth_tokens.first.value
|
|
end
|
|
|
|
end
|
|
|
|
|