From 0064aa42787071547f09d3a8411763170955e094 Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 8 Oct 2015 16:10:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=B6=88=E6=81=AF=E4=B8=8D?= =?UTF-8?q?=E6=B6=88=E5=A4=B1=20=E7=B3=BB=E7=BB=9F=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=92=8C=E6=99=AE=E9=80=9A=E6=B6=88=E6=81=AF=E4=B8=80=E8=87=B4?= =?UTF-8?q?=EF=BC=8C=E6=8C=89=E7=85=A7=E6=97=B6=E9=97=B4=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=20=E5=BE=85=E4=BC=98=E5=8C=96=E7=82=B9=EF=BC=9A=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 13 +++++--- app/models/system_message.rb | 11 +++++++ app/views/users/_user_message_system.html.erb | 30 +++++++++++++++++ app/views/users/user_messages.html.erb | 33 ++----------------- 4 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 app/views/users/_user_message_system.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 73a26b52c..fc19836ce 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -112,10 +112,17 @@ class UsersController < ApplicationController case params[:type] when nil @message_alls = [] - messages = MessageAll.where("user_id =?" ,@user).order("created_at desc") + system_message_types = MessageAll.where("message_type =?", "SystemMessage") + messages = MessageAll.where("user_id =? and message_type !=?" ,@user, "SystemMessage") messages.each do |message_all| @message_alls << message_all.message end + # 系统消息需要给每个人发送,之所以这样做,可以避免遍历系统所有用户 + system_message_types.each do |system_message_type| + @message_alls << system_message_type.message + end + # 取出所有符合条件的按照创建时间排序 + @message_alls = @message_alls.sort {|a,b| b.created_at <=> a.created_at} when 'unviewed' @message_alls = [] messages = MessageAll.where("user_id =?", @user).order("created_at desc") @@ -187,12 +194,8 @@ class UsersController < ApplicationController message_new_time.onclick_time = Time.now message_new_time.save else - # 24小时内显示 - contrast_time = Time.now - 86400 message_time.update_attributes(:onclick_time => Time.now) end - # 24小时内显示系统消息 - @user_system_messages = SystemMessage.where("created_at >?", contrast_time).order("created_at desc") end # 消息设置为已读 diff --git a/app/models/system_message.rb b/app/models/system_message.rb index 2a810e8b8..a05610da7 100644 --- a/app/models/system_message.rb +++ b/app/models/system_message.rb @@ -5,4 +5,15 @@ class SystemMessage < ActiveRecord::Base validates :subject, presence: true # validates :description, presence: true validates_length_of :description, maximum: 10000 + + has_many :message_alls, :class_name => 'MessageAll',:as =>:message, :dependent => :destroy + + # 系统消息放置总消息列表 + after_create :add_system_message + + def add_system_message + if MessageAll.where("message_type = '#{self.class.to_s}' and message_id = '#{self.id}'").first.nil? + self.message_alls << MessageAll.new(:user_id => self.user_id) + end + end end diff --git a/app/views/users/_user_message_system.html.erb b/app/views/users/_user_message_system.html.erb new file mode 100644 index 000000000..efbd5d76b --- /dev/null +++ b/app/views/users/_user_message_system.html.erb @@ -0,0 +1,30 @@ +<% if ma.class == SystemMessage %> + <%# @user_system_messages.each do |usm| %> + + <%# end %> +<% end %> \ No newline at end of file diff --git a/app/views/users/user_messages.html.erb b/app/views/users/user_messages.html.erb index bd794438c..b3a09386e 100644 --- a/app/views/users/user_messages.html.erb +++ b/app/views/users/user_messages.html.erb @@ -41,40 +41,13 @@ <% end %> <% end %> - <%# 系统消息 %> - <% if params[:type] != 'system_messages' %> - <% @user_system_messages.each do |usm| %> - - <% end %> - <% end %> <% unless @message_alls.nil? %> <% @message_alls.each do |ma| %> + <%# 系统消息 %> + <%= render :partial => 'users/user_message_system', :locals => {:ma => ma} %> + <%# 课程消息 %> <%= render :partial => 'users/user_message_course', :locals => {:ma => ma} %>