From a9749fd88615da6778014b2a487e3023448a31f3 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Mon, 24 Jun 2019 16:53:54 +0800 Subject: [PATCH] remove relationships && move user decorator methods to model --- app/controllers/shixuns_controller.rb | 4 +- app/decorators/user_decorator.rb | 56 --------------- app/libs/util.rb | 5 ++ app/models/relationship.rb | 2 + app/models/user.rb | 72 ++++++++++++++----- app/views/shixuns/collaborators.json.jbuilder | 2 +- 6 files changed, 63 insertions(+), 78 deletions(-) diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 8359b14f9..c18e72efa 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -115,8 +115,8 @@ class ShixunsController < ApplicationController def show_right owner = @shixun.owner - #@fans_count = owner.followers.count - #@followed_count = owner.followed_users.count + #@fans_count = owner.fan_count + #@followed_count = owner.follow_count @user_own_shixuns = owner.shixuns.published.count end diff --git a/app/decorators/user_decorator.rb b/app/decorators/user_decorator.rb index 463d9a6e3..e00dead39 100644 --- a/app/decorators/user_decorator.rb +++ b/app/decorators/user_decorator.rb @@ -12,50 +12,6 @@ module UserDecorator logged_user? ? real_name : full_name 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 if authentication? @@ -76,16 +32,4 @@ module UserDecorator 'uncertified' 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 \ No newline at end of file diff --git a/app/libs/util.rb b/app/libs/util.rb index f6c8855b9..3485bebd7 100644 --- a/app/libs/util.rb +++ b/app/libs/util.rb @@ -28,4 +28,9 @@ module Util end end end + + def logger_error(exception) + Rails.logger.error(exception.message) + exception.backtrace.each { |message| Rails.logger.error(message) } + end end \ No newline at end of file diff --git a/app/models/relationship.rb b/app/models/relationship.rb index d5c754745..d71987867 100644 --- a/app/models/relationship.rb +++ b/app/models/relationship.rb @@ -1,6 +1,8 @@ +# TODO: 已废弃 class Relationship < ApplicationRecord belongs_to :follower, class_name: "User" belongs_to :followed, class_name: "User" + validates :follower_id, presence: true validates :followed_id, presence: true end diff --git a/app/models/user.rb b/app/models/user.rb index 8e25efe70..1b3349467 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -47,14 +47,6 @@ class User < ApplicationRecord 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_one :onclick_time, :dependent => :destroy @@ -161,19 +153,16 @@ class User < ApplicationRecord self.user_extension.try(:student_id) end - # 关注总数 - def following?(other_user) - relationships.find_by(followed_id: other_user) - end - - # 关注 - def follow!(other_user) - relationships.create!(followed_id: other_user) + # 关注数 + def follow_count + Watcher.where(user_id: id, watchable_type: %w(Principal User)).count + # User.watched_by(id).count end - # 取消关注 - def unfollow!(other_user) - relationships.find_by(followed_id: other_user.id).destroy + # 粉丝数 + def fan_count + Watcher.where(watchable_type: %w(Principal User), watchable_id: id).count + # watchers.count end # 判断当前用户是否为老师 @@ -545,6 +534,51 @@ class User < ApplicationRecord Educoder::Utils.random_hex(16) 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 def validate_password_length # 管理员的初始密码是5位 diff --git a/app/views/shixuns/collaborators.json.jbuilder b/app/views/shixuns/collaborators.json.jbuilder index bd880c93e..0101f1e59 100644 --- a/app/views/shixuns/collaborators.json.jbuilder +++ b/app/views/shixuns/collaborators.json.jbuilder @@ -10,7 +10,7 @@ json.array! @members do |member| json.user do json.partial! 'users/user', locals: { user: member.user } 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.identity member.user.identity json.school_name member.user.school_name