From d9fe78da209af2aa6f6d46875f9c11ba392a1013 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 20 Oct 2015 10:12:52 +0800 Subject: [PATCH] FIX: Topic#last_posted_at was not being set when changing topic timestamp. --- app/services/post_timestamp_changer.rb | 13 +++++++++---- spec/services/post_timestamp_changer_spec.rb | 2 ++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/services/post_timestamp_changer.rb b/app/services/post_timestamp_changer.rb index 65b056128f9..2364c12472e 100644 --- a/app/services/post_timestamp_changer.rb +++ b/app/services/post_timestamp_changer.rb @@ -8,15 +8,19 @@ class PostTimestampChanger def change! ActiveRecord::Base.transaction do - update_topic + last_posted_at = @timestamp @posts.each do |post| if post.is_first_post? update_post(post, @timestamp) else - update_post(post, Time.at(post.created_at.to_f + @time_difference)) + new_created_at = Time.at(post.created_at.to_f + @time_difference) + last_posted_at = new_created_at if new_created_at > last_posted_at + update_post(post, new_created_at) end end + + update_topic(last_posted_at) end # Burst the cache for stats @@ -29,11 +33,12 @@ class PostTimestampChanger @timestamp - @topic.created_at end - def update_topic + def update_topic(last_posted_at) @topic.update_attributes( created_at: @timestamp, updated_at: @timestamp, - bumped_at: @timestamp + bumped_at: @timestamp, + last_posted_at: last_posted_at ) end diff --git a/spec/services/post_timestamp_changer_spec.rb b/spec/services/post_timestamp_changer_spec.rb index 03ec9a66445..245eaa3df97 100644 --- a/spec/services/post_timestamp_changer_spec.rb +++ b/spec/services/post_timestamp_changer_spec.rb @@ -21,6 +21,8 @@ describe PostTimestampChanger do [:created_at, :updated_at].each do |column| expect(p1.public_send(column)).to be_within_one_second_of(new_timestamp) end + + expect(topic.last_posted_at).to be_within_one_second_of(p2.reload.created_at) end describe 'predated timestamp' do