From 8055a7c64b10c687447de6cd3d95441d114e80ea Mon Sep 17 00:00:00 2001 From: Toks Date: Tue, 24 Dec 2013 20:59:19 -0500 Subject: [PATCH] Fix order:comm and comment deletion for unbumped comments fixes #1351, fixes #1352 --- app/controllers/comments_controller.rb | 2 +- app/models/comment.rb | 16 ++++++----- ...748_add_last_comment_bumped_at_to_posts.rb | 27 +++++++++++++++++++ test/unit/comment_test.rb | 25 +++++++++++++---- 4 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20131225002748_add_last_comment_bumped_at_to_posts.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index a33119c36..07a5a6f3f 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -78,7 +78,7 @@ private end def index_by_post - @posts = Post.where("last_commented_at IS NOT NULL").tag_match(params[:tags]).reorder("last_commented_at DESC").paginate(params[:page], :limit => 5, :search_count => params[:search]) + @posts = Post.where("last_comment_bumped_at IS NOT NULL").tag_match(params[:tags]).reorder("last_comment_bumped_at DESC").paginate(params[:page], :limit => 5, :search_count => params[:search]) @posts.all respond_with(@posts) do |format| format.html {render :action => "index_by_post"} diff --git a/app/models/comment.rb b/app/models/comment.rb index 254f91760..d7b0fa8b1 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -10,7 +10,6 @@ class Comment < ActiveRecord::Base after_create :update_last_commented_at_on_create after_destroy :update_last_commented_at_on_destroy attr_accessible :body, :post_id, :do_not_bump_post - attr_accessor :do_not_bump_post module SearchMethods def recent @@ -140,8 +139,9 @@ class Comment < ActiveRecord::Base end def update_last_commented_at_on_create + Post.update_all(["last_commented_at = ?", created_at], ["id = ?", post_id]) if Comment.where("post_id = ?", post_id).count <= Danbooru.config.comment_threshold && !do_not_bump_post? - Post.update_all(["last_commented_at = ?", created_at], ["id = ?", post_id]) + Post.update_all(["last_comment_bumped_at = ?", created_at], ["id = ?", post_id]) end true end @@ -153,11 +153,15 @@ class Comment < ActiveRecord::Base else Post.update_all(["last_commented_at = ?", other_comments.first.created_at], ["id = ?", post_id]) end - true - end - def do_not_bump_post? - do_not_bump_post == "1" + other_comments = other_comments.where("do_not_bump_post = FALSE") + if other_comments.count == 0 + Post.update_all("last_comment_bumped_at = NULL", ["id = ?", post_id]) + else + Post.update_all(["last_comment_bumped_at = ?", other_comments.first.created_at], ["id = ?", post_id]) + end + + true end def editable_by?(user) diff --git a/db/migrate/20131225002748_add_last_comment_bumped_at_to_posts.rb b/db/migrate/20131225002748_add_last_comment_bumped_at_to_posts.rb new file mode 100644 index 000000000..69d93f86a --- /dev/null +++ b/db/migrate/20131225002748_add_last_comment_bumped_at_to_posts.rb @@ -0,0 +1,27 @@ +class AddLastCommentBumpedAtToPosts < ActiveRecord::Migration + def self.up + execute "SET statement_timeout = 0" + + rename_column :posts, :last_commented_at, :last_comment_bumped_at + rename_index :posts, "index_posts_on_last_commented_at", "index_posts_on_last_comment_bumped_at" + + add_column :posts, :last_commented_at, :datetime + + Post.joins(:comments).uniq.find_each do |post| + post.update_column(:last_commented_at, post.comments.last.created_at) + end + + add_column :comments, :do_not_bump_post, :boolean, :null => false, :default => false + end + + def self.down + execute "SET statement_timeout = 0" + + remove_column :posts, :last_commented_at + + rename_column :posts, :last_comment_bumped_at, :last_commented_at + rename_index :posts, "index_posts_on_last_comment_bumped_at", "index_posts_on_last_commented_at" + + remove_column :comments, :do_not_bump_posts + end +end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 2fbd67a16..50682dc25 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -54,16 +54,16 @@ class CommentTest < ActiveSupport::TestCase should "not bump the parent post" do post = FactoryGirl.create(:post) - comment = FactoryGirl.create(:comment, :do_not_bump_post => "1", :post => post) + comment = FactoryGirl.create(:comment, :do_not_bump_post => true, :post => post) post.reload - assert_nil(post.last_commented_at) + assert_nil(post.last_comment_bumped_at) comment = FactoryGirl.create(:comment, :post => post) post.reload - assert_not_nil(post.last_commented_at) + assert_not_nil(post.last_comment_bumped_at) end - should "not update the post after exceeding the threshold" do + should "not bump the post after exceeding the threshold" do Danbooru.config.stubs(:comment_threshold).returns(1) p = FactoryGirl.create(:post) c1 = FactoryGirl.create(:comment, :post => p) @@ -71,7 +71,22 @@ class CommentTest < ActiveSupport::TestCase c2 = FactoryGirl.create(:comment, :post => p) end p.reload - assert_equal(c1.created_at.to_s, p.last_commented_at.to_s) + assert_equal(c1.created_at.to_s, p.last_comment_bumped_at.to_s) + end + + should "always record the last_commented_at properly" do + post = FactoryGirl.create(:post) + Danbooru.config.stubs(:comment_threshold).returns(1) + + c1 = FactoryGirl.create(:comment, :do_not_bump_post => true, :post => post) + post.reload + assert_equal(c1.created_at.to_s, post.last_commented_at.to_s) + + Timecop.travel(2.seconds.from_now) do + c2 = FactoryGirl.create(:comment, :post => post) + post.reload + assert_equal(c2.created_at.to_s, post.last_commented_at.to_s) + end end should "not record the user id of the voter" do