2015-03-04 15:32:00 +08:00
|
|
|
# Time 2015-02-06 10:42:34
|
|
|
|
# Author lizanle
|
|
|
|
# Description 这是保存Project相关的动态的公共表
|
|
|
|
class ForgeActivity < ActiveRecord::Base
|
|
|
|
# 公共表中活动类型,命名规则:TYPE_OF_{类名}_ACT
|
|
|
|
TYPE_OF_ISSUE_ACT = "Issue"
|
|
|
|
TYPE_OF_MESSAGE_ACT = "Message"
|
|
|
|
TYPE_OF_ATTACHMENT_ACT = "Attachment"
|
|
|
|
TYPE_OF_DOCUMENT_ACT = "Document"
|
|
|
|
TYPE_OF_JOURNAL_ACT = "Journal"
|
|
|
|
TYPE_OF_WIKI_ACT = "Wiki"
|
|
|
|
TYPE_OF_NEWS_ACT = "News"
|
|
|
|
attr_accessible :forge_act_id, :forge_act_type,:project_id,:user_id,:org_id
|
|
|
|
# 虚拟关联
|
|
|
|
belongs_to :forge_act ,:polymorphic => true
|
|
|
|
belongs_to :project
|
|
|
|
belongs_to :user
|
|
|
|
validates :user_id,presence: true
|
|
|
|
validates :project_id,presence: true
|
|
|
|
validates :forge_act_id,presence: true
|
|
|
|
validates :forge_act_type, presence: true
|
2015-08-21 14:21:10 +08:00
|
|
|
has_many :user_acts, :class_name => 'UserAcivity',:as =>:act
|
2016-05-19 15:26:23 +08:00
|
|
|
after_create :add_user_activity, :add_org_activity
|
2016-01-14 14:32:19 +08:00
|
|
|
before_destroy :destroy_user_activity, :destroy_org_activity
|
2015-06-18 09:13:20 +08:00
|
|
|
|
2015-08-21 14:21:10 +08:00
|
|
|
#在个人动态里面增加当前动态
|
|
|
|
def add_user_activity
|
2015-08-29 15:34:18 +08:00
|
|
|
user_activity = UserActivity.where("act_type = '#{self.forge_act_type.to_s}' and act_id = '#{self.forge_act_id}'").first
|
2015-08-21 14:21:10 +08:00
|
|
|
if user_activity
|
|
|
|
user_activity.save
|
|
|
|
else
|
2015-09-11 16:42:24 +08:00
|
|
|
if self.forge_act_type == 'Message' && !self.forge_act.parent_id.nil?
|
|
|
|
user_activity = UserActivity.where("act_type = 'Message' and act_id = #{self.forge_act.parent.id}").first
|
|
|
|
user_activity.created_at = self.created_at
|
|
|
|
user_activity.save
|
|
|
|
else
|
|
|
|
user_activity = UserActivity.new
|
|
|
|
user_activity.act_id = self.forge_act_id
|
|
|
|
user_activity.act_type = self.forge_act_type
|
|
|
|
user_activity.container_type = "Project"
|
|
|
|
user_activity.container_id = self.project_id
|
|
|
|
user_activity.user_id = self.user_id
|
|
|
|
user_activity.save
|
|
|
|
end
|
2015-08-21 14:21:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-16 15:02:57 +08:00
|
|
|
def add_org_activity
|
2015-12-07 17:11:30 +08:00
|
|
|
org_activity = OrgActivity.where("org_act_type = '#{self.forge_act_type.to_s}' and org_act_id = #{self.forge_act_id}").first
|
|
|
|
if org_activity
|
2016-03-10 16:43:50 +08:00
|
|
|
org_activity.updated_at = self.updated_at
|
2015-12-07 17:11:30 +08:00
|
|
|
org_activity.save
|
|
|
|
else
|
|
|
|
if self.forge_act_type == 'Message' && !self.forge_act.parent_id.nil?
|
|
|
|
org_activity = OrgActivity.where("org_act_type = 'Message' and org_act_id = #{self.forge_act.parent.id}").first
|
2015-12-04 10:59:09 +08:00
|
|
|
org_activity.created_at = self.created_at
|
|
|
|
org_activity.save
|
2015-12-07 17:11:30 +08:00
|
|
|
else
|
|
|
|
OrgActivity.create(:user_id => self.user_id,
|
|
|
|
:org_act_id => self.forge_act_id,
|
|
|
|
:org_act_type => self.forge_act_type,
|
|
|
|
:container_id => self.project_id,
|
|
|
|
:container_type => 'Project',
|
|
|
|
:created_at => self.created_at,
|
|
|
|
:updated_at => self.updated_at)
|
2015-12-04 10:59:09 +08:00
|
|
|
end
|
2015-11-20 16:41:15 +08:00
|
|
|
end
|
2015-11-16 15:02:57 +08:00
|
|
|
end
|
|
|
|
|
2015-08-21 14:21:10 +08:00
|
|
|
def destroy_user_activity
|
2015-08-29 15:34:18 +08:00
|
|
|
user_activity = UserActivity.where("act_type = '#{self.forge_act_type.to_s}' and act_id = '#{self.forge_act_id}'")
|
2015-08-21 14:21:10 +08:00
|
|
|
user_activity.destroy_all
|
|
|
|
end
|
2015-11-20 16:41:15 +08:00
|
|
|
|
|
|
|
def destroy_org_activity
|
2015-11-20 17:42:43 +08:00
|
|
|
org_acts = OrgActivity.where("org_act_type='#{self.forge_act_type.to_s}' and org_act_id = '#{self.forge_act_id}'")
|
2015-11-20 16:41:15 +08:00
|
|
|
org_acts.destroy_all
|
|
|
|
end
|
2015-03-04 15:32:00 +08:00
|
|
|
end
|