This commit is contained in:
r888888888
2013-05-07 17:20:42 -07:00
parent c8f83f1442
commit 72b18051f8
2 changed files with 16 additions and 3 deletions

View File

@@ -7,7 +7,8 @@ class ForumPost < ActiveRecord::Base
before_validation :initialize_creator, :on => :create
before_validation :initialize_updater
before_validation :initialize_is_deleted, :on => :create
after_create :update_topic_updated_at
after_create :update_topic_updated_at_on_create
after_destroy :update_topic_updated_at_on_destroy
validates_presence_of :body, :creator_id
validate :validate_topic_is_unlocked
before_destroy :validate_topic_is_unlocked
@@ -87,7 +88,7 @@ class ForumPost < ActiveRecord::Base
creator_id == user.id || user.is_janitor?
end
def update_topic_updated_at
def update_topic_updated_at_on_create
if topic
topic.updater_id = CurrentUser.id
topic.response_count = topic.response_count + 1
@@ -95,6 +96,10 @@ class ForumPost < ActiveRecord::Base
end
end
def update_topic_updated_at_on_destroy
topic.update_column(:updated_at, ForumPost.where(:topic_id => topic.id).maximum(:updated_at))
end
def initialize_creator
self.creator_id = CurrentUser.id
end

View File

@@ -18,9 +18,11 @@ class ForumPostTest < ActiveSupport::TestCase
setup do
Danbooru.config.stubs(:posts_per_page).returns(3)
@posts = []
10.times do
9.times do
@posts << FactoryGirl.create(:forum_post, :topic_id => @topic.id, :body => rand(100_000))
end
sleep 2
@posts << FactoryGirl.create(:forum_post, :topic_id => @topic.id, :body => rand(100_000))
end
should "know which page it's on" do
@@ -29,6 +31,12 @@ class ForumPostTest < ActiveSupport::TestCase
assert_equal(2, @posts[5].forum_topic_page)
assert_equal(3, @posts[6].forum_topic_page)
end
should "update the topic's updated_at when deleted" do
@posts.last.destroy
@topic.reload
assert_equal(@posts[0].updated_at.to_s, @topic.updated_at.to_s)
end
end
context "belonging to a locked topic" do