socialforge/app/models/forum.rb

57 lines
1.9 KiB
Ruby
Raw Normal View History

2013-11-22 10:24:15 +08:00
class Forum < ActiveRecord::Base
2013-11-22 21:55:21 +08:00
include Redmine::SafeAttributes
include ExpireHelper
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'
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',
'creator_id',
'sticky',
'locked'
validates_presence_of :name, :creator_id, :description
2013-11-22 21:55:21 +08:00
validates_length_of :name, maximum: 50
#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")
#after_create :send_email
2013-11-28 15:29:19 +08:00
def reset_counters!
self.class.reset_counters!(id)
end
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?
self.creator == user || user.admin?
end
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)," +
" 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
2013-11-22 10:24:15 +08:00
end