diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index b2c4a3da5..f36103a02 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -8,6 +8,7 @@ class ForumPost < ActiveRecord::Base before_validation :initialize_updater before_validation :initialize_is_deleted, :on => :create after_create :update_topic_updated_at_on_create + after_update :update_topic_updated_at_on_update_for_orignal_posts after_destroy :update_topic_updated_at_on_destroy validates_presence_of :body, :creator_id validate :validate_topic_is_unlocked @@ -95,6 +96,12 @@ class ForumPost < ActiveRecord::Base end end + def update_topic_updated_at_on_update_for_orignal_posts + if is_original_post? + topic.touch + end + end + def update_topic_updated_at_on_destroy max = ForumPost.where(:topic_id => topic.id).maximum(:updated_at) if max diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index 1d635b753..7d534fa3b 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -59,13 +59,33 @@ class ForumPostTest < ActiveSupport::TestCase end end - should "update its parent when saved" do + should "update the topic when created" do + @original_topic_updated_at = @topic.updated_at Timecop.travel(1.second.from_now) do - @original_topic_updated_at = @topic.updated_at + post = FactoryGirl.create(:forum_post, :topic_id => @topic.id) end - post = FactoryGirl.create(:forum_post, :topic_id => @topic.id) @topic.reload - assert_not_equal(@original_topic_updated_at, @topic.updated_at) + assert_not_equal(@original_topic_updated_at.to_s, @topic.updated_at.to_s) + end + + should "update the topic when updated only for the original post" do + posts = [] + 3.times do + posts << FactoryGirl.create(:forum_post, :topic_id => @topic.id, :body => rand(100_000)) + end + + # updating the original post + Timecop.travel(1.second.from_now) do + posts.first.update_attributes(:body => "xxx") + end + @topic.reload + assert_equal(posts.first.updated_at.to_s, @topic.updated_at.to_s) + + # updating a non-original post + Timecop.travel(2.seconds.from_now) do + posts.last.update_attributes(:body => "xxx") + end + assert_equal(posts.first.updated_at.to_s, @topic.updated_at.to_s) end should "be searchable by body content" do