class Post < ActiveRecord::Base belongs_to :forum, :counter_cache => true belongs_to :user, :counter_cache => true belongs_to :topic, :counter_cache => true format_attribute :body before_create { |r| r.forum_id = r.topic.forum_id } after_create { |r| Topic.update_all(['replied_at = ?, replied_by = ?, last_post_id = ?', r.created_at, r.user_id, r.id], ['id = ?', r.topic_id]) } after_destroy { |r| t = Topic.find(r.topic_id) ; Topic.update_all(['replied_at = ?, replied_by = ?, last_post_id = ?', t.posts.last.created_at, t.posts.last.user_id, t.posts.last.id], ['id = ?', t.id]) if t.posts.last } validates_presence_of :user_id, :body validate :body_cannot_contain_blacklisted_strings attr_accessible :body def editable_by?(user) user && (user.id == user_id || user.admin? || user.moderator_of?(topic.forum_id)) end def to_xml(options = {}) options[:except] ||= [] options[:except] << :topic_title << :forum_name super end def body_cannot_contain_blacklisted_strings downcase_body = body.downcase rescue '' blacklist = Blacklist.find(:first).list rescue '' prohibited = false blacklist.split("\n").each do |item| if downcase_body.include?(item) prohibited = true break end end errors.add(:body, "contains prohibited text") if prohibited == true end end