2013-11-22 10:24:15 +08:00
class Forum < ActiveRecord :: Base
2013-11-22 21:55:21 +08:00
include Redmine :: SafeAttributes
2015-01-09 15:28:43 +08:00
include ExpireHelper
2013-12-27 13:54:57 +08:00
has_many :topics , :class_name = > 'Memo' , :conditions = > " #{ Memo . table_name } .parent_id IS NULL " , :order = > " #{ Memo . table_name } .created_at DESC " , :dependent = > :destroy
2014-04-28 20:37:12 +08:00
has_many :memos , :dependent = > :destroy , conditions : " parent_id IS NULL "
2015-01-07 09:27:44 +08:00
belongs_to :creator , :class_name = > " User " , :foreign_key = > 'creator_id'
2015-01-09 15:28:43 +08:00
after_create :expire_forum_cache
after_update :expire_forum_cache
before_destroy :expire_forum_cache
2013-11-22 21:55:21 +08:00
safe_attributes 'name' ,
2013-11-28 15:29:19 +08:00
'description' ,
'topic_count' ,
'memo_count' ,
'last_memo_id' ,
2014-11-03 15:16:56 +08:00
'creator_id' ,
'sticky' ,
'locked'
2014-07-05 14:43:15 +08:00
validates_presence_of :name , :creator_id , :description
2013-11-22 21:55:21 +08:00
validates_length_of :name , maximum : 50
2014-08-14 14:38:18 +08:00
#validates_length_of :description, maximum: 255
2013-11-22 21:55:21 +08:00
validates :name , :uniqueness = > true
2013-11-24 20:45:04 +08:00
acts_as_taggable
scope :by_join_date , order ( " created_at DESC " )
2015-02-02 11:00:23 +08:00
#after_create :send_email
2013-11-28 15:29:19 +08:00
def reset_counters!
self . class . reset_counters! ( id )
end
2013-12-07 10:15:55 +08:00
def editable_by? user
# user && user.logged? || (self.author == usr && usr.allowed_to?(:edit_own_messages, project))
self . creator == user || user . admin?
end
def destroyable_by? user
# user && user.logged? && Forum.find(self.forum_id).creator_id == user.id || user.admin?
2014-11-20 15:55:43 +08:00
self . creator == user || user . admin?
2013-12-07 10:15:55 +08:00
end
2015-01-22 16:30:21 +08:00
def send_email
Thread . start do
Mailer . forum_add ( self ) . deliver if Setting . notified_events . include? ( 'forum_add' )
end
end
2013-11-28 15:29:19 +08:00
# Updates topic_count, memo_count and last_memo_id attributes for +board_id+
def self . reset_counters! ( forum_id )
forum_id = forum_id . to_i
update_all ( " topic_count = (SELECT COUNT(*) FROM #{ Memo . table_name } WHERE forum_id= #{ forum_id } AND parent_id IS NULL), " +
2013-11-28 15:51:38 +08:00
" memo_count = (SELECT COUNT(*) FROM #{ Memo . table_name } WHERE forum_id= #{ forum_id } AND parent_id IS NOT NULL), " +
2013-11-28 15:29:19 +08:00
" last_memo_id = (SELECT MAX(id) FROM #{ Memo . table_name } WHERE forum_id= #{ forum_id } ) " ,
[ " id = ? " , forum_id ] )
2015-01-07 09:27:44 +08:00
end
2015-01-09 15:28:43 +08:00
2013-11-22 10:24:15 +08:00
end