remove relationships && move user decorator methods to model

dev_course
p31729568 6 years ago
parent 799ec98df1
commit a9749fd886

@ -115,8 +115,8 @@ class ShixunsController < ApplicationController
def show_right def show_right
owner = @shixun.owner owner = @shixun.owner
#@fans_count = owner.followers.count #@fans_count = owner.fan_count
#@followed_count = owner.followed_users.count #@followed_count = owner.follow_count
@user_own_shixuns = owner.shixuns.published.count @user_own_shixuns = owner.shixuns.published.count
end end

@ -12,50 +12,6 @@ module UserDecorator
logged_user? ? real_name : full_name logged_user? ? real_name : full_name
end end
# 关注数
def follow_count
Watcher.where(user_id: id, watchable_type: %w(Principal User)).count
# User.watched_by(id).count
end
# 粉丝数
def fan_count
Watcher.where(watchable_type: %w(Principal User), watchable_id: id).count
# watchers.count
end
# 是否绑定邮箱
def email_binded?
mail.present?
end
# 学院的url标识
def college_identifier
Department.find_by_id(department_members.pluck(:department_id).first)&.identifier
end
# 是否能申请试用
def can_apply_trial?
return false if certification == 1
apply = ApplyAction.order(created_at: :desc).find_by(user_id: id, container_type: 'TrialAuthorization')
apply.present? && !apply.status.zero?
end
# 是否已经签到
def attendance_signed?
attendance = Attendance.find_by(user_id: id)
attendance.present? && Util.days_between(Time.zone.now, attendance.created_at).zero?
end
# 明日签到金币
def tomorrow_attendance_gold
Attendance.find_by(user_id: id)&.next_gold || 50
end
# ----------- 账号管理 ------------- # ----------- 账号管理 -------------
def authentication_status def authentication_status
if authentication? if authentication?
@ -76,16 +32,4 @@ module UserDecorator
'uncertified' 'uncertified'
end end
end end
def base_info_completed?
user_columns = %i[nickname lastname]
user_extension_columns = %i[gender location location_city identity school_id department]
user_columns.all? { |column| public_send(column).present? } &&
user_extension_columns.all? { |column| user_extension.send(column).present? }
end
def all_certified?
authentication? && professional_certification?
end
end end

@ -28,4 +28,9 @@ module Util
end end
end end
end end
def logger_error(exception)
Rails.logger.error(exception.message)
exception.backtrace.each { |message| Rails.logger.error(message) }
end
end end

@ -1,6 +1,8 @@
# TODO: 已废弃
class Relationship < ApplicationRecord class Relationship < ApplicationRecord
belongs_to :follower, class_name: "User" belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User" belongs_to :followed, class_name: "User"
validates :follower_id, presence: true validates :follower_id, presence: true
validates :followed_id, presence: true validates :followed_id, presence: true
end end

@ -47,14 +47,6 @@ class User < ApplicationRecord
has_many :graduation_works, dependent: :destroy has_many :graduation_works, dependent: :destroy
# 关注
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
# 粉丝
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
has_many :students_for_courses, foreign_key: :student_id, dependent: :destroy has_many :students_for_courses, foreign_key: :student_id, dependent: :destroy
has_one :onclick_time, :dependent => :destroy has_one :onclick_time, :dependent => :destroy
@ -161,19 +153,16 @@ class User < ApplicationRecord
self.user_extension.try(:student_id) self.user_extension.try(:student_id)
end end
# 关注总数 # 关注数
def following?(other_user) def follow_count
relationships.find_by(followed_id: other_user) Watcher.where(user_id: id, watchable_type: %w(Principal User)).count
end # User.watched_by(id).count
# 关注
def follow!(other_user)
relationships.create!(followed_id: other_user)
end end
# 取消关注 # 粉丝数
def unfollow!(other_user) def fan_count
relationships.find_by(followed_id: other_user.id).destroy Watcher.where(watchable_type: %w(Principal User), watchable_id: id).count
# watchers.count
end end
# 判断当前用户是否为老师 # 判断当前用户是否为老师
@ -545,6 +534,51 @@ class User < ApplicationRecord
Educoder::Utils.random_hex(16) Educoder::Utils.random_hex(16)
end end
# 基本资料是否完善
def base_info_completed?
user_columns = %i[nickname lastname]
user_extension_columns = %i[gender location location_city identity school_id department]
user_columns.all? { |column| public_send(column).present? } &&
user_extension_columns.all? { |column| user_extension.send(column).present? }
end
# 全部已认证
def all_certified?
authentication? && professional_certification?
end
# 是否绑定邮箱
def email_binded?
mail.present?
end
# 学院的url标识
def college_identifier
Department.find_by_id(department_members.pluck(:department_id).first)&.identifier
end
# 是否能申请试用
def can_apply_trial?
return false if certification == 1
apply = ApplyAction.order(created_at: :desc).find_by(user_id: id, container_type: 'TrialAuthorization')
apply.present? && !apply.status.zero?
end
# 是否已经签到
def attendance_signed?
attendance = Attendance.find_by(user_id: id)
attendance.present? && Util.days_between(Time.zone.now, attendance.created_at).zero?
end
# 明日签到金币
def tomorrow_attendance_gold
Attendance.find_by(user_id: id)&.next_gold || 60 # 基础50连续签到+10
end
protected protected
def validate_password_length def validate_password_length
# 管理员的初始密码是5位 # 管理员的初始密码是5位

@ -10,7 +10,7 @@ json.array! @members do |member|
json.user do json.user do
json.partial! 'users/user', locals: { user: member.user } json.partial! 'users/user', locals: { user: member.user }
json.user_shixuns_count member.user.shixuns.published.count json.user_shixuns_count member.user.shixuns.published.count
#json.fans_count member.user.followers.count #json.fans_count member.user.fan_count
json.brief_introduction member.user.user_extension.brief_introduction json.brief_introduction member.user.user_extension.brief_introduction
json.identity member.user.identity json.identity member.user.identity
json.school_name member.user.school_name json.school_name member.user.school_name

Loading…
Cancel
Save