优化“全部设为已读” 代码

This commit is contained in:
huang 2015-09-21 15:17:10 +08:00
parent 6e9db8b922
commit d9620e416e
1 changed files with 15 additions and 11 deletions

View File

@ -106,16 +106,7 @@ class UsersController < ApplicationController
update_onclick_time
# 全部设为已读
if params[:viewed] == "all"
course_querys = @user.course_messages
forge_querys = @user.forge_messages
user_querys = @user.user_feedback_messages
forum_querys = @user.memo_messages
if User.current.id == @user.id
course_querys.update_all(:viewed => true)
forge_querys.update_all(:viewed => true)
user_querys.update_all(:viewed => true)
forum_querys.update_all(:viewed => true)
end
update_message_viewed(@user)
end
# @new_message_count = forge_querys.count + forum_querys.count + course_querys.count + user_querys.count
case params[:type]
@ -189,7 +180,6 @@ class UsersController < ApplicationController
def update_onclick_time
# 记录当前点击按钮的时间
# 考虑到用户未退出刷新消息页面
message_time = OnclickTime.where("user_id =?", User.current).first
if message_time.nil?
message_new_time = OnclickTime.new
@ -205,6 +195,20 @@ class UsersController < ApplicationController
@user_system_messages = SystemMessage.where("created_at >?", contrast_time).order("created_at desc")
end
# 消息设置为已读
def update_message_viewed(user)
course_querys = CourseMessage.where("user_id =? and viewed =?", user, 0)
forge_querys = ForgeMessage.where("user_id =? and viewed =?", user, 0)
user_querys = UserFeedbackMessage.where("user_id =? and viewed =?", user, 0)
forum_querys = MemoMessage.where("user_id =? and viewed =?", user, 0)
if User.current.id == @user.id
course_querys.update_all(:viewed => true)
forge_querys.update_all(:viewed => true)
user_querys.update_all(:viewed => true)
forum_querys.update_all(:viewed => true)
end
end
# 系统消息
def user_system_messages
@sytem_messages = SystemMessage.order("created_at desc").all