You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/app/models/praise_tread.rb

117 lines
4.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

class PraiseTread < ActiveRecord::Base
# praise_or_tread 1赞 2
attr_accessible :user_id,:praise_tread_object_id,:praise_tread_object_type,:praise_or_tread
belongs_to :user
belongs_to :praise_tread_object, polymorphic: true
#after_create :be_user_score
#after_destroy :down_user_score
include UserScoreHelper
has_many :tidings, :as => :container, :dependent => :destroy
after_create :send_tiding
scope :liker, lambda{where(:praise_or_tread => 1)}
def send_tiding
case self.praise_tread_object_type
when "Memo","Message","Issue"
self.tidings << Tiding.new(:trigger_user_id => self.user_id, :user_id => self.praise_tread_object.author_id, :parent_container_id => self.praise_tread_object_id, :parent_container_type => self.praise_tread_object_type, :viewed => 0, :tiding_type => "Praise")
when "Discuss","Challenge","HomeworkCommon","JournalsForMessage","Journal","GraduationTopic","GraduationTask"
self.tidings << Tiding.new(:trigger_user_id => self.user_id, :user_id => self.praise_tread_object.user_id, :parent_container_id => self.praise_tread_object_id, :parent_container_type => self.praise_tread_object_type, :viewed => 0, :tiding_type => "Praise")
end
end
def self.find_object_by_type_and_id(type,id)
@obj = nil
case type
when 'User'
@obj = User.find_by_id(id)
when 'Issue'
@obj = Issue.find_by_id(id)
when 'TrainingTask'
@obj = TrainingTask.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
# 获取裁定对象为Message时Message所属的项目或课程
def project
project = nil
if self.praise_tread_object_type == 'Message' || self.praise_tread_object_type == 'Issues'
obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id)
project = obj.project
end
project
end
#更新用户分数 - by zjc
def be_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(self.user,1)
update_tread(target_user,1)
unless self.project.nil?
update_tread(self.user,2,self.project)
update_tread(target_user,2,self.project)
end
#顶贴吧或讨论区帖子
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)
unless self.project.nil?
update_praise(target_user,2,self.project)
end
#更新用户等级
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(self.user,1)
update_tread(target_user,1)
unless self.project.nil?
update_tread(self.user,2,self.project)
update_tread(target_user,2,self.project)
end
#顶贴吧或讨论区帖子
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)
unless self.project.nil?
update_praise(target_user,2,self.project)
end
#更新用户等级
UserLevels.update_user_level(target_user)
end
end
def self.praised(activity)
self.where(praise_tread_object_id: activity.id,
praise_tread_object_type: activity.class.to_s,
user_id: User.current.id).empty?
end
end