1 class Post < ActiveRecord::Base
2 def self.per_page() 25 end
9 before_create { |r| r.forum_id = r.topic.forum_id }
10 after_create :update_cached_fields
11 after_destroy :update_cached_fields
13 validates_presence_of :user_id, :body, :topic
16 def editable_by?(user)
17 user && (user.id == user_id || user.admin? || user.moderator_of?(topic.forum_id))
20 def to_xml(options = {})
21 options[:except] ||= []
22 options[:except] << :topic_title << :forum_name
27 # using count isn't ideal but it gives us correct caches each time
28 def update_cached_fields
29 Forum.update_all ['posts_count = ?', Post.count(:id, :conditions => {:forum_id => forum_id})], ['id = ?', forum_id]
30 User.update_all ['posts_count = ?', Post.count(:id, :conditions => {:user_id => user_id})], ['id = ?', user_id]
31 topic.update_cached_post_fields(self)