From 2db6696fd42315cbafe7317f66f2288b9c90188d Mon Sep 17 00:00:00 2001 From: sw <939547590@qq.com> Date: Fri, 21 Aug 2015 14:21:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AA=E4=BA=BA=E5=8A=A8=E6=80=81=E7=9A=84?= =?UTF-8?q?=E7=BA=A7=E8=81=94=E6=96=B0=E5=A2=9E=E5=92=8C=E7=BA=A7=E8=81=94?= =?UTF-8?q?=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/course_activity.rb | 23 +++++++++++++++++++++++ app/models/forge_activity.rb | 22 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/app/models/course_activity.rb b/app/models/course_activity.rb index 02b6dacf1..cf356dc05 100644 --- a/app/models/course_activity.rb +++ b/app/models/course_activity.rb @@ -4,4 +4,27 @@ class CourseActivity < ActiveRecord::Base belongs_to :course_act ,:polymorphic => true belongs_to :course belongs_to :user + has_many :user_acts, :class_name => 'UserAcivity',:as =>:act + after_save :add_user_activity + before_destroy :destroy_user_activity + + #在个人动态里面增加当前动态 + def add_user_activity + user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'").first + if user_activity + user_activity.save + else + user_activity = UserActivity.new + user_activity.act_id = self.id + user_activity.act_type = self.class.to_s + user_activity.container_type = "Course" + user_activity.container_id = self.course_id + user_activity.save + end + end + + def destroy_user_activity + user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'") + user_activity.destroy_all + end end diff --git a/app/models/forge_activity.rb b/app/models/forge_activity.rb index 6b75552c0..5d8de8e3a 100644 --- a/app/models/forge_activity.rb +++ b/app/models/forge_activity.rb @@ -19,5 +19,27 @@ class ForgeActivity < ActiveRecord::Base validates :project_id,presence: true validates :forge_act_id,presence: true validates :forge_act_type, presence: true + has_many :user_acts, :class_name => 'UserAcivity',:as =>:act + after_save :add_user_activity + before_destroy :destroy_user_activity + #在个人动态里面增加当前动态 + def add_user_activity + user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'").first + if user_activity + user_activity.save + else + user_activity = UserActivity.new + user_activity.act_id = self.id + user_activity.act_type = self.class.to_s + user_activity.container_type = "Project" + user_activity.container_id = self.project_id + user_activity.save + end + end + + def destroy_user_activity + user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'") + user_activity.destroy_all + end end