From 72b18051f89db70510c0129836435ab11f99e528 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Tue, 7 May 2013 17:20:42 -0700 Subject: [PATCH] fixes #1546 --- app/models/forum_post.rb | 9 +++++++-- test/unit/forum_post_test.rb | 10 +++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 5d2b5822d..0d46d60d4 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -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 diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index f5929d7cc..1ded7d172 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -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