2013-08-08 17:01:56 +08:00
|
|
|
class PraiseTread < ActiveRecord::Base
|
|
|
|
attr_accessible :user_id,:praise_tread_object_id,:praise_tread_object_type,:praise_or_tread
|
2014-04-22 08:56:26 +08:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :praise_tread_object, polymorphic: true
|
2014-06-17 15:57:31 +08:00
|
|
|
after_create :be_user_score
|
2014-08-13 11:17:22 +08:00
|
|
|
after_destroy :down_user_score
|
|
|
|
include UserScoreHelper
|
2014-06-13 16:09:43 +08:00
|
|
|
def self.find_object_by_type_and_id(type,id)
|
2014-06-11 17:41:47 +08:00
|
|
|
@obj = nil
|
|
|
|
case type
|
|
|
|
when 'User'
|
|
|
|
@obj = User.find_by_id(id)
|
|
|
|
when 'Issue'
|
|
|
|
@obj = Issue.find_by_id(id)
|
|
|
|
when 'Project'
|
|
|
|
@obj = Project.find_by_id(id)
|
|
|
|
when 'Bid'
|
|
|
|
@obj = Bid.find_by_id(id)
|
|
|
|
when 'Contest'
|
|
|
|
@obj = Contest.find_by_id(id)
|
|
|
|
when 'Memo'
|
|
|
|
@obj = Memo.find_by_id(id)
|
|
|
|
when 'Message'
|
|
|
|
@obj = Message.find_by_id(id)
|
|
|
|
end
|
|
|
|
return @obj
|
|
|
|
end
|
|
|
|
|
2014-06-24 17:31:39 +08:00
|
|
|
# 获取裁定对象为Message时Message所属的项目或课程
|
|
|
|
def project
|
|
|
|
project = nil
|
2014-08-13 11:17:22 +08:00
|
|
|
if self.praise_tread_object_type == 'Message' || self.praise_tread_object_type == 'Issues'
|
2014-06-24 17:31:39 +08:00
|
|
|
obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id)
|
|
|
|
project = obj.project
|
|
|
|
end
|
|
|
|
project
|
|
|
|
end
|
|
|
|
|
2014-06-11 17:41:47 +08:00
|
|
|
#更新用户分数 - by zjc
|
|
|
|
def be_user_score
|
|
|
|
#踩贴吧或讨论区帖子
|
2014-08-13 11:17:22 +08:00
|
|
|
if self.praise_or_tread == 0 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message')
|
2014-06-13 16:09:43 +08:00
|
|
|
obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id)
|
2014-06-11 17:41:47 +08:00
|
|
|
target_user = obj.author
|
2014-06-17 15:57:31 +08:00
|
|
|
UserScore.skill(:treaded_by_user, User.current,target_user,self, { praise_tread_id: self.id })
|
2014-08-13 11:17:22 +08:00
|
|
|
update_tread(User.current,1)
|
|
|
|
update_tread(target_user,1)
|
2014-06-11 17:41:47 +08:00
|
|
|
#顶贴吧或讨论区帖子
|
2014-08-13 11:17:22 +08:00
|
|
|
elsif self.praise_or_tread == 1 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message')
|
2014-06-13 16:09:43 +08:00
|
|
|
obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id)
|
2014-06-11 17:41:47 +08:00
|
|
|
target_user = obj.author
|
2014-06-17 15:57:31 +08:00
|
|
|
UserScore.skill(:praised_by_user, User.current,target_user,self,{ praise_tread_id: self.id })
|
2014-08-13 11:17:22 +08:00
|
|
|
update_praise(target_user,1)
|
|
|
|
#更新用户等级
|
|
|
|
UserLevels.update_user_level(target_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down_user_score
|
|
|
|
#踩贴吧或讨论区帖子
|
|
|
|
if self.praise_or_tread == 0 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message')
|
|
|
|
obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id)
|
|
|
|
target_user = obj.author
|
|
|
|
#UserScore.skill(:treaded_by_user, User.current,target_user,self, { praise_tread_id: self.id })
|
|
|
|
update_tread(User.current,1)
|
|
|
|
update_tread(target_user,1)
|
|
|
|
#顶贴吧或讨论区帖子
|
|
|
|
elsif self.praise_or_tread == 1 && (self.praise_tread_object_type == 'Issue' || self.praise_tread_object_type == 'Message')
|
|
|
|
obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id)
|
|
|
|
target_user = obj.author
|
|
|
|
#UserScore.skill(:praised_by_user, User.current,target_user,self,{ praise_tread_id: self.id })
|
|
|
|
update_praise(target_user,1)
|
2014-06-12 16:35:59 +08:00
|
|
|
#更新用户等级
|
2014-06-23 09:27:21 +08:00
|
|
|
UserLevels.update_user_level(target_user)
|
2014-06-11 17:41:47 +08:00
|
|
|
end
|
|
|
|
end
|
2013-08-08 17:01:56 +08:00
|
|
|
end
|