socialforge/app/models/forge_activity.rb

46 lines
1.5 KiB
Ruby

# 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
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