From ecd0056f0a8219e982b495a2978a4179b313e7eb Mon Sep 17 00:00:00 2001 From: huang Date: Mon, 7 Sep 2015 11:13:15 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=AE=8C=E6=88=90=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E8=AE=B0=E5=90=8E=E5=9B=BE=E6=A0=87=E4=B8=8D?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E7=BA=A2=E7=82=B9=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E3=80=82=202=E3=80=81=E9=A6=96=E6=AC=A1=E7=99=BB=E9=99=86?= =?UTF-8?q?=E4=BB=A5=E2=80=9C=E6=9C=80=E5=90=8E=E7=99=BB=E5=BD=95=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E2=80=9D=E4=B8=BA=E5=87=86=EF=BC=8C=E5=90=8E=E9=9D=A2?= =?UTF-8?q?=E4=BB=A5=E7=82=B9=E5=87=BB=E6=97=B6=E9=97=B4=E4=B8=BA=E5=87=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 2 +- app/models/onclick_time.rb | 2 ++ app/models/user.rb | 21 ++++++++++++++++----- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7744f20a4..4bbb9848a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -112,7 +112,7 @@ class UsersController < ApplicationController message_new_time.onclick_time = Time.now message_new_time.save else - message_new_time = OnclickTime.where("user_id =?", User.current) + message_new_time = OnclickTime.where("user_id =?", User.current).first message_new_time.update_attributes(:onclick_time => Time.now) end # 当前用户查看消息,则设置消息为已读 diff --git a/app/models/onclick_time.rb b/app/models/onclick_time.rb index 16b17ea9a..c62a9274c 100644 --- a/app/models/onclick_time.rb +++ b/app/models/onclick_time.rb @@ -1,3 +1,5 @@ class OnclickTime < ActiveRecord::Base attr_accessible :onclick_time, :user_id + + belongs_to :user end diff --git a/app/models/user.rb b/app/models/user.rb index b08b29981..e7f4a8c50 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -132,6 +132,7 @@ class User < Principal has_many :course_messages has_many :memo_messages has_many :user_feedback_messages + has_one :onclick_time # 虚拟转换 has_many :new_jours, :as => :jour, :class_name => 'JournalsForMessage', :conditions => "status=1" @@ -209,7 +210,7 @@ class User < Principal before_save :update_hashed_password before_destroy :remove_references_before_destroy # added by fq - after_create :act_as_activity + after_create :act_as_activity, :add_onclick_time # end scope :in_group, lambda {|group| @@ -257,10 +258,16 @@ class User < Principal # 新消息统计 def count_new_message - course_count = CourseMessage.where("user_id =? and viewed =?", User.current.id, 0).count - forge_count = ForgeMessage.where("user_id =? and viewed =?", User.current.id, 0).count - user_feedback_count = UserFeedbackMessage.where("user_id =? and viewed =?", User.current.id, 0).count - user_memo_count = MemoMessage.where("user_id =? and viewed =?", User.current.id, 0).count + if OnclickTime.where("user_id =?", User.current).first.nil? + message_new_time = OnclickTime.new + message_new_time.user_id = User.current.id + message_new_time.onclick_time = User.current.last_login_on.nil? ? Time.now : User.current.last_login_on + message_new_time.save + end + course_count = CourseMessage.where("user_id =? and viewed =? and created_at >?", User.current.id, 0, User.current.onclick_time.onclick_time).count + forge_count = ForgeMessage.where("user_id =? and viewed =? and created_at >?", User.current.id, 0, User.current.onclick_time.onclick_time).count + user_feedback_count = UserFeedbackMessage.where("user_id =? and viewed =? and created_at >?", User.current.id, 0, User.current.onclick_time.onclick_time).count + user_memo_count = MemoMessage.where("user_id =? and viewed =? and created_at >?", User.current.id, 0, User.current.onclick_time.onclick_time).count messages_count = course_count + forge_count + user_feedback_count + user_memo_count end @@ -994,6 +1001,10 @@ class User < Principal self.acts << Activity.new(:user_id => self.id) end + def add_onclick_time + self.onclick_time << OnclickTime.new(:user_id => self.id, :onclick_time => self.created_on) + end + # Removes references that are not handled by associations # Things that are not deleted are reassociated with the anonymous user def remove_references_before_destroy