2013-11-22 21:55:21 +08:00
|
|
|
class Memo < ActiveRecord::Base
|
|
|
|
include Redmine::SafeAttributes
|
|
|
|
belongs_to :forums
|
|
|
|
belongs_to :author, :class_name => "User", :foreign_key => 'author_id'
|
2013-11-26 16:32:08 +08:00
|
|
|
|
|
|
|
validates_presence_of :author_id, :forum_id, :subject
|
|
|
|
validates :content, presence: true
|
|
|
|
validates_length_of :subject, maximum: 50
|
|
|
|
validates_length_of :content, maximum: 2048
|
|
|
|
validate :cannot_reply_to_locked_topic, :on => :create
|
|
|
|
validate :content_cannot_be_blank
|
|
|
|
|
|
|
|
acts_as_tree :counter_cache => :replies_count, :order => "#{Memo.table_name}.created_at ASC"
|
|
|
|
acts_as_attachable
|
|
|
|
belongs_to :last_reply_id, :class_name => 'Memo', :foreign_key => 'last_reply_id'
|
|
|
|
# acts_as_searchable :column => ['subject', 'content'],
|
|
|
|
# #:include => { :forums => :p}
|
|
|
|
# #:project_key => "#{Forum.table_name}.project_id"
|
|
|
|
# :date_column => "#{table_name}.created_at"
|
|
|
|
# acts_as_event :title => Proc.new {|o| "#{o.forums.name}: #{o.subject}"},
|
|
|
|
# :description => :content,
|
|
|
|
# :group => :parent,
|
|
|
|
# :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
|
|
|
|
# :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 => "memo-#{o.id}"})}
|
|
|
|
acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
|
|
|
|
:author_key => :author_id
|
|
|
|
acts_as_watchable
|
|
|
|
|
2013-11-22 21:55:21 +08:00
|
|
|
safe_attributes "author_id",
|
|
|
|
"subject",
|
|
|
|
"content",
|
|
|
|
"forum_id",
|
|
|
|
"last_reply_id",
|
|
|
|
"lock",
|
|
|
|
"parent_id",
|
|
|
|
"replies_count",
|
|
|
|
"sticky"
|
2013-11-26 16:32:08 +08:00
|
|
|
|
|
|
|
after_create :add_author_as_watcher, :reset_counters!
|
|
|
|
# after_update :update_memos_forum
|
|
|
|
after_destroy :reset_counters!
|
|
|
|
# after_create :send_notification
|
|
|
|
|
|
|
|
# scope :visible, lambda { |*args|
|
|
|
|
# includes(:forum => ).where()
|
|
|
|
# }
|
|
|
|
|
|
|
|
def cannot_reply_to_locked_topic
|
|
|
|
errors.add :base, 'Topic is locked' if root.locked? && self != root
|
|
|
|
end
|
|
|
|
def content_cannot_be_blank
|
|
|
|
errors.add :base, "content can't be blank." if self.content.blank?
|
|
|
|
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})
|
|
|
|
end
|
|
|
|
# forums.reset_counters!
|
|
|
|
end
|
2013-11-23 08:20:03 +08:00
|
|
|
|
2013-11-26 16:32:08 +08:00
|
|
|
def sticky=(arg)
|
|
|
|
write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
|
|
|
|
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-11-26 16:55:46 +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
|
|
|
|
|
|
|
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-22 21:55:21 +08:00
|
|
|
end
|