From d657e9f3cc44e2964828eb6dd173eb9cd7c1bf38 Mon Sep 17 00:00:00 2001 From: huang Date: Wed, 19 Aug 2015 11:06:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E8=B4=B4=E5=90=A7=20=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 11 ++-- app/models/forum.rb | 1 + app/models/forum_message.rb | 11 ++++ app/models/memo.rb | 30 ++++++++++- app/models/user.rb | 4 +- .../20150818091800_create_forum_messages.rb | 13 +++++ db/schema.rb | 50 ++++++++++++++++++- spec/factories/forum_messages.rb | 10 ++++ spec/models/forum_message_spec.rb | 5 ++ 9 files changed, 126 insertions(+), 9 deletions(-) create mode 100644 app/models/forum_message.rb create mode 100644 db/migrate/20150818091800_create_forum_messages.rb create mode 100644 spec/factories/forum_messages.rb create mode 100644 spec/models/forum_message_spec.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 00ce151cf..d77ccb958 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -97,8 +97,8 @@ class UsersController < ApplicationController end # 用户消息 - # 说明: homework 发布作业;message:讨论区; news:新闻; poll:问卷;works_reviewers:作品评阅 - # issue:问题; + # 说明: homework 发布作业;message:讨论区; news:新闻; poll:问卷;works_reviewers:作品评阅;works_reply:作品回复 + # issue:问题;journal:缺陷状态更新; forum:公共贴吧 def user_messages unless User.current.logged? render_403 @@ -114,6 +114,7 @@ class UsersController < ApplicationController when nil @user_course_messages = @user.course_messages.reverse @user_forge_messages = @user.forge_messages.reverse + @user_forum_messages = @user.forum_messages.reverse when 'homework' @user_course_messages = CourseMessage.where("course_message_type =? and user_id =?", "HomeworkCommon", @user).order("created_at desc") @user_forge_messages = nil @@ -134,9 +135,11 @@ class UsersController < ApplicationController when 'issue' @user_forge_messages = ForgeMessage.where("forge_message_type =? and user_id =?", "Issue", @user).order("created_at desc") @user_course_messages = nil - when 'journal' - @user_forge_messages = ForgeMessage.where("forge_message_type =? and user_id =?", "Journal", @user).order("created_at desc") + when 'journal' # 缺陷状态更新、留言 + @user_forge_messages = ForgeMessage.where("forge_message_type =? and user_id =?", "Forum", @user).order("created_at desc") @user_course_messages = nil + when 'forum' + @user_forum_messages = ForumMessage.where("memo_message_type =? and user_id =?", "Forum", @user).order("created_at desc") end respond_to do |format| format.html{render :layout=>'base_users_new'} diff --git a/app/models/forum.rb b/app/models/forum.rb index 2af1abf9e..530639f81 100644 --- a/app/models/forum.rb +++ b/app/models/forum.rb @@ -39,6 +39,7 @@ class Forum < ActiveRecord::Base logger.debug "send mail for forum add." Mailer.run.forum_add(self) if Setting.notified_events.include?('forum_add') end + # Updates topic_count, memo_count and last_memo_id attributes for +board_id+ def self.reset_counters!(forum_id) forum_id = forum_id.to_i diff --git a/app/models/forum_message.rb b/app/models/forum_message.rb new file mode 100644 index 000000000..c7b07e164 --- /dev/null +++ b/app/models/forum_message.rb @@ -0,0 +1,11 @@ +class ForumMessage < ActiveRecord::Base + attr_accessible :forum_id, :memo_message_id, :memo_message_type, :user_id, :viewed + + belongs_to :memo + belongs_to :user + + validates :user_id,presence: true + validates :forum_id,presence: true + validates :memo_message_id,presence: true + validates :memo_message_type, presence: true +end diff --git a/app/models/memo.rb b/app/models/memo.rb index e0abaa19f..e4dabf131 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -16,6 +16,8 @@ class Memo < ActiveRecord::Base acts_as_attachable has_many :user_score_details, :class_name => 'UserScoreDetails',:as => :score_changeable_obj has_many :praise_tread, as: :praise_tread_object, dependent: :destroy + # 消息 + has_many :forum_messages, dependent: :destroy belongs_to :last_reply, :class_name => 'Memo', :foreign_key => 'last_reply_id' # acts_as_searchable :column => ['subject', 'content'], # #:include => { :forum => :p} @@ -44,7 +46,7 @@ class Memo < ActiveRecord::Base "parent_id", "replies_count" - after_create :add_author_as_watcher, :reset_counters!, :send_mail + after_create :add_author_as_watcher, :reset_counters!, :send_mail, :send_message # after_update :update_memos_forum after_destroy :reset_counters!,:delete_kindeditor_assets#,:down_user_score -- 公共区发帖暂不计入得分 # after_create :send_notification @@ -59,6 +61,32 @@ class Memo < ActiveRecord::Base Mailer.run.forum_message_added(self) if Setting.notified_events.include?('forum_message_added') end + # 公共贴吧消息记录 + # 原则:贴吧创始人;发帖人,wanglingchun(特殊用户) + def send_message + receivers = [] + u = User.find(6) + receivers << u + # 主贴 + if self.parent_id.nil? + if self.author_id != self.forum.creator_id # 发帖人不是吧主 + receivers << self.forum.creator + end + else # 回帖 + # 添加吧主 + if self.author_id != self.forum.creator_id + receivers << self.forum.creator + end + # 添加发帖人 + if self.author_id != self.parent.author_id + receivers << self.parent.author + end + end + receivers.each do |r| + self.forum_messages << ForumMessage.new(:user_id => r.id, :forum_id => self.forum_id, :memo_message_id => self.id, :memo_message_type => "Forum", :viewed => false) + end + end + def cannot_reply_to_locked_topic errors.add :base, l(:label_memo_locked) if root.locked? && self != root end diff --git a/app/models/user.rb b/app/models/user.rb index 9f88ff53a..82ecdbd47 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -127,10 +127,10 @@ class User < Principal has_many :messages, :foreign_key => 'author_id' has_one :user_score, :dependent => :destroy has_many :documents # 项目中关联的文档再次与人关联 -# 关联虚拟表 +# 关联消息表 has_many :forge_messages has_many :course_messages -# end + has_many :forum_messages # 虚拟转换 has_many :new_jours, :as => :jour, :class_name => 'JournalsForMessage', :conditions => "status=1" diff --git a/db/migrate/20150818091800_create_forum_messages.rb b/db/migrate/20150818091800_create_forum_messages.rb new file mode 100644 index 000000000..20ad1ca51 --- /dev/null +++ b/db/migrate/20150818091800_create_forum_messages.rb @@ -0,0 +1,13 @@ +class CreateForumMessages < ActiveRecord::Migration + def change + create_table :forum_messages do |t| + t.integer :user_id + t.integer :forum_id + t.integer :memo_message_id + t.string :memo_message_type + t.integer :viewed + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index db9b9ff9b..5471381d7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20150810064247) do +ActiveRecord::Schema.define(:version => 20150818091800) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -325,6 +325,15 @@ ActiveRecord::Schema.define(:version => 20150810064247) do t.datetime "updated_on", :null => false end + create_table "course_activities", :force => true do |t| + t.integer "user_id" + t.integer "course_id" + t.integer "course_act_id" + t.string "course_act_type" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "course_attachments", :force => true do |t| t.string "filename" t.string "disk_filename" @@ -357,6 +366,16 @@ ActiveRecord::Schema.define(:version => 20150810064247) do t.datetime "updated_at", :null => false end + create_table "course_messages", :force => true do |t| + t.integer "user_id" + t.integer "course_id" + t.integer "course_message_id" + t.string "course_message_type" + t.integer "viewed" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "course_statuses", :force => true do |t| t.integer "changesets_count" t.integer "watchers_count" @@ -455,6 +474,13 @@ ActiveRecord::Schema.define(:version => 20150810064247) do add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority" + create_table "discuss_demos", :force => true do |t| + t.string "title" + t.text "body" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "documents", :force => true do |t| t.integer "project_id", :default => 0, :null => false t.integer "category_id", :default => 0, :null => false @@ -536,6 +562,26 @@ ActiveRecord::Schema.define(:version => 20150810064247) do add_index "forge_activities", ["forge_act_id"], :name => "index_forge_activities_on_forge_act_id" + create_table "forge_messages", :force => true do |t| + t.integer "user_id" + t.integer "project_id" + t.integer "forge_message_id" + t.string "forge_message_type" + t.integer "viewed" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + create_table "forum_messages", :force => true do |t| + t.integer "user_id" + t.integer "forum_id" + t.integer "memo_message_id" + t.string "memo_message_type" + t.integer "viewed" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "forums", :force => true do |t| t.string "name", :null => false t.text "description" @@ -857,7 +903,6 @@ ActiveRecord::Schema.define(:version => 20150810064247) do t.datetime "created_on" t.integer "comments_count", :default => 0, :null => false t.integer "course_id" - t.datetime "updated_on" end add_index "news", ["author_id"], :name => "index_news_on_author_id" @@ -1461,6 +1506,7 @@ ActiveRecord::Schema.define(:version => 20150810064247) do t.string "identity_url" t.string "mail_notification", :default => "", :null => false t.string "salt", :limit => 64 + t.integer "gid" end add_index "users", ["auth_source_id"], :name => "index_users_on_auth_source_id" diff --git a/spec/factories/forum_messages.rb b/spec/factories/forum_messages.rb new file mode 100644 index 000000000..adb114d3c --- /dev/null +++ b/spec/factories/forum_messages.rb @@ -0,0 +1,10 @@ +FactoryGirl.define do + factory :forum_message do + user_id 1 +forum_id 1 +memo_message_id 1 +memo_message_type "MyString" +viewed 1 + end + +end diff --git a/spec/models/forum_message_spec.rb b/spec/models/forum_message_spec.rb new file mode 100644 index 000000000..e0159c3ca --- /dev/null +++ b/spec/models/forum_message_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ForumMessage, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end