60 lines
2.5 KiB
Ruby
60 lines
2.5 KiB
Ruby
# fq
|
||
# 数据库字段中带有m前缀和is_readed是二次开发添加,之前的字段基本复用
|
||
# 注意reply_id 是提到人的id,不是留言id, Base中叫做ator
|
||
class JournalsForMessage < ActiveRecord::Base
|
||
include Redmine::SafeAttributes
|
||
safe_attributes "jour_type", # 留言所属类型
|
||
"jour_id", # 留言所属类型的id
|
||
"notes", # 留言内容
|
||
"reply_id", # 留言被回复留言者的用户id(用户a回复了用户b,这是b的id,用以查询谁给b留言了)
|
||
"status", # 留言是否被查看(弃用)
|
||
"user_id", # 留言者的id
|
||
"m_parent_id", # 留言信息的父留言id
|
||
"is_readed", # 留言是否已读
|
||
"m_reply_count", # 留言的回复数量
|
||
"m_reply_id" # 回复某留言的留言id(a留言回复了b留言,这是b留言的id)
|
||
acts_as_tree :foreign_key => 'm_parent_id', :counter_cache => :m_reply_count, :order => "#{JournalsForMessage.table_name}.created_on ASC"
|
||
|
||
belongs_to :jour, :polymorphic => true
|
||
belongs_to :user
|
||
belongs_to :ator, :class_name => "User", :foreign_key => 'reply_id'
|
||
|
||
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
|
||
has_many :reply_for_journals, :dependent => :destroy
|
||
|
||
validates :notes, presence: true
|
||
after_create :act_as_activity #huang
|
||
after_create :reset_counters!
|
||
after_destroy :reset_counters!
|
||
|
||
def self.delete_message(message_id)
|
||
self.delete_all "id = #{message_id}"
|
||
end
|
||
|
||
def reference_user
|
||
User.find(reply_id)
|
||
end
|
||
|
||
def self.reference_message(user_id)
|
||
@user = User.find(user_id)
|
||
message = JournalsForMessage.find_by_sql("select * from journals_for_messages where reply_id = #{@user.id} or (jour_type = 'Bid' and jour_id in (select id from bids where author_id = #{@user.id}))")
|
||
message
|
||
end
|
||
|
||
def act_as_activity
|
||
if self.jour_type == 'Principal'
|
||
unless self.user_id == self.jour.id && self.user_id != self.reply_id && self.reply_id != 0
|
||
self.acts << Activity.new(:user_id => self.user_id)
|
||
end
|
||
end
|
||
end
|
||
|
||
def reset_counters!
|
||
self.class.reset_counters!(id)
|
||
end
|
||
def self.reset_counters! journals_for_messages_id
|
||
jfm_id = journals_for_messages_id.to_i
|
||
update_all("m_reply_count = (SELECT COUNT(*) FROM #{JournalsForMssages.table_name} WHERE m_parent_id = #{jfm_id} )", ["id = ?", jfm_id])
|
||
end
|
||
end
|