2013-11-22 10:24:15 +08:00
class Forum < ActiveRecord :: Base
2013-11-22 21:55:21 +08:00
include Redmine :: SafeAttributes
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 "
2013-11-22 21:55:21 +08:00
belongs_to :creator , :class_name = > " User " , :foreign_key = > 'creator_id'
safe_attributes 'name' ,
2013-11-28 15:29:19 +08:00
'description' ,
'topic_count' ,
'memo_count' ,
'last_memo_id' ,
'creator_id'
2013-11-22 21:55:21 +08:00
validates_presence_of :name , :creator_id
validates_length_of :name , maximum : 50
validates_length_of :description , maximum : 255
validates :name , :uniqueness = > true
2013-11-24 20:45:04 +08:00
acts_as_taggable
scope :by_join_date , order ( " created_at DESC " )
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?
user . admin?
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 ] )
end
2013-11-22 10:24:15 +08:00
end