2013-11-22 21:55:21 +08:00
|
|
|
class Memo < ActiveRecord::Base
|
|
|
|
include Redmine::SafeAttributes
|
2014-07-24 18:05:37 +08:00
|
|
|
include UserScoreHelper
|
2013-11-28 15:29:19 +08:00
|
|
|
belongs_to :forum
|
2013-11-22 21:55:21 +08:00
|
|
|
belongs_to :author, :class_name => "User", :foreign_key => 'author_id'
|
2013-11-26 16:32:08 +08:00
|
|
|
|
2014-07-04 10:10:20 +08:00
|
|
|
validates_presence_of :author_id, :forum_id, :subject,:content
|
2014-04-14 10:15:18 +08:00
|
|
|
# 若是主题帖,则内容可以是空
|
2014-07-04 10:10:20 +08:00
|
|
|
#validates :content, presence: true, if: Proc.new{|o| !o.parent_id.nil? }
|
2013-11-26 16:32:08 +08:00
|
|
|
validates_length_of :subject, maximum: 50
|
2014-04-14 10:15:18 +08:00
|
|
|
validates_length_of :content, maximum: 3072
|
2013-11-26 16:32:08 +08:00
|
|
|
validate :cannot_reply_to_locked_topic, :on => :create
|
|
|
|
|
|
|
|
acts_as_tree :counter_cache => :replies_count, :order => "#{Memo.table_name}.created_at ASC"
|
|
|
|
acts_as_attachable
|
2014-06-17 15:57:31 +08:00
|
|
|
has_many :user_score_details, :class_name => 'UserScoreDetails',:as => :score_changeable_obj
|
2013-12-09 18:31:08 +08:00
|
|
|
belongs_to :last_reply, :class_name => 'Memo', :foreign_key => 'last_reply_id'
|
2013-11-26 16:32:08 +08:00
|
|
|
# acts_as_searchable :column => ['subject', 'content'],
|
2013-11-28 15:29:19 +08:00
|
|
|
# #:include => { :forum => :p}
|
2013-11-26 16:32:08 +08:00
|
|
|
# #:project_key => "#{Forum.table_name}.project_id"
|
|
|
|
# :date_column => "#{table_name}.created_at"
|
2013-12-04 15:31:18 +08:00
|
|
|
acts_as_event :title => Proc.new {|o| "#{o.forum.name}: #{o.subject}"},
|
2013-12-27 13:54:57 +08:00
|
|
|
:datetime => :updated_at,
|
|
|
|
# :datetime => :created_at,
|
2013-12-04 15:31:18 +08:00
|
|
|
:description => :content,
|
|
|
|
:author => :author,
|
2013-12-05 08:52:41 +08:00
|
|
|
:type => Proc.new {|o| o.parent_id.nil? ? 'Memo' : 'Reply'},
|
2013-12-23 09:28:28 +08:00
|
|
|
:url => Proc.new {|o| {:controller => 'memos', :action => 'show', :forum_id => o.forum_id}.merge(o.parent_id.nil? ? {:id => o.id} : {:id => o.parent_id, :r => o.id, :anchor => "reply-#{o.id}"})}
|
2013-12-04 15:31:18 +08:00
|
|
|
acts_as_activity_provider :author_key => :author_id,
|
2013-12-04 16:46:16 +08:00
|
|
|
:func => 'memos',
|
|
|
|
:timestamp => 'created_at'
|
2013-12-04 15:31:18 +08:00
|
|
|
# :find_options => {:type => 'memos'}
|
|
|
|
# acts_as_watchable
|
2013-11-26 16:32:08 +08:00
|
|
|
|
2013-11-22 21:55:21 +08:00
|
|
|
safe_attributes "author_id",
|
|
|
|
"subject",
|
|
|
|
"content",
|
|
|
|
"forum_id",
|
2013-11-28 15:29:19 +08:00
|
|
|
"last_memo_id",
|
2013-11-22 21:55:21 +08:00
|
|
|
"lock",
|
2013-12-12 09:14:10 +08:00
|
|
|
"sticky",
|
2013-11-22 21:55:21 +08:00
|
|
|
"parent_id",
|
2013-12-12 09:14:10 +08:00
|
|
|
"replies_count"
|
2013-11-26 16:32:08 +08:00
|
|
|
|
2014-06-17 15:57:31 +08:00
|
|
|
after_create :add_author_as_watcher, :reset_counters!,:be_user_score
|
2013-11-26 16:32:08 +08:00
|
|
|
# after_update :update_memos_forum
|
2014-07-29 10:06:48 +08:00
|
|
|
after_destroy :reset_counters!,:down_user_score
|
2013-11-26 16:32:08 +08:00
|
|
|
# after_create :send_notification
|
2013-11-28 15:29:19 +08:00
|
|
|
# after_save :plusParentAndForum
|
|
|
|
# after_destroy :minusParentAndForum
|
2014-06-17 15:57:31 +08:00
|
|
|
#before_save :be_user_score
|
2013-11-26 16:32:08 +08:00
|
|
|
# scope :visible, lambda { |*args|
|
|
|
|
# includes(:forum => ).where()
|
|
|
|
# }
|
|
|
|
|
|
|
|
def cannot_reply_to_locked_topic
|
2013-12-11 08:52:55 +08:00
|
|
|
errors.add :base, l(:label_memo_locked) if root.locked? && self != root
|
2013-11-26 16:32:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# def update_memos_forum
|
|
|
|
# if forum_id_changed?
|
|
|
|
# Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id ])
|
|
|
|
# Forum.reset_counters!(forum_id_was)
|
|
|
|
# Forum.reset_counters!(forum_id)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
def reset_counters!
|
|
|
|
if parent && parent.id
|
|
|
|
Memo.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id})
|
2013-12-27 15:04:05 +08:00
|
|
|
parent.update_attribute(:updated_at, Time.now)
|
2013-11-26 16:32:08 +08:00
|
|
|
end
|
2013-11-28 15:29:19 +08:00
|
|
|
forum.reset_counters!
|
2013-11-26 16:32:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def sticky?
|
|
|
|
sticky == 1
|
|
|
|
end
|
|
|
|
|
2013-11-23 08:20:03 +08:00
|
|
|
def replies
|
|
|
|
Memo.where("parent_id = ?", id)
|
|
|
|
end
|
2013-11-26 16:32:08 +08:00
|
|
|
|
2013-11-23 20:02:15 +08:00
|
|
|
def locked?
|
|
|
|
self.lock
|
|
|
|
end
|
|
|
|
|
|
|
|
def editable_by? user
|
2013-11-26 16:32:08 +08:00
|
|
|
# user && user.logged? || (self.author == usr && usr.allowed_to?(:edit_own_messages, project))
|
2013-11-26 16:55:46 +08:00
|
|
|
user.admin?
|
2013-11-23 20:02:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroyable_by? user
|
2013-12-12 09:14:10 +08:00
|
|
|
(user && user.logged? && (Forum.find(self.forum_id).creator_id == user.id) ) || user.admin?
|
2013-11-26 16:32:08 +08:00
|
|
|
#self.author == user || user.admin?
|
2013-11-23 20:02:15 +08:00
|
|
|
end
|
2013-11-26 16:32:08 +08:00
|
|
|
|
2013-12-12 09:14:10 +08:00
|
|
|
def deleted_attach_able_by? user
|
|
|
|
(user && user.logged? && (self.author == user) ) || user.admin?
|
|
|
|
end
|
|
|
|
|
2013-11-26 16:32:08 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def add_author_as_watcher
|
|
|
|
Watcher.create(:watchable => self.root, :user => author)
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_notification
|
|
|
|
if Setting.notified_events.include?('message_posted')
|
|
|
|
Mailer.message_posted(self).deliver
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-27 10:16:35 +08:00
|
|
|
def plusParentAndForum
|
|
|
|
@forum = Forum.find(self.forum_id)
|
|
|
|
@forum.memo_count = @forum.memo_count.to_int + 1
|
|
|
|
@forum.last_memo_id = self.id
|
|
|
|
if self.parent_id
|
|
|
|
@parent_memo = Memo.find_by_id(self.parent_id)
|
|
|
|
@parent_memo.last_reply_id = self
|
|
|
|
@parent_memo.replies_count = @parent_memo.replies_count.to_int + 1
|
|
|
|
@parent_memo.save
|
|
|
|
else
|
|
|
|
@forum.topic_count = @forum.topic_count.to_int + 1
|
|
|
|
end
|
|
|
|
@forum.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def minusParentAndForum
|
|
|
|
@forum = Forum.find(self.forum_id)
|
|
|
|
@forum.memo_count = @forum.memo_count.to_int - 1
|
|
|
|
@forum.memo_count = 0 if @forum.memo_count.to_int < 0
|
|
|
|
# @forum.last_memo_id = Memo.reorder('created_at ASC').find_all_by_forum_id(self.forum_id).last.id
|
|
|
|
if self.parent_id
|
|
|
|
@parent_memo = Memo.find_by_id(self.parent_id)
|
|
|
|
# @parent_memo.last_reply_id = Memo.reorder('created_at ASC').find_all_by_parent_id(self.parent_id).last.id
|
|
|
|
@parent_memo.replies_count = @parent_memo.replies_count.to_int - 1
|
|
|
|
@parent_memo.replies_count = 0 if @parent_memo.replies_count.to_int < 0
|
|
|
|
@parent_memo.save
|
|
|
|
else
|
|
|
|
@forum.topic_count = @forum.topic_count.to_int - 1
|
|
|
|
@forum.topic_count = 0 if @forum.topic_count.to_int < 0
|
|
|
|
end
|
|
|
|
@forum.save
|
2014-06-11 17:41:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
#更新用户分数 -by zjc
|
|
|
|
def be_user_score
|
|
|
|
#新建memo且无parent的为发帖
|
2014-06-17 15:57:31 +08:00
|
|
|
if self.parent_id.nil?
|
|
|
|
UserScore.joint(:post_message, User.current,nil,self ,{ memo_id: self.id })
|
2014-07-24 18:05:37 +08:00
|
|
|
update_memo_number(User.current,1)
|
2014-06-11 17:41:47 +08:00
|
|
|
#新建memo且有parent的为回帖
|
2014-06-17 15:57:31 +08:00
|
|
|
elsif !self.parent_id.nil?
|
|
|
|
UserScore.joint(:reply_posting, User.current,self.parent.author,self, { memo_id: self.id })
|
2014-07-24 18:05:37 +08:00
|
|
|
update_replay_for_memo(User.current,1)
|
2014-06-11 17:41:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-29 10:06:48 +08:00
|
|
|
#被删除时更新用户分数
|
|
|
|
def down_user_score
|
|
|
|
update_memo_number(User.current,1)
|
|
|
|
update_replay_for_memo(User.current,1)
|
|
|
|
end
|
|
|
|
|
2013-11-22 21:55:21 +08:00
|
|
|
end
|