From 9a3155d10b2e0186248dc6ae87de4b9362d167ac Mon Sep 17 00:00:00 2001 From: albert Date: Sat, 22 Oct 2011 13:47:46 -0400 Subject: [PATCH] can no longer delete posts in locked forum topics --- app/models/forum_post.rb | 3 ++- test/unit/forum_post_test.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 92e90cbf4..730403473 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -1,6 +1,6 @@ class ForumPost < ActiveRecord::Base attr_accessible :body, :topic_id - attr_accessible :body, :topic_id, :as => [:admin, :moderator, :janitor] + attr_accessible :body, :topic_id, :is_locked, :is_sticky, :as => [:admin, :moderator, :janitor] belongs_to :creator, :class_name => "User" belongs_to :topic, :class_name => "ForumTopic" before_validation :initialize_creator, :on => :create @@ -8,6 +8,7 @@ class ForumPost < ActiveRecord::Base after_save :update_topic_updated_at validates_presence_of :body, :creator_id validate :validate_topic_is_unlocked + before_destroy :validate_topic_is_unlocked scope :body_matches, lambda {|body| where(["forum_posts.text_index @@ plainto_tsquery(?)", body])} scope :for_user, lambda {|user_id| where("forum_posts.creator_id = ?", user_id)} search_methods :body_matches diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index 19255b93e..85a6d684c 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -14,6 +14,25 @@ class ForumPostTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + context "belonging to a locked topic" do + setup do + @post = Factory.create(:forum_post, :topic_id => @topic.id, :body => "zzz") + @topic.update_attribute(:is_locked, true) + @post.reload + end + + should "not be updateable" do + @post.update_attributes(:body => "xxx") + @post.reload + assert_equal("zzz", @post.body) + end + + should "not be deletable" do + @post.destroy + assert_equal(1, ForumPost.count) + end + end + should "update its parent when saved" do sleep 1 original_topic_updated_at = @topic.updated_at